funkd0ct0r

Untitled

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