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

  1. ;demo10-09.bb - A Space Simulation with MouseDown() and KeyDown()
  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.     Field bullettype ;LASER or NORMALBULLET (see constants)
  16. End Type
  17.  
  18.  
  19. ;Player type - holds the actual player
  20. Type player
  21.     Field x,y   ;the coordinates of the player
  22. End Type
  23.  
  24. ;Create player and initialize field
  25. Global player.player = New player
  26. player\x = 400
  27. player\y = 500
  28.  
  29.  
  30. ;CONSTANTS
  31. ;The following constants are used for testing key presses (mouse and keyboard)
  32. Const ESCKEY = 1, LEFTMOUSEBUTTON = 1, RIGHTMOUSEBUTTON = 2
  33. ;The following constants are used for the bullets, BULLET is a regular bullet, LASER is a laser
  34. Const NORMALBULLET = 1, LASER = 2
  35.  
  36.  
  37.  
  38. ;IMAGES
  39. playerimage = LoadImage("ship.bmp")
  40. Global bulletimage = LoadImage("bullet.bmp")
  41. Global laserimage = LoadImage("laser.bmp")
  42. backgroundimage = LoadImage("stars.bmp")
  43.  
  44. HandleImage laserimage, ImageWidth(laserimage)/2, ImageHeight(laserimage)
  45.  
  46.  
  47. ;VARIABLES
  48. ;Create a scrolling indicator variable
  49. scrolly = 0
  50.  
  51. ;Number of times left and mouse buttons were hit
  52. Global leftmouseclicks = 0
  53. Global rightmouseclicks = 0
  54.  
  55. ;MAIN LOOP
  56. While Not KeyDown(ESCKEY)
  57.     
  58. ;Increment scrolling variable
  59. scrolly = scrolly + 1
  60.  
  61. ;Tile the background
  62. TileBlock backgroundimage,0,scrolly
  63.  
  64. ;set up text
  65. Locate 0,0
  66. Print "Player X: " + MouseX()
  67. Print "Player Y: " + MouseY()
  68. Print "Number of times left mouse button was hit: " + leftmouseclicks
  69. Print "Number of times right mouse button was hit: " + rightmouseclicks
  70.  
  71. ;Reset the scrolling variable when it grows too large
  72. If scrolly > ImageHeight(backgroundimage)
  73.     scrolly = 0
  74. EndIf
  75.  
  76. ;Test mouse buttons
  77. TestMouse()
  78.  
  79. ;Update (move) each bullet
  80. UpdateBullets()
  81.  
  82. ;Draw the player
  83. DrawImage playerimage,  player\x, player\y
  84.  
  85.  
  86. ;Flip the front and back buffers
  87. Flip
  88.  
  89. Wend   ;END OF MAIN LOOP
  90.  
  91.  
  92. ;FUNCTIONS
  93. ;Function TestMouse() - Tests what mouse buttons have been pressed and where player is located
  94. Function TestMouse()
  95.  
  96.  
  97.  
  98. ;set the player at the position of the mouse
  99. player\x = MouseX()
  100. player\y = MouseY()
  101.  
  102. ;If the player hits left mouse button, create a bullet
  103. If MouseHit(LEFTMOUSEBUTTON)
  104.     bullet.bullet = New bullet    ;create bullet
  105.     bullet\x = player\x    ;place bullet at player's x coordinate
  106.     bullet\y = player\y    ;place bullet at player's y coordinate
  107.     bullet\bullettype = NORMALBULLET    ;make it a normal bullet
  108.     
  109.     ;increment left mouse clicks
  110.     leftmouseclicks = leftmouseclicks + 1
  111. EndIf 
  112.  
  113. ;If the player hits left, we will scroll the background left
  114. If MouseDown(RIGHTMOUSEBUTTON)
  115.     bullet.bullet = New bullet    ;create bullet
  116.     bullet\x = player\x    ;place bullet at player's x coordinate
  117.     bullet\y = player\y    ;place bullet at player's y coordinate
  118.     bullet\bullettype = LASER    ;make it a laser
  119.     
  120.     ;add amount of right mouse clicks since last frame
  121.     rightmouseclicks = rightmouseclicks + MouseHit(RIGHTMOUSEBUTTON)
  122. EndIf 
  123. End Function
  124.  
  125.  
  126.  
  127.  
  128. ;Function UpdateBullets() - Moves each bullet on screen
  129. Function UpdateBullets()
  130.  
  131. ;For every bullet, move it up 5 pixels. If it goes offscreen, delete it, otherwise, draw it
  132. For bullet.bullet = Each bullet
  133.     bullet\y = bullet\y - 5   ;Move bullet up
  134.     
  135.     ;If bullet moves offscreen, delete it, otherwise, draw it onscreen. Draw laserimage if it is a laser, bulletimage if it is a bullet
  136.     If bullet\y < 0
  137.         Delete bullet
  138.     ElseIf bullet\bullettype = NORMALBULLET    
  139.         DrawImage bulletimage, bullet\x, bullet\y    ;Draw the bullet
  140.     ElseIf bullet\bullettype = LASER
  141.         If player\x <> bullet\x
  142.             Delete bullet
  143.         Else
  144.             DrawImage laserimage, bullet\x, bullet\y     ;Draw the laser
  145.         EndIf
  146.     EndIf
  147.  
  148. Next  ;move to next bullet
  149.  
  150. End Function