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

  1. ;demo12-04.bb - Demonstrates evasion algorithms
  2. Graphics 800,600
  3.  
  4. ;Set up backbuffer and automidhandle
  5. SetBuffer BackBuffer()
  6. AutoMidHandle True
  7.  
  8. ;IMAGES
  9. ;player and enemies ships
  10. playership = LoadImage ("spaceship.bmp")
  11. enemyship  = LoadImage ("enemyship.bmp")
  12.  
  13. ;Load background
  14. backgroundimage = LoadImage ("stars.bmp")
  15.  
  16.  
  17. ;CONSTANTS
  18. ;The following constants are used for testing key presses
  19. Const ESCKEY = 1, UPKEY = 200, LEFTKEY = 203, RIGHTKEY = 205, DOWNKEY = 208
  20.  
  21. ;the following constants define how fast the player and the enemy move
  22. Const PLAYERSPEED = 10
  23. Const ENEMYSPEED = 5
  24.  
  25. ;position player on bottom center of screen
  26. playerx = 400
  27. playery = 400
  28.  
  29. ;positionenemy on upper center of screen
  30. enemyx = 400
  31. enemyy = 200
  32.  
  33. ;set up scrolling variable
  34. scrolly = 0
  35.  
  36. ;MAIN LOOP
  37. While Not KeyDown(ESCKEY)
  38.  
  39. ;tile the background image
  40. TileBlock backgroundimage, 0, scrolly
  41.  
  42. ;move the background up a little
  43. scrolly = scrolly + 1
  44.  
  45. ;If scrolly gets too big, reset it
  46. If scrolly > ImageHeight(backgroundimage)
  47.     scrolly = 0
  48. EndIf
  49.  
  50.  
  51.  
  52. ;Test the keypresses of the player
  53. ;If the player hits up, we move him up
  54. If KeyDown(UPKEY)
  55.     playery = playery - PLAYERSPEED 
  56. EndIf 
  57.  
  58. ;If the player hits left, we move him left
  59. If KeyDown(LEFTKEY)
  60.     playerx = playerx - PLAYERSPEED
  61. EndIf 
  62.  
  63. ;If player hits right, we move him right
  64. If KeyDown(RIGHTKEY) 
  65.     playerx = playerx + PLAYERSPEED 
  66. EndIf 
  67.  
  68. ;If player hits down, we move him down
  69. If KeyDown(DOWNKEY)
  70.     playery = playery + PLAYERSPEED
  71. EndIf 
  72.  
  73.  
  74.  
  75. ;Now, we move the enemy depending on where the player is
  76. ;If the player is above the enemy, move the enemy down
  77. If playery > enemyy
  78.     enemyy = enemyy - ENEMYSPEED
  79. EndIf
  80.  
  81. ;If the player is to the left of the enemy, move the enemy right
  82. If playerx < enemyx
  83.     enemyx = enemyx + ENEMYSPEED
  84. EndIf
  85.  
  86. ;If the player is to the right of the enemy, move the enemy left
  87. If playerx > enemyx
  88.     enemyx = enemyx - ENEMYSPEED
  89. EndIf
  90.  
  91. ;if the player is below the enemy, move the enemy up
  92. If playery < enemyy
  93.  
  94.     enemyy = enemyy + ENEMYSPEED
  95. EndIf
  96.  
  97. ;if enemy goes offscreen, move him back onscreen
  98. If enemyx <= 0 
  99.     enemyx = 0
  100. ElseIf enemyx >= 800
  101.     enemyx = 800
  102. EndIf
  103. If enemyy <= 0
  104.     enemyy = 0
  105. ElseIf enemyy >= 600
  106.     enemyy = 600
  107. EndIf
  108.  
  109.  
  110. ;draw the player and enemy on the screen
  111. DrawImage playership, playerx, playery
  112. DrawImage enemyship, enemyx, enemyy
  113.  
  114.  
  115. ;delay for a bit
  116. Delay 25
  117.  
  118. ;Flip the front and back buffer
  119. Flip
  120.  
  121. Wend
  122. ;END OF MAIN LOOP 
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.