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

  1. ;demo11-04.bb - Demonstrates SoundPan
  2.  
  3. Graphics 800,600
  4.  
  5. ;Set up backbuffer and AutoMidHandle
  6. SetBuffer BackBuffer()
  7. AutoMidHandle True
  8.  
  9. ;TYPES
  10. ;This type is used for the enemy. He only moves left and right, so there is no need for a y velocity variable
  11. Type enemy
  12.     Field x,y   ;what is enemy's x coord?
  13.     Field xv   ;How fast is he moving?
  14.     Field image    ;The enemy's image
  15. End Type
  16.  
  17. ;This type is used for the player
  18. Type player
  19.     Field x, y   ;The player's coordinate position
  20.     Field image   ;The player's image
  21. End Type
  22.  
  23. ;Create player and enemy
  24. player.player = New player
  25. enemy.enemy = New enemy
  26.  
  27. ;Set up player's beginning values
  28. player\x = 400
  29. player\y = 400
  30. player\image = LoadImage("spaceship.bmp")
  31.  
  32. ;Set up enemy's beginning values
  33. enemy\x = 400
  34. enemy\y = 200
  35. enemy\xv = 5    ;Move the player 5 pixels left/right each frame
  36. enemy\image = LoadImage("enemyship.bmp")
  37.  
  38. ;SOUNDS
  39. ;This is the bullet sound
  40. bulletsound = LoadSound("zing.wav")
  41.  
  42. ;CONSTANTS
  43. ;These constants refer to the key codes
  44. Const ESCKEY = 1, SPACEBAR = 57
  45.  
  46. ;IMAGES
  47. ;The scrolling background image
  48. backgroundimage = LoadImage("stars.bmp")
  49.  
  50. ;Set up scrolling variable
  51. scrolly = 0
  52.  
  53. ;Set up panning indicator
  54. pan# = 0
  55.  
  56.  
  57. ;MAIN LOOP
  58. While Not KeyDown(ESCKEY)
  59.  
  60.  
  61. ;Tile the background
  62.  
  63. TileBlock backgroundimage, 0, scrolly
  64. ;increment the scrolling variable
  65. scrolly = scrolly + 1
  66.  
  67. If scrolly > ImageHeight(backgroundimage)
  68.     scrolly = 0
  69. EndIf
  70.  
  71. ;Print all text at top-left corner
  72. Locate 0,0
  73. Print "Panning variable: " + pan#
  74.  
  75. ;set up player coordinates    
  76. player\x = MouseX()
  77. player\y = MouseY()
  78.  
  79.  
  80.  
  81.  
  82. ;if enemy is to the left of player, make sound come out of left speaker
  83. If enemy\x < player\x
  84.     pan# = -1.000
  85.     ;If enemy is to right of player, make sound come out of right speaker
  86. ElseIf enemy\x > player\x
  87.     pan# = 1.000
  88.     ;If enemy is in front of player
  89. Else
  90.     pan# = 0
  91. EndIf
  92.  
  93. ;Pan the sound
  94. SoundPan bulletsound, pan#
  95.  
  96.  
  97. ;If player presses spacebar, play the sound
  98. If KeyHit (SPACEBAR)    
  99.  
  100.  
  101.     PlaySound bulletsound
  102. EndIf
  103.  
  104. ;Move the enemy according to his velocity
  105. enemy\x = enemy\x + enemy\xv
  106.  
  107. ;If the enemy goes offscreen, reflect his velocity
  108. If enemy\x < 0 Or enemy\x > 800
  109.     enemy\xv = - enemy\xv
  110. EndIf 
  111.  
  112. ;Draw the player and the enemy
  113. DrawImage player\image,player\x,player\y
  114. DrawImage enemy\image,enemy\x,enemy\y
  115.  
  116. Flip
  117.  
  118.  
  119. Wend