Add Boundaries
Author |
Message |
SunsFan13
|
Posted: Tue Apr 08, 2008 9:41 am Post subject: Add Boundaries |
|
|
Hey again.
How would I go about adding boundaries to the following program?
As it is right now, the 'balls' just go right off the screen until you bring them back..
I know, or think, its something to do with maxx and maxy.
Help me out here?
Thanks,
-Chris
code: |
Text.Color (brightblue)
put "Use the arrow keys to move the red ball."
put "Use W,A,S,D to move the green ball."
colorback (black)
delay (2200)
cls % Clear Screen
process ball1
setscreen ("graphics")
var key : array char of boolean
var x, y, r : int
x := maxx div 2 % Start at center
y := maxy div 2 % Start at center
r := 10 % Ball radius
loop
% Draw circle
drawfilloval (x, y, r, r, brightred)
Input.KeyDown (key)
% Move Left
if key (KEY_LEFT_ARROW) then
delay (85)
drawfilloval (x, y, r, r, black)
x := x - r
% Move Right
elsif key (KEY_RIGHT_ARROW) then
delay (85)
drawfilloval (x, y, r, r, black)
x := x + r
% Move Up
elsif key (KEY_UP_ARROW) then
delay (85)
drawfilloval (x, y, r, r, black)
y := y + r
% Move Down
elsif key (KEY_DOWN_ARROW) then
delay (85)
drawfilloval (x, y, r, r, black)
y := y - r
end if
end loop
end ball1 %End process ball1
colorback (black)
process ball2 %Begin process ball2
var key : array char of boolean
var x, y, r : int
x := maxx div 3
y := maxy div 3
r := 10 % Ball radius
loop
% Draw circle
drawfilloval (x, y, r, r, brightgreen)
Input.KeyDown (key)
% Move Left
if key ('a') then
delay (85)
drawfilloval (x, y, r, r, black)
x := x - r
% Move Right
elsif key ('d') then
delay (85)
drawfilloval (x, y, r, r, black)
x := x + r
% Move Up
elsif key ('w') then
delay (85)
drawfilloval (x, y, r, r, black)
y := y + r
% Move Down
elsif key ('s') then
delay (85)
drawfilloval (x, y, r, r, black)
y := y - r
end if
end loop
end ball2
fork ball1
fork ball2
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
BigBear
|
Posted: Tue Apr 08, 2008 10:15 am Post subject: Re: Add Boundaries |
|
|
First I would suggest not using process except for music and only for that in older versions because multiple process randomly run one or the other so the outcome of the program can be different every time. Instead you should have one loop that gets the input for both players and moves both the balls with 2 if statements.
Here is a full explanation http://compsci.ca/v3/viewtopic.php?t=7842
Now the boundaries part, when you move the circles around you know where there are. For example ball1 starts in the middle and if you go left then it is one pixel left of the center. To make sure they do not leave the screen you need to only move
Left - if they are not right next to the left side of the screen
Right - if they are not right next to the right edge of the screen
Up - if they are not at the top of the screen
Down - if they are not at the bottom of your screen
So now you have to put the above into if statements when you check to see what key they entered.
Note: left side of screen is 0, right side of screen is maxx (or maxx minus something), top of screen is maxxy and bottom is 0.
You still need to figure out when which part is at 0 or a maxx/maxy of the screen.
Hope this helps. |
|
|
|
|
![](images/spacer.gif) |
SunsFan13
|
Posted: Wed Apr 09, 2008 3:39 pm Post subject: Re: Add Boundaries |
|
|
Yeah, got it.
Thanks Bear.
.. Last part I gotta figure out is how to get it to display something (ie. "You Win") when one ball hits the second ball. |
|
|
|
|
![](images/spacer.gif) |
BigBear
|
Posted: Wed Apr 09, 2008 4:12 pm Post subject: Re: Add Boundaries |
|
|
Well that seem like a weird win condition especially if it is a two player game. You meant when one user controlled circle get a randomly placed non-movable circle right? A race to the circle sounds interesting.
Same type of thing collision detection check to see if the coordinates of the user controlled circle is inside the coordinates of the non-movable circle. I am sure you can figure it out because you solved the first problem with very little help. There a many tutorials on collision detection you should check out. |
|
|
|
|
![](images/spacer.gif) |
Sean
![](http://compsci.ca/v3/uploads/user_avatars/47413941748406f441f83e.png)
|
Posted: Wed Apr 09, 2008 4:16 pm Post subject: Re: Add Boundaries |
|
|
If you are building the game as a race to the other dot between both players, you will want to check the x and y position of both dots, and see which one matches the destination dot.
You will need to do a comparison between the x and y co-ordinates of the player to the dot in one if statement. In your if statement, you will need a put statement with the desired message.
You may run into trouble where both equal it, and will have both messages appearing. |
|
|
|
|
![](images/spacer.gif) |
Tallguy
![](http://compsci.ca/v3/uploads/user_avatars/515706924539b443d32a6e.gif)
|
Posted: Wed Apr 16, 2008 8:39 am Post subject: RE:Add Boundaries |
|
|
use a for statement for ur maxx maxy etc, and if the ball enters the set distances the balls bounce back, this is one of the simplest ways |
|
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Sat Apr 19, 2008 8:54 am Post subject: RE:Add Boundaries |
|
|
Do it like this:
Turing: |
Text.Color (brightblue)
put "Use the arrow keys to move the red ball."
put "Use W,A,S,D to move the green ball."
colorback (black)
Input.Pause
cls % Clear Screen
setscreen ("graphics:offscreenonly")
var key : array char of boolean
var x, y, r, a, b, c : int
x := maxx div 2 % Start at center
y := maxy div 2 % Start at center
r := 10 % Ball radius
a := maxx div 3
b := maxy div 3
c := 10 % Ball radius
loop
% Draw circle
drawfilloval (x, y, r, r, brightred)
Input.KeyDown (key )
% Move Left
if key (KEY_LEFT_ARROW) then
drawfilloval (x, y, r, r, black)
x := x - r
% Move Right
elsif key (KEY_RIGHT_ARROW) then
drawfilloval (x, y, r, r, black)
x := x + r
% Move Up
elsif key (KEY_UP_ARROW) then
drawfilloval (x, y, r, r, black)
y := y + r
% Move Down
elsif key (KEY_DOWN_ARROW) then
drawfilloval (x, y, r, r, black)
y := y - r
end if
colorback (black)
% Draw circle
drawfilloval (a, b, c, c, brightgreen)
Input.KeyDown (key )
% Move Left
if key ('a') then
drawfilloval (a, b, c, c, black)
a := a - c
% Move Right
elsif key ('d') then
drawfilloval (a, b, c, c, black)
a := a + c
% Move Up
elsif key ('w') then
drawfilloval (a, b, c, c, black)
b := b + c
% Move Down
elsif key ('s') then
drawfilloval (a, b, c, c, black)
b := b - c
end if
% This is where the magic happens (lol)
if x > maxx - 10 then
x := maxx - 10
elsif x < 10 then
x := 10
end if
if a > maxx - 10 then
a := maxx - 10
elsif a < 10 then
a := 10
end if
if y > maxy - 10 then
y := maxy - 10
elsif y < 10 then
y := 10
end if
if b > maxy - 10 then
b := maxy - 10
elsif b < 10 then
b := 10
end if
View.Update
delay (15)
end loop
|
I also got rid of your forks. You shouldn't use those unless you want background music. |
|
|
|
|
![](images/spacer.gif) |
|
|