home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming for Teens (2nd Edition) / 3DGPFT2E.iso / Source / Chapter03 / demo03-11.bb < prev    next >
Text File  |  2009-01-20  |  2KB  |  106 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.     Flip
  39. Wend
  40. ;End of loop
  41.     
  42.     
  43.     
  44. ;TestInput() changes the direction or hitpoints of the player
  45. Function TestInput()
  46.  
  47. ;If player presses left, move him left.
  48. If KeyHit(LEFTKEY)
  49.  
  50.     player\x = player\x - 3
  51.     If player\x <= 0
  52.         player\x = 10
  53.     EndIf
  54. EndIf
  55.  
  56. ;If player presses right, move him right.
  57. If KeyHit(RIGHTKEY)
  58.     player\x = player\x + 3
  59.  
  60.     If player\x >= 385
  61.         player\x = 380
  62.     EndIf
  63. EndIf
  64.  
  65. ;If player presses up, move him up.
  66. If KeyHit(UPKEY)
  67.  
  68.     player\y = player\y - 3
  69.     If player\y <= 0
  70.         player\y = 10
  71.     EndIf
  72. EndIf
  73.  
  74. ;If player presses down, move him down.
  75. If KeyHit(DOWNKEY)
  76.  
  77.     player\y = player\y + 3
  78.     If player\y >= 285
  79.         player\y = 280
  80.     EndIf
  81. EndIf
  82.  
  83. ;If player presses space bar, remove a hitpoint
  84.  
  85. If KeyHit(SPACEBAR)
  86.  
  87.     player\hitpoints = player\hitpoints - 1
  88.     If player\hitpoints <= 0
  89.         cont = 0
  90.     EndIf
  91.  
  92. EndIf
  93.  
  94. ;If player presses Esc, set cont to 0, and exit the game
  95. If KeyHit(ESCKEY)
  96.     cont = 0
  97. EndIf
  98.  
  99. End Function
  100.  
  101. ;DrawHUD() draws user's info in top Right of screen
  102. Function DrawHUD()
  103.     Text 260, 10, "X position: " + player\x
  104.     Text 260, 20, "Y position: " + player\y
  105.     Text 260, 30, "Hitpoints:  " + player\hitpoints
  106. End Function