Computer Science Canada

bouncing:ball

Author:  gilbamy [ Fri May 26, 2006 11:13 am ]
Post subject:  bouncing:ball

Alright i need some direction with this program okay the bloody ball won't bounce around the screen and how do i do borders so the ball won't go off the screen here is my code oh and i tried using Pic.Free but it would not work i think it is though in my code
code:
%Amy Gilbank
%May 18 2006
%bounceball.t
%a progrsm that allows picture to bounce around
var winMain : int := Window.Open ("graphics:800;600")
var pic1, pic1w, pic1h, pic1x, pic1y, x, y, speedx, speedy,backg : int
pic1 := Pic.FileNew ("ball.bmp")
pic1w := Pic.Width (pic1)
pic1h := Pic.Height (pic1)
pic1x := 100
pic1y := 200
speedx := 90
speedy := 80
loop
    backg := Pic.New (pic1x,pic1y, pic1x + pic1w,pic1y - pic1h)
        Pic.Draw (pic1, pic1x, pic1y,picCopy)
     delay (500)
        Pic.Draw ( backg,pic1x, pic1y, picCopy)
                                      if pic1y >= 400 - pic1h or pic1y <= 0 then
        speedy := -speedy
    end if
    if pic1x >= 700 - pic1w or pic1x <= 0 then
        speedx := -speedx
    end if
    pic1y := pic1y + speedy
    pic1x := pic1x + speedx
     end loop

Author:  jamonathin [ Fri May 26, 2006 12:19 pm ]
Post subject: 

Here's an example i pulled together for you. One thing you have to keep in mind is:

The velocity starts at 10, and keeps being decreased by 0.5. Eventually it will be negative, once it does, a negative number is being subracted - which then means it is being added.

This is when the ball goes up.

code:

var ballx, bally, vx, vy, gravity : real
ballx := 100 %Ball x-value
bally := 100 %Ball y-value
vx := 5 %Velocity X-value -- speed the ball's x-values moves at
vy := 10 %Velocity Y-value -- speed the ball's y-value moves at
gravity := .25 %Amount the y-speed get's reduced by
View.Set ("offscreenonly")
loop
    cls
    drawline (0, 100, maxx, 100, black) %Floor
    drawfilloval (round (ballx), round (bally), 10, 10, green) %Ball
    ballx += vx %Moving x-value
    bally += vy %Moving y-value
    vy -= gravity %applying gravity
    if vy = -10 then %If it's reached it's maximum
        vy := 10 - gravity %Reset it
    end if
    if ballx + 15 >= maxx or ballx - 15 <= 0 then %Hitting the walls
        vx *= -1 %Change direction of x-value
    end if
    View.Update
    delay (25)
    exit when hasch
end loop

Author:  NikG [ Sat May 27, 2006 12:10 am ]
Post subject: 

Haha jamonathin, almost looks like I'm watching a tennis match from the side.

Author:  upthescale [ Sat May 27, 2006 2:55 pm ]
Post subject: 

yes it does, pretty cool stuff


: