home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter10 / demo10-05.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  2.7 KB  |  124 lines

  1. ;demo10-05.bb - A Space Simulation with KeyHit()
  2.  
  3. Graphics 800,600
  4.  
  5. ;Set automidhandle to true
  6. AutoMidHandle True
  7. ;Set up Backbuffer
  8. SetBuffer BackBuffer()
  9.  
  10. ;TYPES
  11.  
  12. ;Bullet type = hold the information for each bullet
  13. Type bullet
  14.     Field x,y   ;the coordinates of the bullet
  15. End Type
  16.  
  17.  
  18. ;Player type - holds the actual player
  19. Type player
  20.     Field x,y   ;the coordinates of the player
  21. End Type
  22.  
  23. ;Create player and initialize field
  24. Global player.player = New player
  25. player\x = 400
  26. player\y = 500
  27.  
  28.  
  29. ;CONSTANTS
  30. ;The following constants are used for testing key presses
  31. Const ESCKEY = 1, UPKEY = 200, LEFTKEY = 203, RIGHTKEY = 205, DOWNKEY = 208, SPACEBAR = 57
  32.  
  33.  
  34.  
  35. ;IMAGES
  36. playerimage = LoadImage("ship.bmp")
  37. Global bulletimage = LoadImage("bullet.bmp")
  38. backgroundimage = LoadImage("stars.bmp")
  39.  
  40. ;Create a scrolling indicator variable
  41. scrolly = 0
  42.  
  43. ;MAIN LOOP
  44. While Not KeyDown(ESCKEY)
  45.     
  46. ;Increment scrolling variable
  47. scrolly = scrolly + 1
  48.  
  49. ;Tile the background
  50. TileBlock backgroundimage,0,scrolly
  51.  
  52. ;Reset the scrolling variable when it grows too large
  53. If scrolly > ImageHeight(backgroundimage)
  54.     scrolly = 0
  55. EndIf
  56.  
  57. ;Test input keys
  58. TestKeys()
  59.  
  60. ;Update (move) each bullet
  61. UpdateBullets()
  62.  
  63. ;Draw the player
  64. DrawImage playerimage,  player\x, player\y
  65.  
  66.  
  67. ;Flip the front and back buffers
  68. Flip
  69.  
  70. Wend   ;END OF MAIN LOOP
  71.  
  72.  
  73. ;FUNCTIONS
  74. ;Function TestKeys() - Tests what buttons have been pressed by player
  75. Function TestKeys()
  76.  
  77. ;If the player hits up, we move him 5 pixels up
  78. If KeyDown(UPKEY)
  79.     player\y = player\y - 5 ;move player 5 pixels up
  80. EndIf 
  81.  
  82. ;If the player hits left, we move him 5 pixels left
  83. If KeyDown(LEFTKEY)
  84.     player\x = player\x - 5 ;move player 5 pixels left
  85. EndIf 
  86.  
  87. ;If player hits right, we move him 5 pixels right
  88. If KeyDown(RIGHTKEY) 
  89.     player\x = player\x + 5 ;move player 5 pixels right
  90. EndIf 
  91.  
  92. ;If player hits down, we move him 5 pixels down
  93. If KeyDown(DOWNKEY)
  94.     player\y = player\y + 5 ;move player 5 pixels down
  95. EndIf 
  96.  
  97.  
  98.  
  99. ;If player hits spacebar, we will create a new bullet at the player's current position
  100. If KeyHit(SPACEBAR)
  101.     bullet.bullet = New bullet
  102.     bullet\x = player\x
  103.     bullet\y = player\y
  104. EndIf
  105.  
  106. End Function
  107.  
  108. ;Function UpdateBullets() - Moves each bullet on screen
  109. Function UpdateBullets()
  110.  
  111. ;For every bullet, move it up 5 pixels. If it goes offscreen, delete it, otherwise, draw it
  112. For bullet.bullet = Each bullet
  113.     bullet\y = bullet\y - 5   ;Move bullet up
  114.     
  115.     ;If bullet moves offscreen, delete it, otherwise, draw it onscreen
  116.     If bullet\y < 0
  117.         Delete bullet
  118.     Else    
  119.         DrawImage bulletimage, bullet\x, bullet\y    ;Draw the bullet
  120.     EndIf
  121.  
  122. Next  ;move to next bullet
  123.  
  124. End Function