home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter01 / demo01-01.bb < prev    next >
Encoding:
Text File  |  2004-08-28  |  4.6 KB  |  223 lines

  1. ;demo01-01.bb - A Complete game of KONG
  2.  
  3. ;Set up graphics mode
  4. Graphics 800,600 
  5.  
  6. ;Seed the random generator (make random numbers actually random)
  7. SeedRnd(MilliSecs()) 
  8.  
  9. ;Create a back buffer
  10. SetBuffer BackBuffer()
  11. ;Set the handle to the center of images
  12. AutoMidHandle True  
  13.  
  14.  
  15. ;CONSTS
  16. ;The following are key code constants
  17. Const UPKEY = 200   ;Up
  18. Const DOWNKEY = 208  ;Down
  19. Const PAUSEKEY = 25  ;P
  20. Const ESCKEY = 1     ;Escape
  21.  
  22.  
  23. Const HUMANSPEED = 7 ;The human's max speed
  24. Const COMPUTERSPEED = 6 ;The computer's max speed
  25.  
  26.  
  27. ;TYPES
  28. ;The player type: both the human and the opponent
  29. Type player 
  30.     Field y,score     ;y position and score
  31. End Type
  32.  
  33. ;The ball type: for the ball
  34. Type ball 
  35.     Field x,y,xv,yv     ;x, y coordinates, and x, y velocity
  36. End Type
  37.  
  38. ;IMAGES
  39. ;The picture of the human player
  40. Global player1image = LoadImage("player1.bmp")   
  41.  
  42. ;The picture of the computer player
  43. Global player2image = LoadImage("player2.bmp") 
  44.  
  45. ;The picture of the ball
  46. Global ballimage = LoadImage("ball.bmp") ;Load the ball image
  47.  
  48. ;TYPE INITIALIZATION
  49.  
  50. ;Create a ball
  51. Global ball.ball = New ball 
  52. ;Create the human
  53. Global player1.player = New player 
  54. ;Create the computer
  55. Global player2.player = New player 
  56.  
  57.  
  58. ;INITIALIZATION
  59.  
  60. Text 400,300,"Ready...Set"
  61. ;Wait for one second
  62. Delay(1000) 
  63. Text 420,330,"GO!!!"
  64. Flip 
  65. ;Delay for 1/5 of a second
  66. Delay(200) 
  67.  
  68. ;Initialize the level
  69. InitializeLevel() 
  70.  
  71.  
  72. ;Set inital scores
  73. player1\score = 0 
  74. player2\score = 0
  75.  
  76. ;MAIN LOOP
  77. While Not KeyDown(ESCKEY)
  78.  
  79. ;Clear the screen
  80. Cls 
  81.  
  82. ;Draw the ball
  83. DrawImage (ballimage,ball\x,ball\y) 
  84. ;Draw the human
  85. DrawImage (player1image, 60, player1\y) 
  86. ;Draw the computer
  87. DrawImage (player2image, 740, player2\y) 
  88.  
  89. ;Test what user pressed
  90. TestKeyboard() 
  91. ;What should AI do?
  92. TestAI() 
  93. ;Draw the HUD
  94. DrawScore() 
  95.  
  96. Flip
  97.  
  98. Wend ;END OF MAIN LOOP
  99.  
  100. ;FUNCTIONS
  101. ;INITIALIZELEVEL()
  102. ;Sets up starting values
  103. Function InitializeLevel()
  104.  
  105. ;Put ball in center of the screen
  106. ball\x = 400 
  107. ball\y = 300 
  108.  
  109. ;Make the ball move in a random direction
  110. ball\xv = Rand(2,6) 
  111. ball\yv = Rand(-8,8)
  112.  
  113. ;Place the players in their correct position
  114. player2\y = 300 
  115. player1\y = 300
  116. End Function
  117.  
  118.  
  119. ;DRAWSCORE()
  120. ;Draws the HUD in the top right
  121. Function DrawScore()
  122. ;Write the human score
  123. Text 700,0,"Player 1: " + player1\score 
  124. ;Write the computer's score
  125. Text 700,30,"Player 2: " + player2\score 
  126. End Function
  127.  
  128. ;TESTKEYBOARD()
  129. ;Moves player up and down based on keyboard
  130. Function TestKeyboard()
  131.  
  132. ;If player hits up, move him up
  133. If KeyDown(UPKEY)
  134.     player1\y = player1\y - HUMANSPEED 
  135. EndIf
  136.  
  137. ;If player presses down, move him down
  138. If KeyDown(DOWNKEY) 
  139.     player1\y = player1\y + HUMANSPEED 
  140. End If
  141.  
  142. ;if player presses Pause, pause the game
  143. If KeyHit(PAUSEKEY) 
  144.     ;make screen blank
  145.     Cls 
  146.     
  147.     Text 400,300,"Press 'P' to Unpause Game"
  148.     
  149.     Flip 
  150.     
  151.     ;wait for player to unpause
  152.     While Not KeyHit(PAUSEKEY) 
  153.     Wend
  154.  
  155. EndIf
  156.  
  157. End Function
  158.  
  159.  
  160.  
  161.  
  162.  
  163. ;TESTAI()
  164. ;Updates ball and score and enemy
  165. Function TestAI()
  166.  
  167. ;If ball is above computer, move computer up
  168. If ball\y > player2\y 
  169.     player2\y = player2\y + COMPUTERSPEED 
  170.  
  171. ;if ball is lower than computer, move computer down
  172. ElseIf ball\y < player2\y
  173.     player2\y = player2\y - COMPUTERSPEED 
  174.     EndIf
  175.     
  176. ;If ball hits human player, reflect it away from him and variate its velocity and direction
  177. If ImagesOverlap(ballimage,ball\x,ball\y,player1image,60,player1\y) 
  178.     ball\xv = -ball\xv + Rand(-4,4) 
  179.     ball\yv = ball\yv + Rand(-4,4)
  180.     
  181. ;If ball hits computer, reflect it away from computer and variate its velocity and direction
  182. ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y) 
  183.     ball\xv = -ball\xv + Rand(-4,4) 
  184.     ball\yv = ball\yv + Rand(-4,4)
  185.     
  186. ;If ball hits top wall, reflect it downwards
  187. ElseIf ball\y <= 0  
  188.     ball\yv = -ball\yv + Rand (-1,1) 
  189.     ball\xv = ball\xv + Rand (-1,1)  
  190.     
  191. ;If ball hits bottom wall, reflect it upwards
  192. ElseIf ball\y >= 600 
  193.     ball\yv = -ball\yv + Rand (-1,1) 
  194.     ball\xv = ball\xv + Rand (-1,1)  
  195.     
  196. ;if ball hits left wall, computer has scored so computer gets one more point
  197. ElseIf ball\x <= 0  
  198.     player2\score = player2\score + 1   ;computer scores
  199.     Text 400,300,"Player 2 Scores!!!"
  200.     Flip
  201.     ;wait two seconds
  202.     Delay(2000) 
  203.     
  204.     ;reset level
  205.     InitializeLevel() 
  206.     
  207. ;If ball hits right wall, human scored so give him a point
  208. ElseIf ball\x >= 800 
  209.     player1\score = player1\score + 1   ;human scores
  210.     Text 400,300,"Player 1 Scores!!!"
  211.     Flip 
  212.     ;wait 2 secs
  213.     Delay(2000) 
  214.     ;reset level
  215.     InitializeLevel() 
  216. EndIf
  217.     
  218.     ;update ball's position on screen
  219.     ball\x = ball\x + ball\xv 
  220.     ball\y = ball\y + ball\yv 
  221.     
  222. End Function
  223.