home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 60 / supercd60_2.iso / BlitzBasic / Blitz2DPCP / data1.cab / Support / help / samples / Balls / balls.bb next >
Encoding:
Text File  |  2001-11-21  |  1.9 KB  |  65 lines

  1. ; Simple ball test
  2. ; By Steve Smith
  3. ; (my first ever Blitz program)
  4. ; press left mouse to quit
  5.  
  6. Const c_balls=10; Number of Balls
  7.  
  8. Global g_numballs=c_balls
  9.  
  10. Type t_ball     ;    Define the type for all balls
  11.     Field x#    ;    x coord of ball
  12.     Field y#    ;     y coord of ball
  13.     Field dx#    ;     x movement
  14.     Field dy#    ;     y movement
  15.     Field im    ;      Image number
  16.     Field s#    ;     speed of ball
  17.     End Type
  18.  
  19.     Graphics 640,480; Set Graphics mode
  20.  
  21. Global g_balls=LoadAnimImage("balls.png",24,24,0,8) ; Load ball pictures
  22. Global g_bg=LoadImage("bg.jpg")                        ; Load background
  23. Global g_collide=LoadSound("hit.wav")                ; Load Sound
  24.  
  25.  
  26.     For a=1 To g_numballs            ;loop to create all balls
  27.         balls.t_ball=New t_ball        ;Create a new ball
  28.         balls\x#=Rand(600)+16        ;random X coord
  29.         balls\y#=Rand(400)+16        ;random Y coord
  30.         balls\dx#=Rnd(2)-1            ;movement between -1 and 1    
  31.         balls\dy#=Rnd(2)-1            ;
  32.         balls\im=a Mod 8            ;image frame
  33.         balls\s#=Rnd(2)+.3            ;speed
  34.         Next 
  35.     
  36.     SetBuffer BackBuffer()
  37.     
  38.     While MouseDown(1)=0
  39.         TileBlock g_bg,0,0
  40.          For balls.t_ball=Each t_ball
  41.             balls\x#=balls\x#+(balls\dx#*balls\s#)
  42.             balls\y#=balls\y#+(balls\dy#*balls\s#)
  43.             ; check for screen limits
  44.             If balls\y#<0      Then balls\dy#=1
  45.             If balls\y#>480-24 Then balls\dy#=-1
  46.             If balls\x#<0      Then balls\dx#=1
  47.             If balls\x#>640-24 Then balls\dx#=-1
  48.             ; check for colisions with other balls
  49.             For balls2.t_ball=Each t_ball
  50.                 If balls<>balls2 Then ; make sure it's not the same ball
  51.                     If ImagesCollide(g_balls,balls\x#,balls\y#,balls\im,g_balls,balls2\x#,balls2\y#,balls2\im) Then 
  52.                         If balls\x#<balls2\x# Then balls\dx#=-1
  53.                         If balls\x#>balls2\x# Then balls\dx#=1
  54.                         If balls\y#<balls2\y# Then balls\dy#=-1
  55.                         If balls\y#>balls2\y# Then balls\dy#=1
  56.                         PlaySound(g_collide)
  57.                         End If
  58.                     End If
  59.                 Next
  60.             DrawImage g_balls,balls\x#,balls\y#,balls\im
  61.             Next
  62.         Flip 1
  63.         Wend
  64.     EndGraphics
  65.     End