Computer Science Canada

better bouncing balls

Author:  lufthansa747 [ Thu Dec 03, 2009 10:06 am ]
Post subject:  better bouncing balls

View.Set ("graphics:max;max,offscreenonly,nobuttonbar")
const numBall : int := 500

class ball

export startUp, drawBall
var x : int := maxx div 2
var y : int := maxy div 2
var radius : int := 10
var xint, yint : int := 5
var upState, sideState : int := -1

procedure startUp (initside, initup : int)
upState := initup
sideState := initside
Draw.FillBox (0, 0, maxx, maxy, black)
Draw.FillOval (x, y, radius, radius, white)
end startUp

procedure drawBall ()
if x - xint <= 0 and sideState < 0 then %Check left wall
sideState := -sideState
end if
if x + xint >= maxx and sideState > 0 then % check right wall
sideState := -sideState
end if
if y + yint >= maxy and upState > 0 then %check top wall
upState := -upState
end if
if y - yint <= 0 and upState < 0 then % check bottom wall
upState := -upState
end if
Draw.FillOval (x, y, radius, radius, black)
x += xint * sideState
y += yint * upState
Draw.FillOval (x, y, radius, radius, white)
end drawBall
end ball

var balls : array 1 .. numBall of pointer to ball
var randl : int := -5
var randh : int := 5

for i : 1 .. numBall
new ball, balls (i)
balls (i) -> startUp (Rand.Int (randl, randh), Rand.Int (randl, randh))
end for

loop
for i : 1 .. numBall
balls (i) -> drawBall ()
end for
View.Update
end loop

you can change the amount of balls and how fast they move buy changing the array size and chaning the randl and randh but -5 and 5 look the coolest


: