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

  1. ;demo09-05.bb - Demonstrates ImagesOverlap()
  2. Graphics 800,600
  3.  
  4. ;Set up backbuffer, random number generator, and automidhanel
  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
  22.     Field xv,yv
  23.     Field image
  24. End Type
  25.  
  26. ;The playership defines the player
  27. Type playership
  28.     Field x,y
  29.     Field collisions
  30.     Field image
  31. End Type
  32.  
  33. ;Create the enemy and assign it default and random variables
  34. Global enemy.enemyship = New enemyship
  35. enemy\x = 400
  36. enemy\y = 200
  37. enemy\xv = Rand(-5,5)
  38. enemy\yv = Rand(-5,5)
  39. enemy\image = LoadImage("enemyship.bmp") 
  40.  
  41. ;Create the player and assign default values
  42. Global player.playership = New playership
  43. player\x = 400
  44. player\y = 400
  45. player\collisions = 0
  46. player\image = LoadImage("ship.bmp")
  47.  
  48.  
  49. ;MAIN LOOP
  50. While Not KeyDown(1)
  51. Cls
  52.  
  53. ;Make sure all text appears in top left corner
  54. Locate 0,20
  55. Print "Collisions: " + player\collisions
  56.  
  57. ;Find out if enemy hit a wall
  58. TestEnemyCollisions()
  59.  
  60. ;Test keyboard
  61. TestKeys()
  62.  
  63. ;If player and enemy overlap, increment collisions and reset player and enemy
  64. If (ImagesOverlap(player\image,player\x,player\y,enemy\image,enemy\x,enemy\y))
  65.         player\collisions = player\collisions + 1
  66.         player\x = 400
  67.         player\y = 400
  68.         enemy\x = 400
  69.         enemy\y = 200
  70. EndIf
  71.  
  72. ;Move the enemy
  73. enemy\x = enemy\x + enemy\xv
  74. enemy\y = enemy\y + enemy\yv
  75.  
  76. ;Draw the player and the enemy
  77. DrawImage enemy\image,enemy\x,enemy\y
  78. DrawImage player\image,player\x,player\y    
  79.  
  80. Flip
  81.  
  82. Wend
  83.  
  84.  
  85. ;FUNCTION TestEnemyCollisions() - Tests to see if enemy hit a wall
  86. ;No input parameters
  87. ;No return values
  88. Function TestEnemyCollisions()
  89.  
  90. ;If enemy hits a wall, reverse his 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. If enemy\y < 0 
  98.     enemy\yv = -enemy\yv
  99. EndIf
  100. If enemy\y > 600
  101.     enemy\yv = -enemy\yv
  102. EndIf
  103. End Function 
  104.  
  105.  
  106. ;FUNCTION TestKeys() - Tests all of the keys to see if they were hit
  107. ;No input parameters
  108. ;No return value
  109. Function TestKeys()
  110.  
  111. ;If up is hit, move player up
  112. If KeyDown(UPKEY) 
  113.     player\y = player\y - MOVEY 
  114. EndIf
  115.  
  116. ;If down is hit, move player down
  117. If KeyDown(DOWNKEY) ;If down was hit
  118.     player\y = player\y + MOVEY 
  119. EndIf
  120.  
  121. ;If left is hit, move player left
  122. If KeyDown(LEFTKEY) 
  123.     player\x = player\x - MOVEX 
  124. EndIf
  125.  
  126. ;If right is hit, move player right
  127. If KeyDown(RIGHTKEY) 
  128.     player\x = player\x + MOVEX
  129. EndIf
  130.  
  131. End Function
  132. ;END TestKeys()