ElijahCrafter

Snake

Nov 27th, 2025
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | Source Code | 0 0
  1. -- Snake 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 snake = {}
  9. local direction = "right"
  10. local food = {}
  11. local score = 0
  12. local gameOver = false
  13. local speed = 0.15
  14.  
  15. -- Initialize game
  16. local function init()
  17.     snake = {
  18.         {x = math.floor(w/2), y = math.floor(h/2)},
  19.         {x = math.floor(w/2) - 1, y = math.floor(h/2)},
  20.         {x = math.floor(w/2) - 2, y = math.floor(h/2)}
  21.     }
  22.     direction = "right"
  23.     score = 0
  24.     gameOver = false
  25.     spawnFood()
  26. end
  27.  
  28. -- Spawn food at random location
  29. function spawnFood()
  30.     local valid = false
  31.     while not valid do
  32.         food = {x = math.random(1, w), y = math.random(1, h)}
  33.         valid = true
  34.         for _, segment in ipairs(snake) do
  35.             if segment.x == food.x and segment.y == food.y then
  36.                 valid = false
  37.                 break
  38.             end
  39.         end
  40.     end
  41. end
  42.  
  43. -- Draw game
  44. local function draw()
  45.     term.setBackgroundColor(colors.black)
  46.     term.clear()
  47.    
  48.     -- Draw border
  49.     term.setBackgroundColor(colors.gray)
  50.     for x = 1, w do
  51.         term.setCursorPos(x, h + 1)
  52.         term.write(" ")
  53.     end
  54.    
  55.     -- Draw score
  56.     term.setCursorPos(1, h + 1)
  57.     term.setTextColor(colors.white)
  58.     term.write("Score: " .. score .. "  [Q]uit")
  59.    
  60.     -- Draw food
  61.     term.setBackgroundColor(colors.black)
  62.     term.setCursorPos(food.x, food.y)
  63.     term.setTextColor(colors.red)
  64.     term.write("@")
  65.    
  66.     -- Draw snake
  67.     for i, segment in ipairs(snake) do
  68.         term.setCursorPos(segment.x, segment.y)
  69.         if i == 1 then
  70.             term.setTextColor(colors.lime)
  71.             term.write("O")
  72.         else
  73.             term.setTextColor(colors.green)
  74.             term.write("o")
  75.         end
  76.     end
  77. end
  78.  
  79. -- Move snake
  80. local function move()
  81.     local head = {x = snake[1].x, y = snake[1].y}
  82.    
  83.     if direction == "up" then head.y = head.y - 1
  84.     elseif direction == "down" then head.y = head.y + 1
  85.     elseif direction == "left" then head.x = head.x - 1
  86.     elseif direction == "right" then head.x = head.x + 1
  87.     end
  88.    
  89.     -- Wrap around
  90.     if head.x < 1 then head.x = w
  91.     elseif head.x > w then head.x = 1
  92.     elseif head.y < 1 then head.y = h
  93.     elseif head.y > h then head.y = 1
  94.     end
  95.    
  96.     -- Check self collision
  97.     for _, segment in ipairs(snake) do
  98.         if segment.x == head.x and segment.y == head.y then
  99.             gameOver = true
  100.             return
  101.         end
  102.     end
  103.    
  104.     -- Add new head
  105.     table.insert(snake, 1, head)
  106.    
  107.     -- Check food
  108.     if head.x == food.x and head.y == food.y then
  109.         score = score + 10
  110.         spawnFood()
  111.         if speed > 0.05 then
  112.             speed = speed - 0.005
  113.         end
  114.     else
  115.         table.remove(snake)
  116.     end
  117. end
  118.  
  119. -- Game over screen
  120. local function showGameOver()
  121.     term.setBackgroundColor(colors.black)
  122.     term.setTextColor(colors.red)
  123.     term.setCursorPos(math.floor(w/2) - 4, math.floor(h/2))
  124.     term.write("GAME OVER")
  125.     term.setTextColor(colors.white)
  126.     term.setCursorPos(math.floor(w/2) - 6, math.floor(h/2) + 1)
  127.     term.write("Score: " .. score)
  128.     term.setCursorPos(math.floor(w/2) - 10, math.floor(h/2) + 3)
  129.     term.write("Press ENTER to restart")
  130.     term.setCursorPos(math.floor(w/2) - 7, math.floor(h/2) + 4)
  131.     term.write("Press Q to quit")
  132. end
  133.  
  134. -- Main game loop
  135. local function run()
  136.     init()
  137.    
  138.     while true do
  139.         draw()
  140.        
  141.         if gameOver then
  142.             showGameOver()
  143.             while true do
  144.                 local event, key = os.pullEvent("key")
  145.                 if key == keys.enter then
  146.                     init()
  147.                     break
  148.                 elseif key == keys.q then
  149.                     return
  150.                 end
  151.             end
  152.         end
  153.        
  154.         local timer = os.startTimer(speed)
  155.        
  156.         while true do
  157.             local event, p1 = os.pullEvent()
  158.            
  159.             if event == "key" then
  160.                 if p1 == keys.up and direction ~= "down" then
  161.                     direction = "up"
  162.                 elseif p1 == keys.down and direction ~= "up" then
  163.                     direction = "down"
  164.                 elseif p1 == keys.left and direction ~= "right" then
  165.                     direction = "left"
  166.                 elseif p1 == keys.right and direction ~= "left" then
  167.                     direction = "right"
  168.                 elseif p1 == keys.q then
  169.                     return
  170.                 end
  171.             elseif event == "timer" and p1 == timer then
  172.                 break
  173.             end
  174.         end
  175.        
  176.         move()
  177.     end
  178. end
  179.  
  180. run()
  181.  
Advertisement
Add Comment
Please, Sign In to add comment