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

  1. ;demo09-04.bb - Demonstrates Bounding Rectangles
  2. Graphics 800,600 
  3.  
  4. ;Set up backbuffer and automidhandle
  5. SetBuffer BackBuffer() 
  6. AutoMidHandle True 
  7.  
  8. ;Seed the random generator
  9. SeedRnd (MilliSecs()) 
  10.  
  11.  
  12. ;CONSTANTS
  13.  
  14. ;The number of objects on a screen at one time
  15. Const NUMBEROFOBJECTS = 50 
  16.  
  17. ;The key code constants
  18. Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205 
  19.  
  20. ;How many pixels should ship move per frame
  21. Const MOVEX = 5
  22. Const MOVEY = 5
  23.  
  24.  
  25. ;TYPES
  26.  
  27. ;The point type defines each object that can be hit by the ship
  28. Type point 
  29.     Field x,y     ;the x and y coordinate of the ship
  30. End Type
  31.  
  32. ;This type contains the player
  33. Type player 
  34.     Field x,y    ;the x and y coordinate of the player
  35.     Field collisions    ;the number of collisions that have occurred
  36.     Field image     ;the actual image of the player
  37. End Type
  38.  
  39.  
  40. ;Create the player and give him beginning variables
  41. Global player.player = New player 
  42. player\collisions = 0 
  43. player\image = LoadImage("ship.bmp") 
  44.  
  45. ;Reset the level with new points and new coordinates
  46. ResetLevel() 
  47.  
  48.  
  49. ;MAIN LOOP
  50. While Not KeyDown(1)
  51. ;Clear the screen
  52. Cls 
  53.  
  54. ;Reset text to the top left of screen
  55. Locate 0,0 
  56. Print "X: " + player\x 
  57. Print "Y: " + player\y 
  58. Print "Collisions: " + player\collisions 
  59.  
  60. ;Test what keys have been pressed
  61. TestKeys()
  62.  
  63. ;If there have been any collisions, incremen collision counter and reset level
  64. If(TestCollisions() = 1) 
  65.     player\collisions = player\collisions + 1 
  66.     ResetLevel()     
  67. EndIf
  68.  
  69.  
  70. ;Draw each point on the screen
  71. For point.point = Each point 
  72.     Plot point\x, point\y 
  73. Next
  74.  
  75. ;Draw the player
  76. DrawImage player\image,player\x,player\y 
  77.  
  78. ;Wait a (fraction of a) sec
  79. Delay 50 
  80.  
  81. Flip
  82. Wend
  83. ;END OF MAIN LOOP
  84.  
  85.  
  86. ;FUNCTIONS
  87. ;FUNCTION ResetLevel() - Resets the level, creates new random points, resets the player's position
  88. ;No input parameters
  89. ;No return value
  90. Function ResetLevel()
  91.  
  92. ;Delete all remaining objects
  93. For point.point = Each point 
  94.     Delete point 
  95. Next
  96.  
  97. ;Create NUMBEROFOBJECTS (default 50) points and assign them random x/y coordinates
  98. For counter = 0 To NUMBEROFOBJECTS 
  99.     point.point = New point     ;Create the point
  100.  
  101. ;Find random x and y coordinates
  102.     point\x = Rand (0,800) 
  103.     point\y = Rand (0,600) 
  104. Next
  105.  
  106. ;Reset the player's coordinate position
  107. player\x = 400 
  108. player\y = 300 
  109. End Function
  110.  
  111.  
  112. ;END ResetLevel()
  113.  
  114.  
  115. ;FUNCTION TestCollisions() - Tests the objects and the ship for collisions
  116. ;No input parameters
  117. ;Returns 1 if there was a collision, 0 if there was none
  118. Function TestCollisions()
  119.  
  120. ;Test each point to see if it is within the player's radius
  121. For point.point = Each point 
  122.  
  123. ;Find player's bounding box
  124. x1 = -ImageWidth(player\image)/2 + player\x
  125. x2 = ImageWidth(player\image)/2 + player\x
  126. y1 = -ImageHeight(player\image)/2 + player\y
  127. y2 = ImageHeight(player\image)/2 + player\y
  128.  
  129. ;If the point is within collision radius, return 1
  130.     If (point\x > x1) And (point\x < x2) And (point\y > y1) And (point\y < y2)
  131.         Return 1
  132.     EndIf
  133. Next    ;Move on to next point
  134.  
  135. ;There were no collisions if the function makes it here, so return 0
  136. Return 0 
  137. End Function
  138. ;END TestCollisions()
  139.  
  140.  
  141.  
  142. ;FUNCTION TestKeys() - Tests all of the keys to see if they were hit
  143. ;No input parameters
  144. ;No return value
  145. Function TestKeys()
  146.  
  147. ;If up is hit, move player up
  148. If KeyDown(UPKEY) 
  149.     player\y = player\y - MOVEY 
  150. EndIf
  151.  
  152. ;If down is hit, move player down
  153. If KeyDown(DOWNKEY) ;If down was hit
  154.     player\y = player\y + MOVEY 
  155. EndIf
  156.  
  157. ;If left is hit, move player left
  158. If KeyDown(LEFTKEY) 
  159.     player\x = player\x - MOVEX 
  160. EndIf
  161.  
  162. ;If right is hit, move player right
  163. If KeyDown(RIGHTKEY) 
  164.     player\x = player\x + MOVEX
  165. EndIf
  166.  
  167. End Function
  168. ;END TestKeys()