ElijahCrafter

Breakout

Nov 27th, 2025
772
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.36 KB | Source Code | 0 0
  1. -- Breakout Game for SimpleOS
  2. -- CC: Tweaked compatible
  3.  
  4. local w, h = term.getSize()
  5. h = h - 1
  6.  
  7. -- Game state
  8. local paddleX = math.floor(w/2) - 3
  9. local paddleWidth = 7
  10. local ballX, ballY = math.floor(w/2), h - 3
  11. local ballDX, ballDY = 1, -1
  12. local bricks = {}
  13. local score = 0
  14. local lives = 3
  15. local gameOver = false
  16. local gameWon = false
  17. local speed = 0.05
  18. local ballActive = false
  19.  
  20. -- Brick settings
  21. local brickRows = 5
  22. local brickCols = math.floor((w - 4) / 4)
  23. local brickWidth = 3
  24. local brickStartY = 3
  25.  
  26. -- Brick colors
  27. local brickColors = {
  28.     colors.red,
  29.     colors.orange,
  30.     colors.yellow,
  31.     colors.green,
  32.     colors.cyan
  33. }
  34.  
  35. -- Initialize bricks
  36. local function initBricks()
  37.     bricks = {}
  38.     for row = 1, brickRows do
  39.         bricks[row] = {}
  40.         for col = 1, brickCols do
  41.             bricks[row][col] = true
  42.         end
  43.     end
  44. end
  45.  
  46. -- Count remaining bricks
  47. local function countBricks()
  48.     local count = 0
  49.     for row = 1, brickRows do
  50.         for col = 1, brickCols do
  51.             if bricks[row][col] then count = count + 1 end
  52.         end
  53.     end
  54.     return count
  55. end
  56.  
  57. -- Reset ball
  58. local function resetBall()
  59.     ballX = paddleX + paddleWidth / 2
  60.     ballY = h - 3
  61.     ballDX = (math.random(2) == 1) and 1 or -1
  62.     ballDY = -1
  63.     ballActive = false
  64. end
  65.  
  66. -- Draw game
  67. local function draw()
  68.     term.setBackgroundColor(colors.black)
  69.     term.clear()
  70.    
  71.     -- Draw bricks
  72.     for row = 1, brickRows do
  73.         for col = 1, brickCols do
  74.             if bricks[row][col] then
  75.                 local x = 2 + (col - 1) * (brickWidth + 1)
  76.                 local y = brickStartY + row - 1
  77.                 term.setBackgroundColor(brickColors[row] or colors.white)
  78.                 term.setCursorPos(x, y)
  79.                 term.write(string.rep(" ", brickWidth))
  80.             end
  81.         end
  82.     end
  83.    
  84.     -- Draw paddle
  85.     term.setBackgroundColor(colors.white)
  86.     term.setCursorPos(paddleX, h - 1)
  87.     term.write(string.rep(" ", paddleWidth))
  88.    
  89.     -- Draw ball
  90.     term.setBackgroundColor(colors.black)
  91.     term.setTextColor(colors.yellow)
  92.     term.setCursorPos(math.floor(ballX), math.floor(ballY))
  93.     term.write("O")
  94.    
  95.     -- Draw info bar
  96.     term.setBackgroundColor(colors.gray)
  97.     for x = 1, w do
  98.         term.setCursorPos(x, h + 1)
  99.         term.write(" ")
  100.     end
  101.     term.setTextColor(colors.white)
  102.     term.setCursorPos(2, h + 1)
  103.     term.write("Score: " .. score .. "  Lives: " .. lives)
  104.     term.setCursorPos(w - 7, h + 1)
  105.     term.write("[Q]uit")
  106.    
  107.     -- Instructions
  108.     if not ballActive and not gameOver and not gameWon then
  109.         term.setBackgroundColor(colors.black)
  110.         term.setTextColor(colors.gray)
  111.         term.setCursorPos(math.floor(w/2) - 8, math.floor(h/2))
  112.         term.write("Press SPACE to launch")
  113.     end
  114. end
  115.  
  116. -- Update ball
  117. local function update()
  118.     if not ballActive then
  119.         ballX = paddleX + paddleWidth / 2
  120.         return
  121.     end
  122.    
  123.     ballX = ballX + ballDX
  124.     ballY = ballY + ballDY
  125.    
  126.     -- Wall collisions
  127.     if ballX <= 1 then
  128.         ballX = 1
  129.         ballDX = math.abs(ballDX)
  130.     elseif ballX >= w then
  131.         ballX = w
  132.         ballDX = -math.abs(ballDX)
  133.     end
  134.    
  135.     if ballY <= 1 then
  136.         ballY = 1
  137.         ballDY = math.abs(ballDY)
  138.     end
  139.    
  140.     -- Paddle collision
  141.     if ballY >= h - 2 and ballY <= h - 1 then
  142.         if ballX >= paddleX and ballX <= paddleX + paddleWidth then
  143.             ballDY = -math.abs(ballDY)
  144.             -- Adjust angle based on hit position
  145.             local hitPos = (ballX - paddleX) / paddleWidth
  146.             ballDX = (hitPos - 0.5) * 3
  147.             if math.abs(ballDX) < 0.5 then
  148.                 ballDX = (ballDX >= 0) and 0.5 or -0.5
  149.             end
  150.         end
  151.     end
  152.    
  153.     -- Brick collisions
  154.     for row = 1, brickRows do
  155.         for col = 1, brickCols do
  156.             if bricks[row][col] then
  157.                 local bx = 2 + (col - 1) * (brickWidth + 1)
  158.                 local by = brickStartY + row - 1
  159.                
  160.                 if ballX >= bx and ballX < bx + brickWidth and
  161.                    math.floor(ballY) == by then
  162.                     bricks[row][col] = false
  163.                     score = score + (brickRows - row + 1) * 10
  164.                     ballDY = -ballDY
  165.                    
  166.                     if countBricks() == 0 then
  167.                         gameWon = true
  168.                     end
  169.                 end
  170.             end
  171.         end
  172.     end
  173.    
  174.     -- Ball lost
  175.     if ballY > h then
  176.         lives = lives - 1
  177.         if lives <= 0 then
  178.             gameOver = true
  179.         else
  180.             resetBall()
  181.         end
  182.     end
  183. end
  184.  
  185. -- Game over screen
  186. local function showGameOver()
  187.     term.setBackgroundColor(colors.black)
  188.     term.setTextColor(colors.red)
  189.     term.setCursorPos(math.floor(w/2) - 4, math.floor(h/2))
  190.     term.write("GAME OVER")
  191.     term.setTextColor(colors.white)
  192.     term.setCursorPos(math.floor(w/2) - 6, math.floor(h/2) + 1)
  193.     term.write("Score: " .. score)
  194.     term.setCursorPos(math.floor(w/2) - 8, math.floor(h/2) + 3)
  195.     term.write("ENTER=Restart Q=Quit")
  196. end
  197.  
  198. -- Win screen
  199. local function showWin()
  200.     term.setBackgroundColor(colors.black)
  201.     term.setTextColor(colors.lime)
  202.     term.setCursorPos(math.floor(w/2) - 4, math.floor(h/2))
  203.     term.write("YOU WIN!")
  204.     term.setTextColor(colors.white)
  205.     term.setCursorPos(math.floor(w/2) - 6, math.floor(h/2) + 1)
  206.     term.write("Score: " .. score)
  207.     term.setCursorPos(math.floor(w/2) - 8, math.floor(h/2) + 3)
  208.     term.write("ENTER=Restart Q=Quit")
  209. end
  210.  
  211. -- Main game loop
  212. local function run()
  213.     initBricks()
  214.     resetBall()
  215.     score = 0
  216.     lives = 3
  217.     gameOver = false
  218.     gameWon = false
  219.     paddleX = math.floor(w/2) - 3
  220.    
  221.     while true do
  222.         draw()
  223.        
  224.         if gameOver then
  225.             showGameOver()
  226.         elseif gameWon then
  227.             showWin()
  228.         end
  229.        
  230.         local timer = os.startTimer(speed)
  231.        
  232.         while true do
  233.             local event, p1 = os.pullEvent()
  234.            
  235.             if event == "key" then
  236.                 if gameOver or gameWon then
  237.                     if p1 == keys.enter then
  238.                         initBricks()
  239.                         resetBall()
  240.                         score = 0
  241.                         lives = 3
  242.                         gameOver = false
  243.                         gameWon = false
  244.                         paddleX = math.floor(w/2) - 3
  245.                         break
  246.                     elseif p1 == keys.q then
  247.                         return
  248.                     end
  249.                 else
  250.                     if p1 == keys.left then
  251.                         paddleX = math.max(1, paddleX - 2)
  252.                     elseif p1 == keys.right then
  253.                         paddleX = math.min(w - paddleWidth, paddleX + 2)
  254.                     elseif p1 == keys.space then
  255.                         ballActive = true
  256.                     elseif p1 == keys.q then
  257.                         return
  258.                     end
  259.                 end
  260.                 if not (gameOver or gameWon) then
  261.                     draw()
  262.                 end
  263.             elseif event == "timer" and p1 == timer then
  264.                 break
  265.             end
  266.         end
  267.        
  268.         if not (gameOver or gameWon) then
  269.             update()
  270.         end
  271.     end
  272. end
  273.  
  274. run()
  275.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment