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

  1. ;demo09-06.bb - Demonstrates ImagesOverlap()
  2. Graphics 800,600
  3.  
  4. ;Set up backbuffer, random number generator, and AutoMidHandle
  5. SetBuffer BackBuffer()
  6. SeedRnd MilliSecs()
  7. AutoMidHandle True
  8.  
  9. ;CONSTANTS
  10. ;The key code constants
  11. Const UPKEY = 200, DOWNKEY = 208, LEFTKEY = 203, RIGHTKEY = 205
  12.  
  13. ;How many pixels the ship should move per frame
  14. Const MOVEX = 5
  15. Const MOVEY = 5
  16.  
  17.  
  18. ;TYPES
  19. ;The enemy ship is a randomly moving object on the screen
  20. Type enemyship
  21.     Field x,y    ;the x and y coordinates
  22.     Field xv,yv    ;The velocity
  23.     Field image   ;the image
  24. End Type
  25.  
  26. ;The playership type defines the player
  27. Type playership
  28.     Field x,y   ;The x and y coordinate position
  29.     Field collisions    ;How many collisions have occured
  30.     Field image    ;The player's image
  31. End Type
  32.  
  33.  
  34. ;Create the enemy and assign it default variables
  35. Global enemy.enemyship = New enemyship
  36. enemy\x = 400
  37. enemy\y = 200
  38. enemy\xv = Rand(-5,5)
  39. enemy\yv = Rand(-5,5)
  40. enemy\image = LoadImage("enemyship.bmp") 
  41.  
  42. ;Create player and assign it default variables
  43. Global player.playership = New playership
  44. player\x = 400
  45. player\y = 400
  46. player\collisions = 0
  47. player\image = LoadImage("ship.bmp")
  48.  
  49.  
  50. ;MAIN LOOP
  51. While Not KeyDown(1)
  52. ;Clear the screen
  53. Cls
  54.  
  55. ;Make sure all text appears in top left corner
  56. Locate 0,20
  57. Print "Collisions: " + player\collisions
  58.  
  59. ;Find out if hit a wall
  60. TestEnemyCollisions()
  61.  
  62. ;Test keyboard
  63. TestKeys()
  64.  
  65. ;If player and enemy collide, increment collisions and reset player and enemy
  66. If (ImagesCollide(player\image,player\x,player\y,0,enemy\image,enemy\x,enemy\y,0))
  67.     player\collisions = player\collisions + 1
  68.     player\x = 400
  69.     player\y = 400
  70.     enemy\x = 400
  71.     enemy\y = 200
  72. EndIf
  73.  
  74. ;Move the enemy
  75. enemy\x = enemy\x + enemy\xv
  76. enemy\y = enemy\y + enemy\yv
  77.  
  78. ;Draw the player and the enemy
  79. DrawImage enemy\image,enemy\x,enemy\y
  80. DrawImage player\image,player\x,player\y    
  81.  
  82. Flip
  83.  
  84. Wend
  85. ;END OF MAIN LOOP
  86.  
  87. ;FUNCTION TestEnemyCollisions() - Finds out if enemy hit a wall and does something about it
  88. Function TestEnemyCollisions()
  89.  
  90. ;If enemy goes too far to the left or right, flip its x velocity
  91. If enemy\x < 0
  92.     enemy\xv = -enemy\xv
  93. EndIf
  94. If enemy\x > 800
  95.     enemy\xv = - enemy\xv
  96. EndIf
  97.  
  98. ;If enemy goes too high or too low, flip its y velocity 
  99. If enemy\y < 0 
  100.     enemy\yv = -enemy\yv
  101. EndIf
  102. If enemy\y > 600
  103.     enemy\yv = -enemy\yv
  104. EndIf
  105.  
  106. End Function 
  107. ;END OF TestEnemyCollisions()
  108.  
  109. ;FUNCTION TestKeys() - Tests all of the keys to see if they were hit
  110. ;No input parameters
  111. ;No return value
  112. Function TestKeys()
  113.  
  114. ;If up is hit, move player up
  115. If KeyDown(UPKEY) 
  116.     player\y = player\y - MOVEY 
  117. EndIf
  118.  
  119. ;If down is hit, move player down
  120. If KeyDown(DOWNKEY) ;If down was hit
  121.     player\y = player\y + MOVEY 
  122. EndIf
  123.  
  124. ;If left is hit, move player left
  125. If KeyDown(LEFTKEY) 
  126.     player\x = player\x - MOVEX 
  127. EndIf
  128.  
  129. ;If right is hit, move player right
  130. If KeyDown(RIGHTKEY) 
  131.     player\x = player\x + MOVEX
  132. EndIf
  133.  
  134. End Function
  135. ;END TestKeys()