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

  1. ;demo11-03.bb - Demonstrates SoundVolume
  2.  
  3. Graphics 800,600
  4.  
  5. ;Create Backbuffer() and set up AutoMidHandle
  6. SetBuffer BackBuffer()
  7. AutoMidHandle True
  8.  
  9. ;TYPES
  10. ;This type is used for the ship that controls the player
  11. Type player
  12.     Field x,y    ;The player's x and y coordinates
  13.     Field image    ;The image of the player's ship
  14. End Type
  15.  
  16. ;This type is used for the enemy
  17. Type enemy
  18.     Field x,y     ;The enemy's coordinate position
  19.     Field xv,yv   ;The enemy's velocity
  20.     Field image   ;The image of the enemy's ship
  21. End Type
  22.  
  23. ;Create the player and the enemy
  24. Global player.player = New player
  25. Global enemy.enemy = New enemy
  26.  
  27. ;Choose the player's x and y coords
  28. player\x = 400
  29. player\y = 400
  30.  
  31. ;Assign the player's image
  32. player\image = LoadImage("spaceship.bmp")
  33.  
  34. ;Choose the enemy's beginning x and y coords
  35. enemy\x = 400
  36. enemy\y = 200
  37.  
  38. ;Give the enemy a velocity that is between -7 and 7
  39. enemy\xv = Rand (-7,7)
  40. enemy\yv = Rand (-7,7)
  41.  
  42. ;Assign the enemy's image
  43. enemy\image = LoadImage("enemyship.bmp")
  44.     
  45.  
  46. ;Make sure that neither the x nor the y velocity is 0
  47. While enemy\xv = 0 Or enemy\yv = 0
  48.     enemy\xv = Rand(-7,7)
  49.     enemy\yv = Rand(-7,7)
  50. Wend
  51.  
  52.  
  53. ;CONSTANTS
  54. ;The following constants are used for key codes
  55. Const ESCKEY = 1,SPACEBAR = 57, UPKEY = 200, DOWNKEY = 208
  56.  
  57. ;SOUNDS
  58. ;The following sound is produced each time the player hits space bar
  59. Global bulletsound = LoadSound("zing.wav")
  60.  
  61. ;Create a volume tracker variable and set it to half volume (.5)
  62. Global volume# = .5
  63.  
  64. ;Synchronize the sound with the volume
  65. SoundVolume bulletsound, volume#
  66.  
  67. ;Create a distance variable
  68. Global dist
  69.  
  70. ;MAIN LOOP
  71. While Not KeyDown(ESCKEY)
  72. ;Clear the screen
  73. Cls
  74.  
  75. ;Make sure print commands occur in top left hand corner
  76. Locate 0,0
  77.  
  78. Print "Current Volume (between 0 and 1.000): " + volume#
  79. Print "Distance between enemy and player (in pixels) at time of last bullet: " + dist
  80.  
  81.  
  82. ;Test if any keys have been pressed
  83. TestKeys()
  84.  
  85. ;Check to see if enemy hit any walls
  86. TestBoundaries()
  87.  
  88. ;Move the enemy according to his velocity
  89. enemy\x = enemy\x + enemy\xv
  90. enemy\y = enemy\y + enemy\yv
  91.  
  92. ;Set player\x to the current mouve position
  93. player\x = MouseX()
  94. player\y = MouseY()
  95.  
  96. ;Draw the player and enemy images
  97. DrawImage player\image,player\x,player\y
  98. DrawImage enemy\image,enemy\x,enemy\y
  99.  
  100. Flip
  101. Wend
  102. ;END OF MAIN LOOP
  103.  
  104.  
  105. ;FUNCTIONS
  106. ;Function TestKeys() - Tests the keyboard to see what has been pressed
  107. Function TestKeys()
  108.  
  109.  
  110. ;Create a new bullet if spacebar is hit
  111. If KeyHit(SPACEBAR)
  112.     
  113.     ;Find what volume# should be
  114.     FindCorrectVolume()
  115.     
  116.     ;Assign bulletsound to volume#
  117.     SoundVolume bulletsound, volume#
  118.     
  119.     ;Play the bullet
  120.     PlaySound bulletsound
  121. EndIf
  122.  
  123.  
  124. ;Move player left if left arrow is hit
  125. If KeyDown (LEFTKEY)
  126.     player\x = player\x - 5
  127. EndIf
  128.  
  129. ;Move player right if right arrow is hit
  130. If KeyDown (RIGHTKEY)
  131.     player\x = player\x + 5
  132. EndIf
  133.  
  134. ;Move player up if up arrow is hit
  135. If KeyDown (UPKEY)
  136.     player\y = player\y - 5
  137. EndIf
  138.  
  139. ;Move player down if down arrow is hit
  140. If KeyDown (DOWNKEY)
  141.     player\y = player\y + 5
  142. EndIf
  143.  
  144. End Function
  145.  
  146.  
  147. ;Function FindCorrectVolume - Sets volume# to the correct value depending on distance from player to enemy
  148. Function FindCorrectVolume()
  149.  
  150. ;Find distance between player and enemy
  151. dist = Distance(player\x,player\y,enemy\x,enemy\y)
  152.  
  153. ;Assign the volume number to volume# depending on how far the distance is. The farther the distance, the quieter the sound
  154. If dist < 100
  155.     volume# = 1.000
  156. ElseIf dist < 200
  157.     volume# = .700
  158. ElseIf dist < 300
  159.     volume# = .400
  160. ElseIf dist < 400
  161.     volume# = .1000
  162. Else
  163.     volume# = 0.000
  164. EndIf
  165.  
  166.  
  167.  
  168. End Function
  169. ;Function Distance()
  170. ;Returns the distance between two points
  171. ;x1: x coord of first point
  172. ;y1: y coord of first point
  173. ;x2: x coord of second point
  174. ;y2: y coord of second point
  175. Function Distance(x1,y1,x2,y2)
  176.  
  177. ;Find difference between x's and y's
  178. dx = (x2 - x1)
  179. dy = (y2 - y1)
  180.  
  181. ;Find the actual distance using the distance formula
  182. dist = Sqr( (dx * dx) + (dy * dy) )
  183.  
  184.  
  185. ;Send the result back to the calling function
  186. Return dist
  187.  
  188. End Function
  189.  
  190.  
  191. ;Function TestBoundaries() - Tests enemy to see if it has hit any walls
  192.  
  193. Function TestBoundaries()
  194.  
  195. ;If enemy has gone either too left or two right, make him move the opposite direction
  196. ;For example, if the enemy has gone off screen to the right (x > 800), make him begin to move left
  197.     If enemy\x < 0 Or enemy\x > 800
  198.         enemy\xv = -enemy\xv
  199.     EndIf
  200.     
  201. ;Flip enemy in the opposite direction if he goes too far up or down
  202.     If enemy\y <0 Or enemy\y > 600
  203.         enemy\yv = -enemy\yv
  204.     EndIf
  205. End Function