home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter11 / demo11-01.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  5.1 KB  |  224 lines

  1. ;demo11-01.bb - Demonstrates playing a sound
  2. Graphics 800,600
  3.  
  4.  
  5. ;Initialize Back Buffer and set AutoMidHandle to True
  6. SetBuffer BackBuffer()
  7. AutoMidHandle True
  8.  
  9.  
  10. ;CONSTANTS
  11. ;The following constants are scan codes for their corresponding keys
  12. Const ESCKEY = 1, LEFTKEY = 203, RIGHTKEY = 205, UPKEY = 200, DOWNKEY = 208, SPACEBAR = 57
  13.  
  14.  
  15. ;Load the background image and set up a scrolling variable
  16. backgroundimage = LoadImage("stars.bmp")
  17. scrolly = 0
  18.  
  19. ;SOUNDS
  20. ;Load the sound that is played when the player fires a bullet
  21. Global bulletsound = LoadSound("zing.wav")
  22.  
  23. ;Load the sound that is played when the player destroys the enemy
  24. Global explosionsound = LoadSound ("explode.wav")
  25.  
  26.  
  27.  
  28. ;TYPES
  29. ;The player type is made for the player's spaceship
  30. Type player
  31.     Field x,y    ;The X and Y coordinates
  32.     Field image    ;The drawn player image
  33. End Type
  34.  
  35. ;The enemy type is the player's uhh, ship?
  36. Type enemy
  37.     Field x,y   ;The x and y coordinates
  38.     Field xv,yv   ;The x and y velocites
  39.     Field image   ;The enemy spacecraft
  40. End Type
  41.  
  42. ;The bullet type is used for each instance of the bullet
  43. Type bullet
  44.     Field x,y   ;The x and y coordinates
  45.     Field image   ;The bullet image
  46. End Type
  47.  
  48.  
  49. ;We now create the player and the enemy
  50. Global player.player = New player
  51. Global enemy.enemy = New enemy
  52.  
  53.  
  54. ;Reset the level (gives starting values to the player and enemy)
  55. ResetLevel()
  56.  
  57.  
  58.  
  59. ;MAIN LOOP
  60. While Not KeyDown (ESCKEY)
  61.  
  62. ;Draw the background of stars
  63. TileBlock backgroundimage, 0, scrolly
  64.  
  65. ;Scroll the background up
  66. scrolly = scrolly + 1
  67.  
  68. ;If the scrolling variable gets too high, reset the scrolling variable
  69. If scrolly > ImageHeight(backgroundimage)
  70.     scrolly = 0
  71. EndIf
  72.  
  73. ;Test to see what keys the player pressed
  74. TestKeys()
  75.  
  76. ;Test to see if the enemy hit a wall
  77. TestBoundaries()
  78.  
  79. ;Move and draw each bullet, and test for collision with enemy
  80. UpdateBullets()
  81.  
  82. ;Move enemy depending on their xv and yv velocities
  83. enemy\x = enemy\x + enemy\xv
  84. enemy\y = enemy\y + enemy\yv
  85.  
  86. ;Draw player and enemy
  87. DrawImage player\image, player\x, player\y
  88. DrawImage enemy\image, enemy\x, enemy\y
  89.  
  90. ;Flip the buffers
  91. Flip
  92.  
  93. Wend
  94.  
  95.  
  96. ;END OF MAIN LOOP
  97.  
  98. ;FUNCTIONS
  99.  
  100. ;Function TestKeys() - Tests the keyboard to see if the player pressed any keys
  101. Function TestKeys()
  102.  
  103. ;Create a new bullet if space bar is hit
  104. If KeyHit(SPACEBAR)
  105.     bullets.bullet = New bullet    ;Create the bullet
  106.     bullets\x = player\x   ;Assign bullet to player's x
  107.     bullets\y = player\y   ;Assign bullet to player's y
  108.     bullets\image = LoadImage("bullet.bmp")   ;Load the bullet image
  109.     
  110.     ;Play the bullet sound
  111.     PlaySound bulletsound
  112. EndIf
  113.  
  114.  
  115. ;Move player left if left arrow is hit
  116. If KeyDown (LEFTKEY)
  117.     player\x = player\x - 5
  118. EndIf
  119.  
  120. ;Move player right if right arrow is hit
  121. If KeyDown (RIGHTKEY)
  122.     player\x = player\x + 5
  123. EndIf
  124.  
  125. ;Move player up if up arrow is hit
  126. If KeyDown (UPKEY)
  127.     player\y = player\y - 5
  128. EndIf
  129.  
  130. ;Move player down if down arrow is hit
  131. If KeyDown (DOWNKEY)
  132.     player\y = player\y + 5
  133. EndIf
  134.  
  135. End Function
  136.  
  137.  
  138. ;Function TestBoundaries() - Tests enemy to see if it has hit any walls
  139.  
  140. Function TestBoundaries()
  141.  
  142. ;If enemy has gone either too left or two right, make him move the opposite direction
  143. ;For example, if the enemy has gone off screen to the right (x > 800), make him begin to move left
  144.     If enemy\x < 0 Or enemy\x > 800
  145.         enemy\xv = -enemy\xv
  146.     EndIf
  147.     
  148. ;Flip enemy in the opposite direction if he goes too far up or down
  149.     If enemy\y <0 Or enemy\y > 600
  150.         enemy\yv = -enemy\yv
  151.     EndIf
  152. End Function
  153.  
  154.  
  155. ;Function UpdateBullets() - Moves and tests each bullet for collision
  156. Function UpdateBullets()
  157.  
  158. ;Loop through every bullet
  159.     For bullets.bullet = Each bullet
  160.         
  161.         ;Update the bullet's position by moving 5 pixels up
  162.         bullets\y = bullets\y - 5
  163.         
  164. ;Draw the bullet at its proper coordinates
  165.         DrawImage bullets\image, bullets\x, bullets\y
  166.         
  167.     
  168. ;If the bullet hit the enemy, play the explosion and reset the level
  169.         If ImagesOverlap(enemy\image,enemy\x,enemy\y,bullets\image,bullets\x,bullets\y)
  170.             PlaySound explosionsound ;Play the explosion
  171.             Cls
  172.             Text 260,300, "You destroyed the enemy! How could you?"
  173.             Flip
  174.             Delay 4000
  175.             ResetLevel() ;Reset all variables of the level
  176.             Return ;Go back to main loop
  177.         EndIf
  178.  
  179. ;If the bullet goes off screen, delete it
  180.         If bullets\y < 0
  181.             Delete bullets
  182.         EndIf
  183.         
  184.         
  185.     Next  ;Move on to next bullet
  186. End Function 
  187.  
  188.  
  189. ;Function ResetLevel() - Deletes all previous bullets and resets all type variables
  190. Function ResetLevel()
  191.  
  192. ;Delete each of the bullets (if there are any)
  193. For bullets.bullet = Each bullet
  194.     Delete bullets
  195. Next
  196.     
  197.  
  198. ;Choose the player's x and y coords
  199. player\x = 400
  200. player\y = 400
  201.  
  202. ;Assign the player's image
  203. player\image = LoadImage("spaceship.bmp")
  204.  
  205.     
  206.     
  207. ;Choose the enemy's beginning x and y coords
  208. enemy\x = 400
  209. enemy\y = 200
  210.  
  211. ;Give the enemy a velocity that is between -7 and 7
  212. enemy\xv = Rand (-7,7)
  213. enemy\yv = Rand (-7,7)
  214.  
  215. ;Assign the enemy's image
  216. enemy\image = LoadImage("maneeshimage.bmp")
  217.     
  218.  
  219. ;Make sure that neither the x nor the y velocity is 0
  220. While enemy\xv = 0 Or enemy\yv = 0
  221.     enemy\xv = Rand(-7,7)
  222.     enemy\yv = Rand(-7,7)
  223. Wend
  224. End Function