TGC Codebase Backup



Bouncing balls with gravity by flibX0r

14th May 2004 8:45
Summary

Creates as random number of balls and gravity spots, and then runs the simulation. Works as a nice screensaver. rem out the cls for a really cool effect



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on
sync rate 0
hide mouse

randomize timer()

NUMBEROFBALLS = rnd(6)+1
NUMBEROFGRAV = rnd(4)+1

SCREENWIDTH = 640
SCREENHEIGHT = 480

global timer1=0
global timer2=0

dim dx(NUMBEROFBALLS,40) as float
dim dy(NUMBEROFBALLS,40) as float

type balltype
   x# as float
   y# as float
   vx# as float
   vy# as float
   ax# as float
   ay# as float
   c1 as integer : c2 as integer : c3 as integer
endtype


type gravtype
   x# as float
   y# as float
   size# as float
endtype

dim ball(NUMBEROFBALLS) as balltype
dim grav(NUMBEROFGRAV) as gravtype

GRAVITY# as float
FRICTION# as float

`Init the balls
for j=1 to NUMBEROFBALLS
   ball(j).x# = rnd(SCREENWIDTH-40)+20
   ball(j).y# = rnd(SCREENHEIGHT-40)+20
   ball(j).vx# = 0.0
   ball(j).vy# = 0.0
   ball(j).ax# = 0.0
   ball(j).ay# = 0.0
   ball(j).c1 = 128*rnd(1)+127
   ball(j).c2 = 128*rnd(1)+127
   ball(j).c3 = 128*rnd(1)+127
   for i=1 to 40
         dx(j,i) = ball(j).x#
         dy(j,i) = ball(j).y#
   next i
next j

`init the planets (gravs)
for j=1 to NUMBEROFGRAV
   grav(j).x# = rnd(SCREENWIDTH-100) + 70.0
   grav(j).y# = rnd(SCREENHEIGHT-100) + 70.0
   grav(j).size# = rnd(30) + 10.0
next j

GRAVITY# = 0.1
FRICTION# = 0.95

do

   cls

   set cursor 0,0

   for j=1 to NUMBEROFBALLS

      `Reset the acceleration vars
      ball(j).ax# = 0.0
      ball(j).ay# = 0.0

      `Shufle the trail array
      for i=39 to 1 step -1
        dx(j,i+1) = dx(j,i)
        dy(j,i+1) = dy(j,i)
      next i

      `Gravity
      for i=1 to NUMBEROFGRAV
         `Calculate the distance and gravity effect of each grav
         dist# = sqrt((ball(j).x#-grav(i).x#)^2 + (ball(j).y#-grav(i).y#)^2)
         accel# = (GRAVITY# * grav(i).size#) / dist#
         `Update the acceleration and velocity
         ball(j).ax# = ball(j).ax# + accel#*((grav(i).x#-ball(j).x#)/dist#)
         ball(j).ay# = ball(j).ay# + accel#*((grav(i).y#-ball(j).y#)/dist#)
         ball(j).vx# = ball(j).vx# + ball(j).ax#
         ball(j).vy# = ball(j).vy# + ball(j).ay#
      next i

      ball(j).vx# = ball(j).vx# * FRICTION#
      ball(j).vy# = ball(j).vy# * FRICTION#


      `Update the velocities and positions
      ball(j).x# = ball(j).x# + ball(j).vx#
      ball(j).y# = ball(j).y# + ball(j).vy#

      `Check screen limits and bounce if neccesary
      if ball(j).x#<10.0
         ball(j).x#=10.0
         ball(j).vx# = -ball(j).vx# * FRICTION#
         ball(j).vy# = ball(j).vy# * FRICTION#
      endif
      if ball(j).x#>SCREENWIDTH-10
         ball(j).x#=SCREENWIDTH-10
         ball(j).vx# = -ball(j).vx# * FRICTION#
         ball(j).vy# = ball(j).vy# * FRICTION#
      endif
      if ball(j).y#<10.0
         ball(j).y#=10.0
         ball(j).vx# = ball(j).vx# * FRICTION#
         ball(j).vy# = -ball(j).vy# * FRICTION#
      endif
      if ball(j).y#>SCREENHEIGHT-10
         ball(j).y#=SCREENHEIGHT-10
         ball(j).vx# = ball(j).vx# * FRICTION#
         ball(j).vy# = -ball(j).vy# * FRICTION#
      endif

      `Update trail position
      dx(j,1) = ball(j).x#
      dy(j,1) = ball(j).y#

      `Draw trail
      for i=39 to 1 step -1
         `ink rgb(ball(j).c1,ball(j).c2,ball(j).c3),0
         ink rgb(pos(ball(j).c1,i*255/40),pos(ball(j).c2, i*255/40),pos(ball(j).c3, i*255/40)),0

         circle dx(j,i+1), dy(j,i+1), 9*(1-(i/40.0))
      next i

      `draw ball
      ink rgb(ball(j).c1,ball(j).c2,ball(j).c3),0
      circle ball(j).x#, ball(j).y#, 9

   next j

   for i=1 to NUMBEROFGRAV
      ink rgb(255,255,255),0
      circle grav(i).x#, grav(i).y#, grav(i).size#/2
   next i

   `print true fps
   ink rgb(255,255,255),0
   print framespersecond()

   sync

loop

end


function pos(value1 as float, value2 as float)

   if value1-value2<0 then exitfunction 0

endfunction (value1-value2)


function framespersecond()

   timer2 = timer()

   result = 1000/(timer2-timer1)

   timer1 = timer()

endfunction result