ElijahCrafter

Pong

Nov 27th, 2025
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | Source Code | 0 0
  1. -- Pong Game for SimpleOS
  2. -- CC: Tweaked compatible
  3.  
  4. local w, h = term.getSize()
  5. h = h - 1 -- Leave room for score
  6.  
  7. -- Game state
  8. local paddle1Y, paddle2Y = math.floor(h/2) - 1, math.floor(h/2) - 1
  9. local paddleHeight = 4
  10. local ballX, ballY = math.floor(w/2), math.floor(h/2)
  11. local ballDX, ballDY = 1, 0
  12. local score1, score2 = 0, 0
  13. local gameOver = false
  14. local speed = 0.08
  15. local aiMode = true -- Single player by default
  16.  
  17. -- Reset ball
  18. local function resetBall(direction)
  19.     ballX = math.floor(w/2)
  20.     ballY = math.floor(h/2)
  21.     ballDX = direction or (math.random(2) == 1 and 1 or -1)
  22.     ballDY = (math.random(3) - 2) * 0.5
  23. end
  24.  
  25. -- Draw game
  26. local function draw()
  27.     term.setBackgroundColor(colors.black)
  28.     term.clear()
  29.    
  30.     -- Draw center line
  31.     term.setTextColor(colors.gray)
  32.     for y = 1, h do
  33.         if y % 2 == 0 then
  34.             term.setCursorPos(math.floor(w/2), y)
  35.             term.write("|")
  36.         end
  37.     end
  38.    
  39.     -- Draw paddles
  40.     term.setBackgroundColor(colors.white)
  41.     for i = 0, paddleHeight - 1 do
  42.         term.setCursorPos(2, paddle1Y + i)
  43.         term.write(" ")
  44.         term.setCursorPos(w - 1, paddle2Y + i)
  45.         term.write(" ")
  46.     end
  47.    
  48.     -- Draw ball
  49.     term.setBackgroundColor(colors.black)
  50.     term.setTextColor(colors.yellow)
  51.     term.setCursorPos(math.floor(ballX), math.floor(ballY))
  52.     term.write("O")
  53.    
  54.     -- Draw score bar
  55.     term.setBackgroundColor(colors.gray)
  56.     for x = 1, w do
  57.         term.setCursorPos(x, h + 1)
  58.         term.write(" ")
  59.     end
  60.     term.setTextColor(colors.white)
  61.     term.setCursorPos(2, h + 1)
  62.     term.write("P1: " .. score1)
  63.     term.setCursorPos(w - 7, h + 1)
  64.     term.write("P2: " .. score2)
  65.     term.setCursorPos(math.floor(w/2) - 3, h + 1)
  66.     term.write("[Q]uit")
  67. end
  68.  
  69. -- AI movement
  70. local function moveAI()
  71.     local targetY = ballY - paddleHeight / 2
  72.     if paddle2Y < targetY - 1 then
  73.         paddle2Y = math.min(paddle2Y + 1, h - paddleHeight + 1)
  74.     elseif paddle2Y > targetY + 1 then
  75.         paddle2Y = math.max(paddle2Y - 1, 1)
  76.     end
  77. end
  78.  
  79. -- Update ball
  80. local function update()
  81.     ballX = ballX + ballDX
  82.     ballY = ballY + ballDY
  83.    
  84.     -- Top/bottom bounce
  85.     if ballY <= 1 then
  86.         ballY = 1
  87.         ballDY = -ballDY
  88.     elseif ballY >= h then
  89.         ballY = h
  90.         ballDY = -ballDY
  91.     end
  92.    
  93.     -- Paddle 1 collision
  94.     if ballX <= 3 and ballX >= 2 then
  95.         if ballY >= paddle1Y and ballY < paddle1Y + paddleHeight then
  96.             ballDX = math.abs(ballDX)
  97.             local hitPos = (ballY - paddle1Y) / paddleHeight
  98.             ballDY = (hitPos - 0.5) * 2
  99.         end
  100.     end
  101.    
  102.     -- Paddle 2 collision
  103.     if ballX >= w - 2 and ballX <= w - 1 then
  104.         if ballY >= paddle2Y and ballY < paddle2Y + paddleHeight then
  105.             ballDX = -math.abs(ballDX)
  106.             local hitPos = (ballY - paddle2Y) / paddleHeight
  107.             ballDY = (hitPos - 0.5) * 2
  108.         end
  109.     end
  110.    
  111.     -- Scoring
  112.     if ballX < 1 then
  113.         score2 = score2 + 1
  114.         resetBall(-1)
  115.     elseif ballX > w then
  116.         score1 = score1 + 1
  117.         resetBall(1)
  118.     end
  119.    
  120.     -- Win condition
  121.     if score1 >= 10 or score2 >= 10 then
  122.         gameOver = true
  123.     end
  124. end
  125.  
  126. -- Game over screen
  127. local function showGameOver()
  128.     term.setBackgroundColor(colors.black)
  129.     term.setTextColor(colors.yellow)
  130.     term.setCursorPos(math.floor(w/2) - 5, math.floor(h/2) - 1)
  131.     if score1 >= 10 then
  132.         term.write("PLAYER 1 WINS!")
  133.     else
  134.         term.write("PLAYER 2 WINS!")
  135.     end
  136.     term.setTextColor(colors.white)
  137.     term.setCursorPos(math.floor(w/2) - 10, math.floor(h/2) + 1)
  138.     term.write("Press ENTER to restart")
  139.     term.setCursorPos(math.floor(w/2) - 7, math.floor(h/2) + 2)
  140.     term.write("Press Q to quit")
  141. end
  142.  
  143. -- Main game loop
  144. local function run()
  145.     resetBall()
  146.     score1, score2 = 0, 0
  147.     gameOver = false
  148.     paddle1Y = math.floor(h/2) - paddleHeight/2
  149.     paddle2Y = math.floor(h/2) - paddleHeight/2
  150.    
  151.     -- Mode selection
  152.     term.setBackgroundColor(colors.black)
  153.     term.clear()
  154.     term.setTextColor(colors.yellow)
  155.     term.setCursorPos(math.floor(w/2) - 3, math.floor(h/2) - 2)
  156.     term.write("PONG")
  157.     term.setTextColor(colors.white)
  158.     term.setCursorPos(math.floor(w/2) - 10, math.floor(h/2))
  159.     term.write("[1] Single Player (vs AI)")
  160.     term.setCursorPos(math.floor(w/2) - 8, math.floor(h/2) + 1)
  161.     term.write("[2] Two Players")
  162.    
  163.     while true do
  164.         local event, key = os.pullEvent("key")
  165.         if key == keys.one then
  166.             aiMode = true
  167.             break
  168.         elseif key == keys.two then
  169.             aiMode = false
  170.             break
  171.         elseif key == keys.q then
  172.             return
  173.         end
  174.     end
  175.    
  176.     while true do
  177.         draw()
  178.        
  179.         if gameOver then
  180.             showGameOver()
  181.             while true do
  182.                 local event, key = os.pullEvent("key")
  183.                 if key == keys.enter then
  184.                     resetBall()
  185.                     score1, score2 = 0, 0
  186.                     gameOver = false
  187.                     paddle1Y = math.floor(h/2) - paddleHeight/2
  188.                     paddle2Y = math.floor(h/2) - paddleHeight/2
  189.                     break
  190.                 elseif key == keys.q then
  191.                     return
  192.                 end
  193.             end
  194.         end
  195.        
  196.         local timer = os.startTimer(speed)
  197.        
  198.         while true do
  199.             local event, p1 = os.pullEvent()
  200.            
  201.             if event == "key" then
  202.                 -- Player 1 controls (W/S)
  203.                 if p1 == keys.w then
  204.                     paddle1Y = math.max(1, paddle1Y - 1)
  205.                 elseif p1 == keys.s then
  206.                     paddle1Y = math.min(h - paddleHeight + 1, paddle1Y + 1)
  207.                 end
  208.                
  209.                 -- Player 2 controls (Up/Down) - only in 2P mode
  210.                 if not aiMode then
  211.                     if p1 == keys.up then
  212.                         paddle2Y = math.max(1, paddle2Y - 1)
  213.                     elseif p1 == keys.down then
  214.                         paddle2Y = math.min(h - paddleHeight + 1, paddle2Y + 1)
  215.                     end
  216.                 end
  217.                
  218.                 if p1 == keys.q then
  219.                     return
  220.                 end
  221.                 draw()
  222.             elseif event == "timer" and p1 == timer then
  223.                 break
  224.             end
  225.         end
  226.        
  227.         if aiMode then
  228.             moveAI()
  229.         end
  230.         update()
  231.     end
  232. end
  233.  
  234. run()
  235.  
Advertisement
Add Comment
Please, Sign In to add comment