funkd0ct0r

Untitled

Sep 21st, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local lives = 3
  3.  
  4. local mon = term
  5.  
  6. local timerLength = 0.25
  7. local ballX, ballY
  8. local paddleX = 25
  9. local paddleY = 19
  10. local paddleWidth = 7
  11. local paddleWidth2 = 3
  12. local paddleString = " "
  13. local ballVelX, ballVelY
  14. local boardWidth = 50
  15. local boardHeight = 19
  16. local boardColor = colors.black
  17. local ballColor = colors.white
  18. local paddleColor = colors.gray
  19.  
  20. local blockColor = {colors.red, colors.blue, colors.green}
  21. local numBlockColors = 3
  22.  
  23. local boardArray = {}
  24.  
  25. local function breakBlock(px, py)
  26. minX = px
  27. color = boardArray[px][py]
  28. while boardArray[minX - 1][py] == color do minX = minX - 1 end
  29. mon.setBackgroundColor(boardColor)
  30. mon.setCursorPos(minX, py)
  31. mon.write(" ")
  32. maxX = minX + 4
  33. for x = minX, maxX do
  34. boardArray[x][py] = boardColor
  35. end
  36. end
  37.  
  38. local function main()
  39.  
  40. local event, param1, param2, xPos, yPos
  41. local oldX, oldY, newX, newY
  42.  
  43.  
  44. ballX = boardWidth / 2
  45. ballY = paddleY - 1
  46. ballVelX = math.random(-1, 1)
  47. ballVelY = -1
  48. paddleX = boardWidth / 2
  49.  
  50. timer1 = os.startTimer(timerLength)
  51. while lives > -1 do
  52. event, param1, xPos, yPos = os.pullEvent()
  53.  
  54. if event == "timer" and param1 == timer1 then
  55. oldX = math.floor(ballX)
  56. oldY = math.floor(ballY)
  57. repeat
  58. bounced = false
  59. newX = math.floor(ballX + ballVelX)
  60. newY = math.floor(ballY + ballVelY)
  61. if newX < 1 then
  62. ballVelX = ballVelX * -1
  63. bounced = true
  64. elseif newX > boardWidth then
  65. ballVelX = ballVelX * -1
  66. bounced = true
  67. elseif newY < 1 then
  68. ballVelY = ballVelY * -1
  69. bounced = true
  70. elseif newY == paddleY and math.abs(newX - paddleX) <= paddleWidth2 then
  71. ballVelY = ballVelY * -1
  72. bounced = true
  73. elseif newY > boardHeight then
  74. lives = lives - 1
  75. mon.setCursorPos(ballX, ballY)
  76. mon.setBackgroundColor(boardColor)
  77. mon.write(" ")
  78. ballX = paddleX
  79. ballY = paddleY - 1
  80. ballVelX = math.random(-1, 1)
  81. ballVelY = -1
  82. mon.setCursorPos(ballX, ballY)
  83. mon.setBackgroundColor(ballColor)
  84. mon.write(" ")
  85. elseif boardArray[oldX][newY] ~= boardColor then
  86. breakBlock(oldX, newY)
  87. ballVelY = ballVelY * -1
  88. bounced = true
  89. elseif boardArray[newX][oldY] ~= boardColor then
  90. breakBlock(oldX, newY)
  91. ballVelX = ballVelX * -1
  92. bounced = true
  93. end
  94. until not bounced
  95.  
  96. mon.setCursorPos(oldX, oldY)
  97. mon.setBackgroundColor(boardColor)
  98. mon.write(" ")
  99. ballX = ballX + ballVelX
  100. ballY = ballY + ballVelY
  101. mon.setCursorPos(newX, newY)
  102. mon.setBackgroundColor(ballColor)
  103. mon.write(" ")
  104.  
  105. timer1 = os.startTimer(timerLength)
  106. end
  107.  
  108. if event == "mouse_drag" or event == "mouse_click" then
  109. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  110. mon.setBackgroundColor(boardColor)
  111. mon.write(paddleString)
  112. paddleX = xPos
  113. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  114. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  115. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  116. mon.setBackgroundColor(paddleColor)
  117. mon.write(paddleString)
  118. end
  119.  
  120. if event == "key" then
  121. if param1 == 203 then --left
  122. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  123. mon.setBackgroundColor(boardColor)
  124. mon.write(paddleString)
  125. paddleX = paddleX - 1
  126. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  127. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  128. mon.setBackgroundColor(paddleColor)
  129. mon.write(paddleString)
  130. elseif param1 == 205 then --right
  131. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  132. mon.setBackgroundColor(boardColor)
  133. mon.write(paddleString)
  134. paddleX = paddleX + 1
  135. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  136. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  137. mon.setBackgroundColor(paddleColor)
  138. mon.write(paddleString)
  139. elseif param1 == 28 then --enter
  140. term.setBackgroundColor(colors.black)
  141. term.setCursorPos(1, 1)
  142. term.write("Paused")
  143.  
  144. repeat
  145. event, param = os.pullEvent()
  146. until event == "key" and param == 28
  147.  
  148. term.setCursorPos(1, 1)
  149. term.write(" ")
  150. timer1 = os.startTimer(timerLength)
  151. end
  152. end
  153. end
  154. end
  155.  
  156. local function board1()
  157. local color, prevColor = boardColor
  158. for y = 1, 3 do
  159. for x = 1, boardWidth do
  160. boardArray[x][y] = boardColor
  161. end
  162. end
  163. for y = 4, 10 do
  164. for x = 1, boardWidth, 5 do
  165. repeat
  166. color = blockColor[math.floor(math.random(1, numBlockColors + 0.999))]
  167. until color ~= prevColor
  168. for x2 = x, x + 4 do
  169. boardArray[x2][y] = color
  170. end
  171. end
  172. end
  173. for y = 11, boardHeight do
  174. for x = 1, boardWidth do
  175. boardArray[x][y] = boardColor
  176. end
  177. end
  178. end
  179.  
  180. local function drawBoard()
  181. for y = 1, boardHeight do
  182. mon.setCursorPos(1, y)
  183. for x = 1, boardWidth do
  184. mon.setBackgroundColor(boardArray[x][y])
  185. mon.write(" ")
  186. end
  187. end
  188. end
  189.  
  190. for x = 1, boardWidth do
  191. boardArray[x] = {}
  192. end
  193.  
  194.  
  195. board1()
  196. drawBoard()
  197. main()
Advertisement
Add Comment
Please, Sign In to add comment