funkd0ct0r

Untitled

Sep 21st, 2013
64
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 = 51
  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. end
  65. if newX > boardWidth then
  66. ballVelX = ballVelX * -1
  67. bounced = true
  68. end
  69. if newY < 1 then
  70. ballVelY = ballVelY * -1
  71. bounced = true
  72. end
  73. if newY == paddleY and math.abs(newX - paddleX) <= paddleWidth2 then
  74. ballVelY = ballVelY * -1
  75. bounced = true
  76. end
  77. if boardArray[oldX][newY] ~= boardArray then
  78. breakBlock(oldX, newY)
  79. ballVelY = ballVelY * -1
  80. bounced = true
  81. end
  82. if boardArray[newX][oldY] ~= boardArray then
  83. breakBlock(oldX, newY)
  84. ballVelY = ballVelY * -1
  85. bounced = true
  86. end
  87. if newY > boardHeight then
  88. lives = lives - 1
  89. mon.setCursorPos(ballX, ballY)
  90. mon.setBackgroundColor(boardColor)
  91. mon.write(" ")
  92. ballX = paddleX
  93. ballY = paddleY - 1
  94. ballVelX = math.random(-1, 1)
  95. ballVelY = -1
  96. mon.setCursorPos(ballX, ballY)
  97. mon.setBackgroundColor(ballColor)
  98. mon.write(" ")
  99. end
  100. until not bounced
  101.  
  102. mon.setCursorPos(oldX, oldY)
  103. mon.setBackgroundColor(boardColor)
  104. mon.write(" ")
  105. ballX = ballX + ballVelX
  106. ballY = ballY + ballVelY
  107. mon.setCursorPos(newX, newY)
  108. mon.setBackgroundColor(ballColor)
  109. mon.write(" ")
  110.  
  111. timer1 = os.startTimer(timerLength)
  112. end
  113.  
  114. if event == "mouse_drag" or event == "mouse_click" then
  115. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  116. mon.setBackgroundColor(boardColor)
  117. mon.write(paddleString)
  118. paddleX = xPos
  119. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  120. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  121. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  122. mon.setBackgroundColor(paddleColor)
  123. mon.write(paddleString)
  124. end
  125.  
  126. if event == "key" then
  127. if param1 == 203 then --left
  128. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  129. mon.setBackgroundColor(boardColor)
  130. mon.write(paddleString)
  131. paddleX = paddleX - 1
  132. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  133. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  134. mon.setBackgroundColor(paddleColor)
  135. mon.write(paddleString)
  136. elseif param1 == 205 then --right
  137. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  138. mon.setBackgroundColor(boardColor)
  139. mon.write(paddleString)
  140. paddleX = paddleX + 1
  141. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  142. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  143. mon.setBackgroundColor(paddleColor)
  144. mon.write(paddleString)
  145. elseif param1 == 28 then --enter
  146. term.setBackgroundColor(colors.black)
  147. term.setCursorPos(1, 1)
  148. term.write("Paused")
  149.  
  150. repeat
  151. event, param = os.pullEvent()
  152. until event == "key" and param == 28
  153.  
  154. term.setCursorPos(1, 1)
  155. term.write(" ")
  156. timer1 = os.startTimer(timerLength)
  157. end
  158. end
  159. end
  160. end
  161.  
  162. local function board1()
  163. local color, prevColor = boardColor
  164. for y = 1, 3 do
  165. for x = 1, boardWidth do
  166. boardArray[x][y] = boardColor
  167. end
  168. end
  169. for y = 4, 10 do
  170. for x = 1, boardWidth, 5 do
  171. repeat
  172. color = blockColor[math.floor(math.random(1, numBlockColors + 0.999))]
  173. until color ~= prevColor
  174. for x2 = x, x + 4 do
  175. boardArray[x][y] = color
  176. end
  177. end
  178. end
  179. for y = 11, boardHeight do
  180. for x = 1, boardWidth do
  181. boardArray[x][y] = boardColor
  182. end
  183. end
  184. end
  185.  
  186. local function drawBoard()
  187. for y = 1, boardHeight do
  188. mon.setCursorPos(1, y)
  189. for x = 1, boardWidth do
  190. mon.setBackgroundColor(boardArray[x][y])
  191. mon.write(" ")
  192. end
  193. end
  194. end
  195.  
  196. for x = 1, boardWidth do
  197. boardArray[x] = {}
  198. end
  199.  
  200.  
  201. board1()
  202. drawBoard()
  203. main()
Advertisement
Add Comment
Please, Sign In to add comment