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

  1. ;demo03-12.bb - A textdrawn arkanoid
  2. Graphics 640,480
  3. SetBuffer BackBuffer()
  4.  
  5. ;TYPES
  6. ;the player type
  7. Type paddle 
  8.     Field x,y   ;coordinates
  9. End Type
  10.  
  11. ;The ball type
  12. Type ball
  13.     Field x,y   ;the coordinate
  14.     Field directionx, directiony  ;the velocities of the ball
  15. End Type
  16.     
  17.  
  18. ;Constants
  19. ;What the blocks look like
  20. Const BLOCKSTRING$ = "XXXXXXX"
  21. ;What the paddle looks like
  22. Const PADDLESTRING$ = "---------"
  23. ;What the ball looks like
  24. Const BALLSTRING$ = "O"
  25. ;How many rows of blocks there are
  26. Const BLOCKROWS = 3
  27. ;How many columns of blocks there are
  28. Const BLOCKCOLUMNS = 6
  29. ;The number of pixels between each column
  30. Const BLOCKXGAP = 85
  31. ;The number of pixels between each row
  32. Const BLOCKYGAP = 32
  33. ;The number of pixels from the top left corner the first column is
  34. Const BLOCKXORIGIN = 16
  35. ;The number of pixels from the top left corner the first row is
  36. Const BLOCKYORIGIN = 8
  37. ;The height of each block
  38. Global BLOCKHEIGHT = FontHeight()
  39. ;The length of each block
  40. Global BLOCKWIDTH = Len(BLOCKSTRING$) * FontWidth()
  41. ;The width of each paddle
  42. Global PADDLEWIDTH = Len(PADDLESTRING$) * FontWidth()
  43. ;The height of each paddle
  44. Global PADDLEHEIGHT = FontHeight()
  45. ;The width of the ball
  46. Global BALLWIDTH = Len(BALLSTRING$) * FontWidth()
  47. ;The height of the ball
  48. Global BALLHEIGHT = FontHeight()
  49. ;The starting X coordinate for the player
  50. Const STARTX = 300
  51. ;The starting Y coordinate for the player
  52. Const STARTY= 340
  53. ;The Key code constants
  54. Const ESCKEY = 1, LEFTKEY = 203, RIGHTKEY = 205 
  55.  
  56.  
  57.  
  58. ;Initialization
  59. ;Seed the random Generator
  60. SeedRnd MilliSecs()
  61. ;Initialize the score
  62. Global score = 0
  63. ;The number of total block hits
  64. Global blockhits = 0 
  65. ;The level the player is on
  66. Global level = 1
  67.  
  68. ;Creat an array of blocks
  69. Dim blocks(BLOCKROWS, BLOCKCOLUMNS)
  70.  
  71. ;Create a new ball
  72. Global ball.ball = New ball
  73. ;Create a new paddle
  74. Global player.paddle = New paddle
  75.  
  76. ;Initialize the new level
  77. NewLevel()
  78.  
  79.  
  80.  
  81.  
  82.  
  83. ;Game Loop
  84. While Not KeyDown(1)
  85. Cls
  86.  
  87. DrawHUD()
  88. TestInput()
  89. DrawBlocks()
  90. DrawPaddle()
  91. CheckBall()
  92.  
  93.  
  94. Flip
  95. Wend
  96.  
  97. Function DrawBlocks()
  98.     
  99.     x = BLOCKXORIGIN
  100.     y = BLOCKYORIGIN
  101. ;This variable creates a new level if there are no blocks
  102.     newlevel = 0 
  103.     
  104. ;FOr all the rows
  105.     For rows = 0 To BLOCKROWS - 1
  106. ;reset rows position
  107.         x = BLOCKXORIGIN 
  108.         
  109.         For cols = 0 To BLOCKCOLUMNS - 1
  110.  
  111.             ;If the block exists, draw it on screen
  112.             If (blocks(rows,cols) = 1) Then
  113.                 Text x,y, BLOCKSTRING$
  114.                 newlevel = newlevel + 1
  115.             EndIf
  116.         ;Move over to the next block
  117.         x = x + BLOCKXGAP
  118.         
  119.         Next
  120.         ;Move to the next column
  121.         y = y + BLOCKYGAP
  122.     Next
  123.     If newlevel = 0
  124.         level = level + 1
  125.         NewLevel()
  126.     EndIf
  127.         
  128. End Function
  129.  
  130. Function CheckBall()
  131.  
  132. ;Move and draw ball 
  133. UpdateBall() 
  134.  
  135. ;Check and see if ball hit anything
  136. CheckBallWithPaddle()
  137. CheckBallWithBlocks()
  138. CheckBallWithWalls()
  139. End Function
  140.             
  141.             
  142. Function UpdateBall()
  143.  
  144. ;Move the ball to the left or right
  145. ball\x = ball\x + ball\directionx 
  146.  
  147. ;Move the ball up or down
  148. ball\y = ball\y + ball\directiony 
  149.  
  150. ;Draw the ball
  151. Text ball\x, ball\y, BALLSTRING$  
  152. End Function
  153.  
  154.  
  155.  
  156. Function DrawPaddle()
  157. ;Draw the paddle
  158. Text player\x,player\y,PADDLESTRING$
  159. End Function
  160.  
  161. Function CheckBallWithPaddle()
  162. ;If the ball hits a paddle, revers its direction
  163. If ball\x >= player\x And ball\x <= player\x + PADDLEWIDTH And ball\y + BALLHEIGHT>= player\y  And ball\y + BALLHEIGHT <= player\y + PADDLEHEIGHT
  164.     ball\directiony = -ball\directiony + Rand(-3,3)
  165. EndIf
  166. End Function
  167.  
  168. Function CheckBallWithBlocks()
  169. ;y is the first row
  170. y = BLOCKYORIGIN 
  171.  
  172. For rows=0 To BLOCKROWS - 1
  173.  
  174.     ;Reset x to first block of column
  175.     x = BLOCKXORIGIN 
  176.  
  177.     ;For every column of blocks
  178.     For cols = 0 To BLOCKCOLUMNS - 1;
  179.         
  180.         ;If it exists
  181.         If blocks(rows,cols) 
  182.  
  183.             ;If the ball hit the block, delete the block
  184.             If ball\x >= x And ball\x <=  x + BLOCKWIDTH And ball\y >= y And ball\y <= y + BLOCKHEIGHT 
  185.  
  186.                 blocks(rows,cols) = 0  ;Delete block
  187.  
  188.                 ball\directiony = -ball\directiony + Rand(-2,2)  ;Reverse its direction and add randomizer        
  189.  
  190.                 score = score + 75
  191.  
  192.                 blockhits = blockhits + 1
  193.  
  194.                 ;It can't hit more than one block, so leave function
  195.                 Return 
  196.             EndIf
  197.         EndIf
  198.  
  199.         ;move to next column
  200.         x = x + BLOCKXGAP 
  201.     Next
  202.     
  203.     ;move to next row
  204.     y = y + BLOCKYGAP
  205. Next
  206.  
  207. End Function
  208.  
  209.  
  210.  
  211. Function CheckBallWithWalls()
  212. ;If ball hits the left wall, reverse its direction and add randomizer
  213. If ball\x <= 0 
  214.     ball\directionx = -ball\directionx + Rand(-2,2)
  215.  
  216. ;If ball hits top wall, reverse its direction and add randomizer
  217. ElseIf ball\y <= 0 
  218.     ball\directiony = -ball\directiony + Rand(-2,2)
  219.  
  220. ; If it hits right wall, reverse its direction and add randomizer
  221. ElseIf ball\x >= 640 - BALLWIDTH 
  222.     ball\directionx = -ball\directionx + Rand(-2,2) ;
  223.  
  224. ;If ball hits lower wall, dock points for missing ball
  225. ElseIf ball\y >= 480
  226.     score = score - 200
  227.  
  228.     ;Reset the level
  229.     ResetLevel()
  230. EndIf
  231. End Function
  232.  
  233.  
  234. Function TestInput()
  235.  
  236. ;hit Esc
  237. If KeyDown(ESCKEY) 
  238.  
  239.     ;quit the game
  240.     End 
  241.  
  242.  ;hit left arrow
  243. ElseIf KeyDown(LEFTKEY)
  244. ;move paddle left
  245.     player\x = player\x - 10 
  246.  
  247. ;hit right arrow
  248. ElseIf KeyDown(RIGHTKEY) 
  249.  
  250.     ;move paddle right
  251.     player\x = player\x + 10 
  252. EndIf
  253. End Function
  254.  
  255. Function ResetLevel()
  256. ball\x = 320
  257. ball\y = 150
  258. ball\directiony = 4
  259. ball\directionx = Rand(-5,5)
  260. player\x = STARTX
  261. player\y = STARTY
  262. Delay 500
  263.  
  264. End Function
  265.  
  266. Function DrawHUD()
  267. Text 0,440, "Level: " + level ;write the level
  268. Text 0,450, "Score: " + score ;write the score
  269. Text 0,460, "Block Hits: " + blockhits ;write the block hits
  270. End Function
  271.  
  272. Function NewLevel()
  273. For rows=0 To BLOCKROWS 
  274.     For cols=0 To BLOCKCOLUMNS 
  275.         ;Set block to existing (1)
  276.         blocks(rows,cols) = 1
  277.     Next
  278. Next
  279. ResetLevel()
  280. End Function