Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local buffer = window.create(term.current(), 1, 1, w, h, false)
- local gameSpeedMultiplier = 0.5
- local gameRunning = false
- local inTitleScreen = true
- local vsComputer = false
- local gameOver = false
- local paddleHeight = 5
- local paddleWidth = 1
- local leftPaddle = {x = 2, y = h/2 - paddleHeight/2}
- local rightPaddle = {x = w-2, y = h/2 - paddleHeight/2}
- local leftPaddleVelocity = 0
- local rightPaddleVelocity = 0
- local maxPaddleSpeed = 0.5
- local paddleAcceleration = 0.1
- local paddleDeceleration = 0.05
- local ball = {x = w/2, y = h/2, dx = 0.5, dy = 0.25}
- local leftScore = 0
- local rightScore = 0
- local winScore = 5
- local time = 0
- local plasmaSpeed = 0.05
- local pressedKeys = {}
- local function drawPlasma()
- while not gameOver do
- for y = 1, h do
- for x = 1, w do
- local value = math.sin(x/10 + time) + math.sin(y/10 + time) + math.sin((x+y)/10 + time)
- value = math.floor((value + 3) * 5) % 16 + 1
- buffer.setCursorPos(x, y)
- buffer.setBackgroundColor(value)
- buffer.write(" ")
- end
- end
- time = time + plasmaSpeed
- sleep(0.05)
- end
- end
- local function drawPaddle(paddle)
- buffer.setBackgroundColor(colors.white)
- for i = 0, paddleHeight-1 do
- buffer.setCursorPos(paddle.x, math.floor(paddle.y) + i)
- buffer.write(string.rep(" ", paddleWidth))
- end
- end
- local function drawBall()
- buffer.setCursorPos(math.floor(ball.x), math.floor(ball.y))
- buffer.setBackgroundColor(colors.green)
- buffer.write(" ")
- end
- local function drawScore()
- buffer.setCursorPos(w/2 - 3, 1)
- buffer.setBackgroundColor(colors.black)
- buffer.setTextColor(colors.white)
- buffer.write(leftScore .. " - " .. rightScore)
- end
- local function handleKeys()
- while not gameOver do
- local event, key = os.pullEvent()
- if event == "key" then
- pressedKeys[key] = true
- if inTitleScreen then
- if key == keys.enter then
- inTitleScreen = false
- gameRunning = true
- vsComputer = false
- elseif key == keys.c then
- inTitleScreen = false
- gameRunning = true
- vsComputer = true
- elseif key == keys.q then
- gameOver = true
- return
- end
- end
- elseif event == "key_up" then
- pressedKeys[key] = nil
- end
- if gameRunning and key == keys.q then
- gameRunning = false
- inTitleScreen = true
- leftScore = 0
- rightScore = 0
- end
- end
- end
- local function drawTitleScreen()
- local title = "Welcome to Pong!"
- local instructions = {
- "Press ENTER for 2 players",
- "Press C to play vs Computer",
- "W/S and Up/Down to move",
- "Q to quit",
- "Coded by nonogamer9"
- }
- buffer.setBackgroundColor(colors.black)
- buffer.clear()
- buffer.setTextColor(colors.white)
- buffer.setCursorPos(w/2 - #title/2, h/2 - 3)
- buffer.write(title)
- for j, line in ipairs(instructions) do
- buffer.setCursorPos(w/2 - #line/2, h/2 - 1 + j*2)
- buffer.write(line)
- end
- buffer.setVisible(false)
- buffer.redraw()
- buffer.setVisible(true)
- end
- local function drawTitleOverPlasma()
- buffer.setBackgroundColor(colors.black)
- local titleY = h/2 - 3
- local instructionsY = h/2 - 1
- for y = titleY - 1, instructionsY + 10 do
- buffer.setCursorPos(1, y)
- buffer.write(string.rep(" ", w))
- end
- buffer.setTextColor(colors.white)
- buffer.setCursorPos(w/2 - #"Welcome to Pong!"/2, titleY)
- buffer.write("Welcome to Pong!")
- local instructions = {
- "Press ENTER for 2 players",
- "Press C to play vs Computer",
- "W/S and Up/Down to move",
- "Q to quit",
- "Coded by nonogamer9"
- }
- for i, line in ipairs(instructions) do
- buffer.setCursorPos(w/2 - #line/2, instructionsY + i*2)
- buffer.write(line)
- end
- end
- local function updateCPU(dt)
- local targetY = ball.y - paddleHeight / 2
- local currentY = rightPaddle.y
- local diff = targetY - currentY
- if math.abs(diff) > 0.5 then
- rightPaddleVelocity = math.min(math.max(diff, -maxPaddleSpeed), maxPaddleSpeed)
- else
- rightPaddleVelocity = 0
- end
- rightPaddle.y = math.max(1, math.min(h - paddleHeight, rightPaddle.y + rightPaddleVelocity * dt * 60))
- end
- local function showEndGameMessage(winner)
- buffer.setBackgroundColor(colors.black)
- buffer.setTextColor(colors.white)
- local message = winner .. " wins!"
- buffer.setCursorPos(w/2 - #message/2, h/2)
- buffer.write(message)
- buffer.setCursorPos(w/2 - 11, h/2 + 2)
- buffer.write("Press any key to continue")
- buffer.setVisible(false)
- buffer.redraw()
- buffer.setVisible(true)
- os.pullEvent("key")
- end
- local function gameLoop()
- local lastUpdate = os.clock()
- local titleAnimationDone = false
- while not gameOver do
- local currentTime = os.clock()
- local dt = (currentTime - lastUpdate) * gameSpeedMultiplier
- lastUpdate = currentTime
- if inTitleScreen then
- if not titleAnimationDone then
- drawTitleScreen()
- titleAnimationDone = true
- else
- drawTitleOverPlasma()
- end
- elseif gameRunning then
- ball.x = ball.x + ball.dx * dt * 60
- ball.y = ball.y + ball.dy * dt * 60
- if ball.y <= 1 or ball.y >= h then
- ball.dy = -ball.dy
- end
- if ball.x <= leftPaddle.x + paddleWidth and ball.y >= leftPaddle.y and ball.y < leftPaddle.y + paddleHeight then
- ball.dx = -ball.dx
- elseif ball.x >= rightPaddle.x - 1 and ball.y >= rightPaddle.y and ball.y < rightPaddle.y + paddleHeight then
- ball.dx = -ball.dx
- end
- if ball.x < 1 then
- rightScore = rightScore + 1
- ball.x = w/2
- ball.y = h/2
- elseif ball.x > w then
- leftScore = leftScore + 1
- ball.x = w/2
- ball.y = h/2
- end
- if leftScore >= winScore or rightScore >= winScore then
- gameRunning = false
- if leftScore >= winScore then
- showEndGameMessage("Left player")
- else
- showEndGameMessage(vsComputer and "Computer" or "Right player")
- end
- leftScore = 0
- rightScore = 0
- inTitleScreen = true
- titleAnimationDone = false
- end
- if pressedKeys[keys.w] then
- leftPaddleVelocity = math.min(leftPaddleVelocity + paddleAcceleration, maxPaddleSpeed)
- elseif pressedKeys[keys.s] then
- leftPaddleVelocity = math.max(leftPaddleVelocity - paddleAcceleration, -maxPaddleSpeed)
- else
- leftPaddleVelocity = leftPaddleVelocity * (1 - paddleDeceleration)
- end
- if not vsComputer then
- if pressedKeys[keys.up] then
- rightPaddleVelocity = math.min(rightPaddleVelocity + paddleAcceleration, maxPaddleSpeed)
- elseif pressedKeys[keys.down] then
- rightPaddleVelocity = math.max(rightPaddleVelocity - paddleAcceleration, -maxPaddleSpeed)
- else
- rightPaddleVelocity = rightPaddleVelocity * (1 - paddleDeceleration)
- end
- else
- updateCPU(dt)
- end
- leftPaddle.y = math.max(1, math.min(h - paddleHeight, leftPaddle.y + leftPaddleVelocity * dt * 60))
- if not vsComputer then
- rightPaddle.y = math.max(1, math.min(h - paddleHeight, rightPaddle.y + rightPaddleVelocity * dt * 60))
- end
- drawPaddle(leftPaddle)
- drawPaddle(rightPaddle)
- drawBall()
- drawScore()
- end
- buffer.setVisible(false)
- buffer.redraw()
- buffer.setVisible(true)
- sleep(0.016)
- end
- end
- parallel.waitForAll(drawPlasma, handleKeys, gameLoop)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("Thanks for playing Pong!")
Add Comment
Please, Sign In to add comment