Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lives = 3
- local mon = term
- local timerLength = 0.25
- local ballX, ballY
- local paddleX = 25
- local paddleY = 19
- local paddleWidth = 7
- local paddleWidth2 = 3
- local paddleString = " "
- local ballVelX, ballVelY
- local boardWidth = 51
- local boardHeight = 19
- local boardColor = colors.black
- local ballColor = colors.white
- local paddleColor = colors.gray
- local blockColor = {colors.red, colors.blue, colors.green}
- local numBlockColors = 3
- local boardArray = {}
- local function breakBlock(px, py)
- minX = px
- color = boardArray[px][py]
- while boardArray[minX - 1][py] == color do minX = minX - 1 end
- mon.setBackgroundColor(boardColor)
- mon.setCursorPos(minX, py)
- mon.write(" ")
- maxX = minX + 4
- for x = minX, maxX do
- boardArray[x][py] = boardColor
- end
- end
- local function main()
- local event, param1, param2, xPos, yPos
- local oldX, oldY, newX, newY
- ballX = boardWidth / 2
- ballY = paddleY - 1
- ballVelX = math.random(-1, 1)
- ballVelY = -1
- paddleX = boardWidth / 2
- timer1 = os.startTimer(timerLength)
- while lives > -1 do
- event, param1, xPos, yPos = os.pullEvent()
- if event == "timer" and param1 == timer1 then
- oldX = math.floor(ballX)
- oldY = math.floor(ballY)
- repeat
- bounced = false
- newX = math.floor(ballX + ballVelX)
- newY = math.floor(ballY + ballVelY)
- if newX < 1 then
- ballVelX = ballVelX * -1
- bounced = true
- end
- if newX > boardWidth then
- ballVelX = ballVelX * -1
- bounced = true
- end
- if newY < 1 then
- ballVelY = ballVelY * -1
- bounced = true
- end
- if newY == paddleY and math.abs(newX - paddleX) <= paddleWidth2 then
- ballVelY = ballVelY * -1
- bounced = true
- end
- if boardArray[oldX][newY] ~= boardArray then
- breakBlock(oldX, newY)
- ballVelY = ballVelY * -1
- bounced = true
- end
- if boardArray[newX][oldY] ~= boardArray then
- breakBlock(oldX, newY)
- ballVelY = ballVelY * -1
- bounced = true
- end
- if newY > boardHeight then
- lives = lives - 1
- mon.setCursorPos(ballX, ballY)
- mon.setBackgroundColor(boardColor)
- mon.write(" ")
- ballX = paddleX
- ballY = paddleY - 1
- ballVelX = math.random(-1, 1)
- ballVelY = -1
- mon.setCursorPos(ballX, ballY)
- mon.setBackgroundColor(ballColor)
- mon.write(" ")
- end
- until not bounced
- mon.setCursorPos(oldX, oldY)
- mon.setBackgroundColor(boardColor)
- mon.write(" ")
- ballX = ballX + ballVelX
- ballY = ballY + ballVelY
- mon.setCursorPos(newX, newY)
- mon.setBackgroundColor(ballColor)
- mon.write(" ")
- timer1 = os.startTimer(timerLength)
- end
- if event == "mouse_drag" or event == "mouse_click" then
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(boardColor)
- mon.write(paddleString)
- paddleX = xPos
- if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
- if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(paddleColor)
- mon.write(paddleString)
- end
- if event == "key" then
- if param1 == 203 then --left
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(boardColor)
- mon.write(paddleString)
- paddleX = paddleX - 1
- if paddleX - paddleWidth2 < 1 then paddleX = paddleWidth2 + 1 end
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(paddleColor)
- mon.write(paddleString)
- elseif param1 == 205 then --right
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(boardColor)
- mon.write(paddleString)
- paddleX = paddleX + 1
- if paddleX + paddleWidth2 > boardWidth then paddleX = boardWidth - paddleWidth2 end
- mon.setCursorPos(paddleX - paddleWidth2, paddleY)
- mon.setBackgroundColor(paddleColor)
- mon.write(paddleString)
- elseif param1 == 28 then --enter
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 1)
- term.write("Paused")
- repeat
- event, param = os.pullEvent()
- until event == "key" and param == 28
- term.setCursorPos(1, 1)
- term.write(" ")
- timer1 = os.startTimer(timerLength)
- end
- end
- end
- end
- local function board1()
- local color, prevColor = boardColor
- for y = 1, 3 do
- for x = 1, boardWidth do
- boardArray[x][y] = boardColor
- end
- end
- for y = 4, 10 do
- for x = 1, boardWidth, 5 do
- repeat
- color = blockColor[math.floor(math.random(1, numBlockColors + 0.999))]
- until color ~= prevColor
- for x2 = x, x + 4 do
- boardArray[x][y] = color
- end
- end
- end
- for y = 11, boardHeight do
- for x = 1, boardWidth do
- boardArray[x][y] = boardColor
- end
- end
- end
- local function drawBoard()
- for y = 1, boardHeight do
- mon.setCursorPos(1, y)
- for x = 1, boardWidth do
- mon.setBackgroundColor(boardArray[x][y])
- mon.write(" ")
- end
- end
- end
- for x = 1, boardWidth do
- boardArray[x] = {}
- end
- board1()
- drawBoard()
- main()
Advertisement
Add Comment
Please, Sign In to add comment