home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter07 / demo07-15.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  4.7 KB  |  180 lines

  1. ;KONG
  2. ;Developed by Maneesh Sethi
  3. ;11/2002
  4.  
  5.  
  6. Graphics 800,600 ;Set up graphics mode
  7.  
  8. AppTitle "KONG" ;Make the program name "KONG"
  9.  
  10.  
  11. SeedRnd(MilliSecs()) ;Seed the random generator (make random numbers actually random)
  12.  
  13.  
  14. SetBuffer BackBuffer() ;Create a back buffer)
  15.  
  16.  
  17.  
  18. ;CONSTS
  19. Const UPKEY = 200  ;Up
  20. Const DOWNKEY = 208 ;Down
  21. Const PAUSEKEY = 25 ;P
  22. Const HUMANSPEED = 7 ;The human's max speed
  23. Const COMPUTERSPEED = 6 ;The computer's max speed
  24.  
  25.  
  26. ;TYPES
  27. Type player ;Each player's type
  28.     Field y,score ;y position and score
  29. End Type
  30.  
  31.  
  32. Type ball ;ball's type
  33.     Field x,y,xv,yv ;x, y coordinates, and x, y velocity
  34. End Type
  35.  
  36. ;IMAGES
  37.  
  38. Global player1image = LoadImage("player1.bmp") ;Load the human image
  39. Global player2image = LoadImage("player2.bmp") ;Load the computer image
  40. Global ballimage = LoadImage("ball.bmp") ;Load the ball image
  41. Global backgroundimageclose = LoadImage("stars.bmp") ;Load the close
  42. Global backgroundimagefar = LoadImage("starsfarther.bmp") ;Load the far stars
  43.  
  44. ;TYPE INITIALIZATION
  45. Global ball.ball = New ball ;Create a ball
  46. Global player1.player = New player ;Create the human
  47. Global player2.player = New player ;Create the computer
  48.  
  49.  
  50. ;INITIALIZATION
  51.  
  52. Text 400,300,"Ready...Set"
  53. Delay(1000) ;Wait for one second
  54. Text 420,330,"GO!!!"
  55. Flip ;Show GO!!! on the screen
  56.     Delay(200) ;Delay for 1/5 of a second
  57. scrolly = 0
  58.  
  59.  
  60. InitializeLevel() ;Call InitializeLevel()
  61.  
  62. AutoMidHandle True ;Set the handle to the center of images
  63. player1\score = 0 ;Set init scores
  64. player2\score = 0
  65.  
  66. ;MAIN LOOP
  67. While Not KeyDown(1)
  68.  
  69. Cls ;Clear the screen
  70.  
  71. TileImage backgroundimagefar,0,scrolly
  72. TileImage backgroundimageclose,0,scrolly*2
  73.  
  74. scrolly=scrolly+1
  75. If scrolly >= ImageHeight(backgroundimageclose)
  76.     scrolly = 0
  77. EndIf
  78.  
  79.  
  80. TestKeyboard() ;Test what user pressed
  81. TestAI() ;What should AI do?
  82. DrawScore() ;Draw the HUD
  83.  
  84. DrawImage (ballimage,ball\x,ball\y) ;Draw the ball
  85. DrawImage (player1image, 60, player1\y) ;Draw the human
  86. DrawImage (player2image, 740, player2\y) ;Draw the computer
  87.  
  88. Flip
  89.  
  90. Wend ;END OF MAIN LOOP
  91.  
  92.  
  93.  
  94.  
  95. ;FUNCTIONS
  96. ;INITIALIZELEVEL()
  97. ;Sets up starting values
  98. Function InitializeLevel()
  99. ball\x = 400 ;Ball is in center of screen
  100. ball\y = 300 ;Ball is in center of screen
  101. ball\xv = Rand(2,6) ;Ball is moving to the right between 2 and 6
  102. ball\yv = Rand(-8,8) ;Ball is moving up and down between -8 and 8
  103.  
  104. player2\y = 300 ;Put players in center of screen
  105. player1\y = 300
  106. End Function
  107.  
  108.  
  109. ;DRAWSCORE()
  110. ;Draws the HUD in the top right
  111. Function DrawScore()
  112. Text 700,0,"Player 1: " + player1\score ;Write the human score
  113. Text 700,30,"Player 2: " + player2\score ;Write the computer's score
  114. End Function
  115.  
  116. ;TESTKEYBOARD()
  117. ;Moves player up and down based on keyboard
  118. Function TestKeyboard()
  119.  
  120. If KeyDown(UPKEY) ;player hits up
  121.     player1\y = player1\y - HUMANSPEED ;Move him up
  122. EndIf
  123.  
  124. If KeyDown(DOWNKEY) ;player hits down
  125.     player1\y = player1\y + HUMANSPEED ;move player down
  126. End If
  127.  
  128. If KeyHit(PAUSEKEY) ;player hits 'p'
  129.     Cls ;make screen blank
  130.     Text 400,300,"Press 'P' to Unpause Game"
  131.     Flip ;show text
  132.     While Not KeyHit(PAUSEKEY) ;wait for player to unpause
  133.     Wend
  134. EndIf
  135.  
  136. End Function
  137.  
  138.  
  139.  
  140.  
  141.  
  142. ;TESTAI()
  143. ;Updates ball and score and enemy
  144. Function TestAI()
  145. If ball\y > player2\y ;If ball is higher than computer
  146.     player2\y = player2\y + COMPUTERSPEED ;move computer up
  147. ElseIf ball\y < player2\y;if ball is lower than computer
  148.     player2\y = player2\y - COMPUTERSPEED ;move computer down
  149.     EndIf
  150. If ImagesOverlap(ballimage,ball\x,ball\y,player1image,60,player1\y) ;if ball hits player
  151.     ball\xv = -ball\xv + Rand(-4,4) ;reflect it towards player
  152.     ball\yv = ball\yv + Rand(-4,4) ;keep speed with a little variation
  153. ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y) ;if ball hits computer
  154.     ball\xv = -ball\xv + Rand(-4,4) ;reflect towards human
  155.     ball\yv = ball\yv + Rand(-4,4) ;keep speed with a little variation
  156. ElseIf ball\y <= 0  ;if ball hits top wall
  157.     ball\yv = -ball\yv + Rand (-1,1) ;reflect downwards
  158.     ball\xv = ball\xv + Rand (-1,1)  ;change slope a little
  159. ElseIf ball\y >= 600 ;if ball hits bottom wall
  160.     ball\yv = -ball\yv + Rand (-1,1) ;reflect upwards
  161.     ball\xv = ball\xv + Rand (-1,1)  ;change slope a little
  162. ElseIf ball\x <= 0  ;if ball hit's human wall
  163.     player2\score = player2\score + 1 ;computer scores
  164.     Text 400,300,"Player 2 Scores!!!"
  165.     Flip ;show text
  166.     Delay(2000) ;wait two seconds
  167.     InitializeLevel() ;reset level
  168. ElseIf ball\x >= 800 ;if human scores
  169.     player1\score = player1\score + 1 ;human scores
  170.     Text 400,300,"Player 1 Scores!!!"
  171.     Flip ;show text
  172.     Delay(2000) ;wait 2 secs
  173.     InitializeLevel() ;reset level
  174. EndIf
  175.     
  176.     ball\x = ball\x + ball\xv ;move ball a little right and left
  177.     ball\y = ball\y + ball\yv ;move ball a little up and down
  178.  
  179. End Function
  180.