home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter09 / demo09-01.bb next >
Encoding:
Text File  |  2003-04-06  |  1.6 KB  |  68 lines

  1. ;demo09-01.bb - Demonstrates Pixel Collisions
  2. Graphics 400,300
  3.  
  4. ;create variables that define coordinate position of pixel
  5. Global x = 200
  6. Global y = 150
  7.  
  8. ;This variable contains the amount of times a collision has occured
  9. collisions = 0
  10.  
  11. ;CONSTANTS
  12. ;These are the key code constants
  13. Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205 
  14.  
  15. ;MAIN LOOP
  16. While Not KeyDown (1)
  17.  
  18. ;Print text in upper left corner
  19. Locate 0,0 
  20.  
  21. Print "Press the arrow keys to move the pixel around."
  22.  
  23. ;Print the number of collisions
  24. Print "Collisions: " + collisions
  25.  
  26. ;Move player around depending on the key he pressed
  27. If KeyDown(UPKEY) 
  28.     y = y - 5 
  29. ElseIf KeyDown(DOWNKEY)
  30.     y = y + 5 
  31. ElseIf KeyDown(LEFTKEY)
  32.     x = x - 5 
  33. ElseIf KeyDown(RIGHTKEY)
  34.     x = x + 5 
  35. EndIf
  36.  
  37. ;Call the CheckForCollisions function and determine if a collision occurred
  38. collisions = CheckForCollisions(collisions)
  39.  
  40. ;Draw the pixel on the screen
  41. Plot x,y 
  42.  
  43. ;wait a (fraction of a )sec
  44. Delay 100 
  45. Wend
  46. ;END OF MAIN LOOP
  47.  
  48.  
  49. ;FUNCTIONS
  50.  
  51. ;Function CheckForCollisions(collisions) - Returns number of total collisions, tests for new ones
  52. ;collisions: the number of collisions at the time of calling the function
  53. Function CheckForCollisions(collisions)
  54.  
  55. ;If the pixel is offscreen, report a collision
  56. If x <= 0 Or x >= 400 Or y <= 0 Or y >= 300
  57.     collisions = collisions + 1     ;increment collisions
  58.     Cls     ;clear the screen
  59.     Text 100,150,"A Collision Has Occured"
  60.     Delay 1000     ;wait a sec
  61.     Cls     ;clear screen again
  62.     x = 200     ;reset x
  63.     y = 150     ;reset y
  64. EndIf
  65.  
  66. ;return the amount of collisions
  67. Return collisions 
  68. End Function