home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter03 / demo03-11.bb < prev    next >
Encoding:
Text File  |  2004-08-28  |  2.0 KB  |  105 lines

  1. ;demo03-11.bb - Draws a ship which can be moved and killed
  2.  
  3. Graphics 400,300
  4.  
  5. ;CONSTANTS
  6. ;starting hitpoints, feel free To change
  7. Const STARTHITPOINTS = 3 ;
  8. ;What the ship looks like
  9. Const SHIP$ = "<-*->" 
  10. ;the keycodes
  11. Const ESCKEY = 1, SPACEBAR = 57, UPKEY = 200, LEFTKEY = 203, DOWNKEY = 208, RIGHTKEY = 205 
  12. ; Where the player starts
  13. Const STARTX = 200, STARTY = 150
  14.  
  15.  
  16. ;TYPES
  17. Type Ship
  18.     Field x,y ;the position of the ship
  19.     Field hitpoints ;how many hits left
  20.     Field shipstring$ ;what the ship looks like
  21. End Type
  22.  
  23. ;INITIALZATION SECTION
  24. Global cont = 1 ;continue?
  25. Global player.ship = New ship
  26. player\x = STARTX
  27. player\y = STARTY
  28. player\hitpoints = STARTHITPOINTS
  29. player\shipstring = SHIP$
  30.  
  31. ;Game loop
  32. While cont = 1
  33.     Cls
  34.     Text player\x, player\y, player\shipstring$
  35.     
  36.     TestInput()
  37.     DrawHUD()
  38. Wend
  39. ;End of loop
  40.     
  41.     
  42.     
  43. ;TestInput() changes the direction or hitpoints of the player
  44. Function TestInput()
  45.  
  46. ;If player presses left, move him left.
  47. If KeyHit(LEFTKEY)
  48.  
  49.     player\x = player\x - 3
  50.     If player\x <= 0
  51.         player\x = 10
  52.     EndIf
  53. EndIf
  54.  
  55. ;If player presses right, move him right.
  56. If KeyHit(RIGHTKEY)
  57.     player\x = player\x + 3
  58.  
  59.     If player\x >= 385
  60.         player\x = 380
  61.     EndIf
  62. EndIf
  63.  
  64. ;If player presses up, move him up.
  65. If KeyHit(UPKEY)
  66.  
  67.     player\y = player\y - 3
  68.     If player\y <= 0
  69.         player\y = 10
  70.     EndIf
  71. EndIf
  72.  
  73. ;If player presses down, move him down.
  74. If KeyHit(DOWNKEY)
  75.  
  76.     player\y = player\y + 3
  77.     If player\y >= 285
  78.         player\y = 280
  79.     EndIf
  80. EndIf
  81.  
  82. ;If player presses space bar, remove a hitpoint
  83.  
  84. If KeyHit(SPACEBAR)
  85.  
  86.     player\hitpoints = player\hitpoints - 1
  87.     If player\hitpoints <= 0
  88.         cont = 0
  89.     EndIf
  90.  
  91. EndIf
  92.  
  93. ;If player presses Esc, set cont to 0, and exit the game
  94. If KeyHit(ESCKEY)
  95.     cont = 0
  96. EndIf
  97.  
  98. End Function
  99.  
  100. ;DrawHUD() draws user's info in top Right of screen
  101. Function DrawHUD()
  102.     Text 260, 10, "X position: " + player\x
  103.     Text 260, 20, "Y position: " + player\y
  104.     Text 260, 30, "Hitpoints:  " + player\hitpoints
  105. End Function