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

  1. ;demo09-02.bb - Demonstrates Bounding Circles
  2. Graphics 800,600 
  3.  
  4. ;Set up back buffer and automidhandle
  5. SetBuffer BackBuffer() 
  6. AutoMidHandle True 
  7.  
  8. ;Seed the random generator
  9. SeedRnd (MilliSecs()) 
  10.  
  11.  
  12. ;CONSTANTS
  13. ;The number of collidable objects on screen
  14. Const NUMBEROFOBJECTS = 50
  15.  
  16. ;The key code constants
  17. Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205 ;The key code constants
  18.  
  19. ;How many pixels the ship moves per frame
  20. Const MOVEX = 5
  21. Const MOVEY = 5
  22.  
  23.  
  24. ;TYPES
  25.  
  26. ;the point type defines each object that can be hit by the ship
  27. Type point 
  28.     Field x,y     ;the x and y coordinate of the ship
  29. End Type
  30.  
  31. ;The player type is the space ship on the screen
  32. Type player 
  33.  
  34.     Field x,y     ;the x and y coordinate of the player
  35.     Field collisions     ;the number of collisions that have occurred
  36.     Field radius      ;the radius of the player image
  37.     Field image     ;the actual image of the player
  38.  
  39. End Type
  40.  
  41.  
  42. ;Create the player and define his starting variables
  43. Global player.player = New player 
  44. player\collisions = 0 
  45. player\image = LoadImage("ship.bmp") 
  46.  
  47. ;Find the radius of the player image
  48. player\radius = FindRadius(player\image) 
  49.  
  50. ;Reset the level and give it new points, coords, etc.
  51. ResetLevel() 
  52.  
  53.  
  54. ;GAME LOOP
  55. While Not KeyDown(1)
  56.  
  57. ;Clear the screen
  58. Cls
  59.  
  60. ;Make text appear in top left hand corner
  61. Locate 0,0 
  62. Print "X: " + player\x     ;Write out the x coordiante of the player
  63. Print "Y: " + player\y     ;Write out the y coordinate of the player
  64. Print "Collisions: " + player\collisions     ;Write out how many collisions have occured
  65.  
  66. ;Test to see if player has pressed any keys
  67. TestKeys() 
  68.  
  69. ;If there were any collisions, increment the  collision counter and reset the level
  70. If(TestCollisions() = 1) 
  71.     player\collisions = player\collisions + 1 
  72.     ResetLevel() 
  73. EndIf
  74.  
  75. ;Plot every point on the screen
  76. For point.point = Each point 
  77.     Plot point\x, point\y 
  78. Next
  79.  
  80. ;Draw the player
  81. DrawImage player\image,player\x,player\y
  82.  
  83. ;Delay the program a few millisecs
  84. Delay 50
  85.  
  86. Flip
  87. Wend
  88. ;END OF MAIN LOOP
  89.  
  90.  
  91. ;FUNCTIONS
  92.  
  93. ;FUNCTION ResetLevel() - Resets the level, creates new random points, resets the player's position
  94. ;No input parameters
  95. ;No return value
  96. Function ResetLevel()
  97.  
  98. ;Delete every point on screen
  99. For point.point = Each point 
  100.     Delete point 
  101. Next
  102.  
  103. ;Create NUMBEROFOBJECTS new points with random x and y coords
  104. For counter = 0 To NUMBEROFOBJECTS 
  105.     point.point = New point 
  106.     point\x = Rand (0,800) 
  107.     point\y = Rand (0,600) 
  108. Next
  109.  
  110. ;Reset the player's coordinates
  111. player\x = 400 
  112. player\y = 300 
  113. End Function
  114.  
  115.  
  116.  
  117. ;FUNCTION TestCollisions() - Tests the objects and the ship for collisions
  118. ;No input parameters
  119. ;Returns 1 if there was a collision, 0 if there was none
  120. Function TestCollisions()
  121.  
  122. ;Check every object to see if it is within player's radius. If it is, return that there was a collision.
  123. For point.point = Each point 
  124.     If Distance(player\x,player\y,point\x,point\y) < player\radius 
  125.         Return 1 
  126.     EndIf
  127. Next
  128.  
  129. ;If there was no collision, return 0
  130. Return 0 ;There was no collision
  131.  
  132. End Function
  133.  
  134.  
  135.  
  136. ;FUNCTION TestKeys() - Tests all of the keys to see if they were hit
  137. ;No input parameters
  138. ;No return value
  139. Function TestKeys()
  140.  
  141. ;If up is hit, move player up
  142. If KeyDown(UPKEY) 
  143.     player\y = player\y - MOVEY 
  144. EndIf
  145.  
  146. ;If down is hit, move player down
  147. If KeyDown(DOWNKEY) ;If down was hit
  148.     player\y = player\y + MOVEY 
  149. EndIf
  150.  
  151. ;If left is hit, move player left
  152. If KeyDown(LEFTKEY) 
  153.     player\x = player\x - MOVEX 
  154. EndIf
  155.  
  156. ;If right is hit, move player right
  157. If KeyDown(RIGHTKEY) 
  158.     player\x = player\x + MOVEX
  159. EndIf
  160.  
  161. End Function
  162. ;END TestKeys()
  163.  
  164.  
  165. ;FUNCTION Distance(x1,y1,x2,y2) - Finds the distance between two points
  166. ;x1 - first point's x, y1 - first point's y, x2 - second point's x, y2 - second point's y
  167. ;Returns the distance between the two points
  168. Function Distance(x1,y1,x2,y2)
  169.  
  170. ;Find difference in x's and y's
  171. dx = x2 - x1 
  172. dy = y2 - y1 
  173.  
  174. ;return the actual distance
  175. Return Sqr((dx*dx) + (dy*dy)) 
  176. End Function
  177.  
  178.  
  179. ;FUNCTION FindRadius(imagehandle)
  180. ;imagehandle - the image whose radius you wish to find
  181. ;Returns the radius of the image
  182. Function FindRadius(imagehandle)
  183.  
  184. ;Return the radius
  185. Return ((ImageWidth(imagehandle)/2) + (ImageHeight(imagehandle)/2) / 2)
  186. End Function
  187. ;END FindRadius()