nonogamer9

Pong For ComputerCraft! (Coded By Me)

Jan 3rd, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.73 KB | Software | 0 0
  1. local w, h = term.getSize()
  2. local buffer = window.create(term.current(), 1, 1, w, h, false)
  3.  
  4. local gameSpeedMultiplier = 0.5
  5.  
  6. local gameRunning = false
  7. local inTitleScreen = true
  8. local vsComputer = false
  9. local gameOver = false
  10.  
  11. local paddleHeight = 5
  12. local paddleWidth = 1
  13. local leftPaddle = {x = 2, y = h/2 - paddleHeight/2}
  14. local rightPaddle = {x = w-2, y = h/2 - paddleHeight/2}
  15. local leftPaddleVelocity = 0
  16. local rightPaddleVelocity = 0
  17. local maxPaddleSpeed = 0.5
  18. local paddleAcceleration = 0.1
  19. local paddleDeceleration = 0.05
  20.  
  21. local ball = {x = w/2, y = h/2, dx = 0.5, dy = 0.25}
  22.  
  23. local leftScore = 0
  24. local rightScore = 0
  25. local winScore = 5
  26.  
  27. local time = 0
  28. local plasmaSpeed = 0.05
  29.  
  30. local pressedKeys = {}
  31.  
  32. local function drawPlasma()
  33.   while not gameOver do
  34.     for y = 1, h do
  35.       for x = 1, w do
  36.         local value = math.sin(x/10 + time) + math.sin(y/10 + time) + math.sin((x+y)/10 + time)
  37.         value = math.floor((value + 3) * 5) % 16 + 1
  38.         buffer.setCursorPos(x, y)
  39.         buffer.setBackgroundColor(value)
  40.         buffer.write(" ")
  41.       end
  42.     end
  43.     time = time + plasmaSpeed
  44.     sleep(0.05)
  45.   end
  46. end
  47.  
  48. local function drawPaddle(paddle)
  49.   buffer.setBackgroundColor(colors.white)
  50.   for i = 0, paddleHeight-1 do
  51.     buffer.setCursorPos(paddle.x, math.floor(paddle.y) + i)
  52.     buffer.write(string.rep(" ", paddleWidth))
  53.   end
  54. end
  55.  
  56. local function drawBall()
  57.   buffer.setCursorPos(math.floor(ball.x), math.floor(ball.y))
  58.   buffer.setBackgroundColor(colors.green)
  59.   buffer.write(" ")
  60. end
  61.  
  62. local function drawScore()
  63.   buffer.setCursorPos(w/2 - 3, 1)
  64.   buffer.setBackgroundColor(colors.black)
  65.   buffer.setTextColor(colors.white)
  66.   buffer.write(leftScore .. " - " .. rightScore)
  67. end
  68.  
  69. local function handleKeys()
  70.   while not gameOver do
  71.     local event, key = os.pullEvent()
  72.     if event == "key" then
  73.       pressedKeys[key] = true
  74.       if inTitleScreen then
  75.         if key == keys.enter then
  76.           inTitleScreen = false
  77.           gameRunning = true
  78.           vsComputer = false
  79.         elseif key == keys.c then
  80.           inTitleScreen = false
  81.           gameRunning = true
  82.           vsComputer = true
  83.         elseif key == keys.q then
  84.           gameOver = true
  85.           return
  86.         end
  87.       end
  88.     elseif event == "key_up" then
  89.       pressedKeys[key] = nil
  90.     end
  91.    
  92.     if gameRunning and key == keys.q then
  93.       gameRunning = false
  94.       inTitleScreen = true
  95.       leftScore = 0
  96.       rightScore = 0
  97.     end
  98.   end
  99. end
  100.  
  101. local function drawTitleScreen()
  102.   local title = "Welcome to Pong!"
  103.   local instructions = {
  104.     "Press ENTER for 2 players",
  105.     "Press C to play vs Computer",
  106.     "W/S and Up/Down to move",
  107.     "Q to quit",
  108.     "Coded by nonogamer9"
  109.   }
  110.  
  111.   buffer.setBackgroundColor(colors.black)
  112.   buffer.clear()
  113.  
  114.   buffer.setTextColor(colors.white)
  115.   buffer.setCursorPos(w/2 - #title/2, h/2 - 3)
  116.   buffer.write(title)
  117.  
  118.   for j, line in ipairs(instructions) do
  119.     buffer.setCursorPos(w/2 - #line/2, h/2 - 1 + j*2)
  120.     buffer.write(line)
  121.   end
  122.  
  123.   buffer.setVisible(false)
  124.   buffer.redraw()
  125.   buffer.setVisible(true)
  126. end
  127.  
  128. local function drawTitleOverPlasma()
  129.   buffer.setBackgroundColor(colors.black)
  130.   local titleY = h/2 - 3
  131.   local instructionsY = h/2 - 1
  132.  
  133.   for y = titleY - 1, instructionsY + 10 do
  134.     buffer.setCursorPos(1, y)
  135.     buffer.write(string.rep(" ", w))
  136.   end
  137.  
  138.   buffer.setTextColor(colors.white)
  139.   buffer.setCursorPos(w/2 - #"Welcome to Pong!"/2, titleY)
  140.   buffer.write("Welcome to Pong!")
  141.  
  142.   local instructions = {
  143.     "Press ENTER for 2 players",
  144.     "Press C to play vs Computer",
  145.     "W/S and Up/Down to move",
  146.     "Q to quit",
  147.     "Coded by nonogamer9"
  148.   }
  149.   for i, line in ipairs(instructions) do
  150.     buffer.setCursorPos(w/2 - #line/2, instructionsY + i*2)
  151.     buffer.write(line)
  152.   end
  153. end
  154.  
  155. local function updateCPU(dt)
  156.   local targetY = ball.y - paddleHeight / 2
  157.   local currentY = rightPaddle.y
  158.   local diff = targetY - currentY
  159.  
  160.   if math.abs(diff) > 0.5 then
  161.     rightPaddleVelocity = math.min(math.max(diff, -maxPaddleSpeed), maxPaddleSpeed)
  162.   else
  163.     rightPaddleVelocity = 0
  164.   end
  165.  
  166.   rightPaddle.y = math.max(1, math.min(h - paddleHeight, rightPaddle.y + rightPaddleVelocity * dt * 60))
  167. end
  168.  
  169. local function showEndGameMessage(winner)
  170.   buffer.setBackgroundColor(colors.black)
  171.   buffer.setTextColor(colors.white)
  172.   local message = winner .. " wins!"
  173.   buffer.setCursorPos(w/2 - #message/2, h/2)
  174.   buffer.write(message)
  175.   buffer.setCursorPos(w/2 - 11, h/2 + 2)
  176.   buffer.write("Press any key to continue")
  177.   buffer.setVisible(false)
  178.   buffer.redraw()
  179.   buffer.setVisible(true)
  180.   os.pullEvent("key")
  181. end
  182.  
  183. local function gameLoop()
  184.   local lastUpdate = os.clock()
  185.   local titleAnimationDone = false
  186.  
  187.   while not gameOver do
  188.     local currentTime = os.clock()
  189.     local dt = (currentTime - lastUpdate) * gameSpeedMultiplier
  190.     lastUpdate = currentTime
  191.  
  192.     if inTitleScreen then
  193.       if not titleAnimationDone then
  194.         drawTitleScreen()
  195.         titleAnimationDone = true
  196.       else
  197.         drawTitleOverPlasma()
  198.       end
  199.     elseif gameRunning then
  200.       ball.x = ball.x + ball.dx * dt * 60
  201.       ball.y = ball.y + ball.dy * dt * 60
  202.      
  203.       if ball.y <= 1 or ball.y >= h then
  204.         ball.dy = -ball.dy
  205.       end
  206.      
  207.       if ball.x <= leftPaddle.x + paddleWidth and ball.y >= leftPaddle.y and ball.y < leftPaddle.y + paddleHeight then
  208.         ball.dx = -ball.dx
  209.       elseif ball.x >= rightPaddle.x - 1 and ball.y >= rightPaddle.y and ball.y < rightPaddle.y + paddleHeight then
  210.         ball.dx = -ball.dx
  211.       end
  212.      
  213.       if ball.x < 1 then
  214.         rightScore = rightScore + 1
  215.         ball.x = w/2
  216.         ball.y = h/2
  217.       elseif ball.x > w then
  218.         leftScore = leftScore + 1
  219.         ball.x = w/2
  220.         ball.y = h/2
  221.       end
  222.      
  223.       if leftScore >= winScore or rightScore >= winScore then
  224.         gameRunning = false
  225.         if leftScore >= winScore then
  226.           showEndGameMessage("Left player")
  227.         else
  228.           showEndGameMessage(vsComputer and "Computer" or "Right player")
  229.         end
  230.         leftScore = 0
  231.         rightScore = 0
  232.         inTitleScreen = true
  233.         titleAnimationDone = false
  234.       end
  235.      
  236.       if pressedKeys[keys.w] then
  237.         leftPaddleVelocity = math.min(leftPaddleVelocity + paddleAcceleration, maxPaddleSpeed)
  238.       elseif pressedKeys[keys.s] then
  239.         leftPaddleVelocity = math.max(leftPaddleVelocity - paddleAcceleration, -maxPaddleSpeed)
  240.       else
  241.         leftPaddleVelocity = leftPaddleVelocity * (1 - paddleDeceleration)
  242.       end
  243.  
  244.       if not vsComputer then
  245.         if pressedKeys[keys.up] then
  246.           rightPaddleVelocity = math.min(rightPaddleVelocity + paddleAcceleration, maxPaddleSpeed)
  247.         elseif pressedKeys[keys.down] then
  248.           rightPaddleVelocity = math.max(rightPaddleVelocity - paddleAcceleration, -maxPaddleSpeed)
  249.         else
  250.           rightPaddleVelocity = rightPaddleVelocity * (1 - paddleDeceleration)
  251.         end
  252.       else
  253.         updateCPU(dt)
  254.       end
  255.  
  256.       leftPaddle.y = math.max(1, math.min(h - paddleHeight, leftPaddle.y + leftPaddleVelocity * dt * 60))
  257.       if not vsComputer then
  258.         rightPaddle.y = math.max(1, math.min(h - paddleHeight, rightPaddle.y + rightPaddleVelocity * dt * 60))
  259.       end
  260.      
  261.       drawPaddle(leftPaddle)
  262.       drawPaddle(rightPaddle)
  263.       drawBall()
  264.       drawScore()
  265.     end
  266.    
  267.     buffer.setVisible(false)
  268.     buffer.redraw()
  269.     buffer.setVisible(true)
  270.    
  271.     sleep(0.016)
  272.   end
  273. end
  274.  
  275. parallel.waitForAll(drawPlasma, handleKeys, gameLoop)
  276.  
  277. term.setBackgroundColor(colors.black)
  278. term.setTextColor(colors.white)
  279. term.clear()
  280. term.setCursorPos(1,1)
  281. print("Thanks for playing Pong!")
  282.  
Add Comment
Please, Sign In to add comment