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

  1. ;demo12-03.bb - Demonstrates chasing 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 up
  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 left
  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 right
  87. If playerx > enemyx
  88.     enemyx = enemyx + ENEMYSPEED
  89. EndIf
  90.  
  91. ;if the player is below the enemy, move the enemy down
  92. If playery < enemyy
  93.  
  94.     enemyy = enemyy - ENEMYSPEED
  95. EndIf
  96.  
  97. ;draw the player and enemy on the screen
  98. DrawImage playership, playerx, playery
  99. DrawImage enemyship, enemyx, enemyy
  100.  
  101.  
  102. ;delay for a bit
  103. Delay 25
  104.  
  105. ;Flip the front and back buffer
  106. Flip
  107.  
  108. Wend
  109. ;END OF MAIN LOOP 
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.