Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 bouncing ball??
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hpdudette




PostPosted: 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
code:
for x:10..360
    drawfilloval (x, x, 30, 30, 4)

    delay (5)

    drawfilloval (x, x, 30, 30, 0)
end for
Sponsor
Sponsor
Sponsor
sponsor
PaddyLong




PostPosted: Thu Jun 05, 2003 4:08 pm   Post subject: (No subject)

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

code:

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


loop
    %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
naoki




PostPosted: Thu Jun 05, 2003 4:28 pm   Post subject: (No 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
PaddyLong




PostPosted: Thu Jun 05, 2003 6:07 pm   Post subject: (No subject)

that's what mine is doing by multiplying it by -1 ..
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: