funkd0ct0r

Untitled

Sep 21st, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1.  
  2. local lives = 3
  3.  
  4.  
  5. local timerLength = 0.25
  6. local ballX, ballY
  7. local paddleX = 25
  8. local paddleY = 19
  9. local paddleWidth = 7
  10. local paddleWidth2 = 3
  11. local paddleString = " "
  12. local ballVelX, ballVelY
  13. local boardWidth = 51
  14. local boardHeight = 19
  15. local boardColor = colors.black
  16. local ballColor = colors.white
  17. local paddleColor = colors.gray
  18.  
  19. local function main()
  20.  
  21. local event, param1, param2, xPos, yPos
  22. local newX, newY
  23.  
  24. timer1 = os.startTimer(timerLength)
  25.  
  26. ballX = boardWidth / 2
  27. ballY = paddleY - 1
  28. ballVelX = math.random(-1, 1)
  29. ballVelY = -1
  30. paddleX = boardWidth / 2
  31.  
  32. while lives > -1 do
  33. event, param1, xPos, yPos = os.pullEvent()
  34.  
  35. if event == "timer" and param1 == timer1 then
  36. bounced = false
  37. repeat
  38. newX = math.floor(ballX + ballVelX)
  39. newY = math.floor(ballY + ballVelY)
  40. if newX < 1 then
  41. ballVelX = ballVelX * -1
  42. bounced = true
  43. end
  44. if newX > boardWidth then
  45. ballVelX = ballVelX * -1
  46. bounced = true
  47. end
  48. if newY < 1 then
  49. ballVelY = ballVelY * -1
  50. bounced = true
  51. end
  52. if newY == paddleY and math.abs(newX - paddleX) <= paddleWidth2 then
  53. ballVelY = ballVelY * -1
  54. bounced = true
  55. end
  56. if newY > boardHeight then
  57. lives = lives - 1
  58. mon.setCursorPos(ballX, ballY)
  59. mon.setBackgroundColor(boardColor)
  60. mon.write(" ")
  61. ballX = paddleX
  62. ballY = paddleY - 1
  63. ballVelX = math.random(-1, 1)
  64. ballVelY = -1
  65. mon.setCursorPos(ballX, ballY)
  66. mon.setBackgroundColor(ballColor)
  67. mon.write(" ")
  68. end
  69. until not bounced
  70.  
  71. mon.setCursorPos(ballX, ballY)
  72. mon.setBackgroundColor(boardColor)
  73. mon.write(" ")
  74. ballX = ballX + ballVelX
  75. ballY = ballY + ballVelY
  76. mon.setCursorPos(ballX, ballY)
  77. mon.setBackgroundColor(ballColor)
  78. mon.write(" ")
  79.  
  80. timer1 = os.startTimer(timerLength)
  81. end
  82.  
  83. if event == "mouse_drag" or event == "mouse_click" then
  84. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  85. mon.setBackgroundColor(boardColor)
  86. mon.write(paddleString)
  87. paddleX = xPos
  88. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  89. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  90. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  91. mon.setBackgroundColor(paddleColor)
  92. mon.write(paddleString)
  93. end
  94.  
  95. if event == "key" then
  96. if param1 == 203 then --left
  97. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  98. mon.setBackgroundColor(boardColor)
  99. mon.write(paddleString)
  100. paddleX = paddleX - 1
  101. if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
  102. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  103. mon.setBackgroundColor(paddleColor)
  104. mon.write(paddleString)
  105. elseif param1 == 205 then --right
  106. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  107. mon.setBackgroundColor(boardColor)
  108. mon.write(paddleString)
  109. paddleX = paddleX + 1
  110. if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
  111. mon.setCursorPos(paddleX - paddleWidth2, paddleY)
  112. mon.setBackgroundColor(paddleColor)
  113. mon.write(paddleString)
  114. elseif param1 == 28 then --enter
  115. term.setBackgroundColor(colors.black)
  116. term.setCursorPos(1, 1)
  117. term.write("Paused")
  118.  
  119. repeat
  120. event, param = os.pullEvent()
  121. until event == "key" and param == 28
  122.  
  123. term.setCursorPos(1, 1)
  124. term.write(" ")
  125. timer1 = os.startTimer(timerLength)
  126. end
  127. end
  128. end
  129.  
  130.  
  131. end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment