Computer Science Canada

bouncing ball??

Author:  hpdudette [ Thu Jun 05, 2003 3:56 pm ]
Post subject:  bouncing ball??

i'm trying to make this ball bounce in diagonally across the screen but i can't seem to figure out how to make it bounce back or bounce from top to bottom
for x:10..360
    drawfilloval (x, x, 30, 30, 4)

    delay (5)

    drawfilloval (x, x, 30, 30, 0)
end for

Author:  PaddyLong [ Thu Jun 05, 2003 4:08 pm ]
Post subject: 

easiest way to do a bouncing ball around the screen is something like this...


const speed := 5 %the ammount the ball is moved
const radius := 25 %the radius of the ball
var xmove, ymove : int := speed %the amount it moves on the x and y axis
var x, y : int %the ball's x and y coordinates on the screen
var colr : int := 7 %the colour of the ball

%start the ball in the middle of the screen
x := maxx div 2
y := maxy div 2

    %draw the ball, wait a short time, then draw a white one on top of it
    drawfilloval (x, y, radius, radius, colr)
    delay (25)
    drawfilloval (x, y, radius, radius, 0)
    %move the ball's coordinates the respective x and y amounts
    x += xmove
    y += ymove
    if x >= maxx - radius or x <= radius then
        %if the middle of the ball (x) is at the edge of the screen
        xmove *= -1 %switch the x direction
    end if
    if y >= maxy - radius or y <= radius then
        %if the middle of the ball (y) is at the top of bottom of the screen
        ymove *= -1  %switch the y direction
    end if
end loop

Author:  naoki [ Thu Jun 05, 2003 4:28 pm ]
Post subject: 

i use something like

if ballx is near 0 or maxx then
xchange := - xchange
end if
if bally is near 0 or maxy then
ychange := ychange
end if
ballx += xchange
bally += ychange

this way only the increment by which you're moving the ball left/right, up/down will change from negative to positive, thus doing all the work for you

Author:  PaddyLong [ Thu Jun 05, 2003 6:07 pm ]
Post subject: 

that's what mine is doing by multiplying it by -1 ..