ElijahCrafter

2048

Nov 27th, 2025
777
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | Source Code | 0 0
  1. -- 2048 Game for SimpleOS
  2. -- CC: Tweaked compatible
  3.  
  4. local w, h = term.getSize()
  5.  
  6. -- Game settings
  7. local gridSize = 4
  8. local cellWidth = 6
  9. local startX = math.floor((w - gridSize * cellWidth) / 2)
  10. local startY = 4
  11.  
  12. -- Game state
  13. local grid = {}
  14. local score = 0
  15. local bestScore = 0
  16. local gameOver = false
  17. local gameWon = false
  18.  
  19. -- Colors for tiles
  20. local tileColors = {
  21.     [2] = colors.white,
  22.     [4] = colors.lightGray,
  23.     [8] = colors.orange,
  24.     [16] = colors.orange,
  25.     [32] = colors.red,
  26.     [64] = colors.red,
  27.     [128] = colors.yellow,
  28.     [256] = colors.yellow,
  29.     [512] = colors.yellow,
  30.     [1024] = colors.yellow,
  31.     [2048] = colors.lime
  32. }
  33.  
  34. local textColors = {
  35.     [2] = colors.black,
  36.     [4] = colors.black,
  37.     [8] = colors.white,
  38.     [16] = colors.white,
  39.     [32] = colors.white,
  40.     [64] = colors.white,
  41.     [128] = colors.white,
  42.     [256] = colors.white,
  43.     [512] = colors.white,
  44.     [1024] = colors.white,
  45.     [2048] = colors.white
  46. }
  47.  
  48. -- Initialize grid
  49. local function initGrid()
  50.     grid = {}
  51.     for y = 1, gridSize do
  52.         grid[y] = {}
  53.         for x = 1, gridSize do
  54.             grid[y][x] = 0
  55.         end
  56.     end
  57.     score = 0
  58.     gameOver = false
  59.     gameWon = false
  60. end
  61.  
  62. -- Add random tile
  63. local function addRandomTile()
  64.     local empty = {}
  65.     for y = 1, gridSize do
  66.         for x = 1, gridSize do
  67.             if grid[y][x] == 0 then
  68.                 table.insert(empty, {x = x, y = y})
  69.             end
  70.         end
  71.     end
  72.    
  73.     if #empty > 0 then
  74.         local pos = empty[math.random(#empty)]
  75.         grid[pos.y][pos.x] = math.random(10) == 1 and 4 or 2
  76.     end
  77. end
  78.  
  79. -- Check if moves available
  80. local function movesAvailable()
  81.     for y = 1, gridSize do
  82.         for x = 1, gridSize do
  83.             if grid[y][x] == 0 then return true end
  84.             if x < gridSize and grid[y][x] == grid[y][x+1] then return true end
  85.             if y < gridSize and grid[y][x] == grid[y+1][x] then return true end
  86.         end
  87.     end
  88.     return false
  89. end
  90.  
  91. -- Move tiles in direction
  92. local function move(dx, dy)
  93.     local moved = false
  94.     local merged = {}
  95.     for y = 1, gridSize do
  96.         merged[y] = {}
  97.         for x = 1, gridSize do
  98.             merged[y][x] = false
  99.         end
  100.     end
  101.    
  102.     local startX, endX, stepX = 1, gridSize, 1
  103.     local startY, endY, stepY = 1, gridSize, 1
  104.    
  105.     if dx > 0 then startX, endX, stepX = gridSize, 1, -1 end
  106.     if dy > 0 then startY, endY, stepY = gridSize, 1, -1 end
  107.    
  108.     for y = startY, endY, stepY do
  109.         for x = startX, endX, stepX do
  110.             if grid[y][x] ~= 0 then
  111.                 local newX, newY = x, y
  112.                 while true do
  113.                     local nextX = newX + dx
  114.                     local nextY = newY + dy
  115.                     if nextX < 1 or nextX > gridSize or nextY < 1 or nextY > gridSize then
  116.                         break
  117.                     end
  118.                     if grid[nextY][nextX] == 0 then
  119.                         newX, newY = nextX, nextY
  120.                     elseif grid[nextY][nextX] == grid[y][x] and not merged[nextY][nextX] then
  121.                         newX, newY = nextX, nextY
  122.                         merged[newY][newX] = true
  123.                         break
  124.                     else
  125.                         break
  126.                     end
  127.                 end
  128.                
  129.                 if newX ~= x or newY ~= y then
  130.                     moved = true
  131.                     if grid[newY][newX] == grid[y][x] then
  132.                         grid[newY][newX] = grid[y][x] * 2
  133.                         score = score + grid[newY][newX]
  134.                         if score > bestScore then bestScore = score end
  135.                         if grid[newY][newX] == 2048 then gameWon = true end
  136.                     else
  137.                         grid[newY][newX] = grid[y][x]
  138.                     end
  139.                     grid[y][x] = 0
  140.                 end
  141.             end
  142.         end
  143.     end
  144.    
  145.     return moved
  146. end
  147.  
  148. -- Draw a tile
  149. local function drawTile(x, y, value)
  150.     local screenX = startX + (x - 1) * cellWidth
  151.     local screenY = startY + (y - 1) * 2
  152.    
  153.     local bgColor = tileColors[value] or colors.purple
  154.     local fgColor = textColors[value] or colors.white
  155.     if value == 0 then bgColor = colors.gray end
  156.    
  157.     term.setBackgroundColor(bgColor)
  158.     term.setCursorPos(screenX, screenY)
  159.     term.write(string.rep(" ", cellWidth))
  160.     term.setCursorPos(screenX, screenY + 1)
  161.     term.write(string.rep(" ", cellWidth))
  162.    
  163.     if value > 0 then
  164.         term.setTextColor(fgColor)
  165.         local str = tostring(value)
  166.         local offset = math.floor((cellWidth - #str) / 2)
  167.         term.setCursorPos(screenX + offset, screenY)
  168.         term.write(str)
  169.     end
  170. end
  171.  
  172. -- Draw game
  173. local function draw()
  174.     term.setBackgroundColor(colors.black)
  175.     term.clear()
  176.    
  177.     -- Title
  178.     term.setTextColor(colors.yellow)
  179.     term.setCursorPos(math.floor(w/2) - 2, 1)
  180.     term.write("2048")
  181.    
  182.     -- Score
  183.     term.setTextColor(colors.white)
  184.     term.setCursorPos(2, 2)
  185.     term.write("Score: " .. score)
  186.     term.setCursorPos(w - 12, 2)
  187.     term.write("Best: " .. bestScore)
  188.    
  189.     -- Draw grid
  190.     for y = 1, gridSize do
  191.         for x = 1, gridSize do
  192.             drawTile(x, y, grid[y][x])
  193.         end
  194.     end
  195.    
  196.     -- Controls
  197.     term.setBackgroundColor(colors.black)
  198.     term.setTextColor(colors.gray)
  199.     term.setCursorPos(2, startY + gridSize * 2 + 1)
  200.     term.write("Arrow keys: Move  [Q] Quit  [R] Restart")
  201. end
  202.  
  203. -- Game over screen
  204. local function showGameOver()
  205.     term.setBackgroundColor(colors.black)
  206.     term.setTextColor(colors.red)
  207.     term.setCursorPos(math.floor(w/2) - 4, startY + gridSize)
  208.     term.write("GAME OVER!")
  209. end
  210.  
  211. -- Win screen
  212. local function showWin()
  213.     term.setBackgroundColor(colors.black)
  214.     term.setTextColor(colors.lime)
  215.     term.setCursorPos(math.floor(w/2) - 4, startY + gridSize)
  216.     term.write("YOU WIN!")
  217. end
  218.  
  219. -- Main game loop
  220. local function run()
  221.     initGrid()
  222.     addRandomTile()
  223.     addRandomTile()
  224.    
  225.     while true do
  226.         draw()
  227.        
  228.         if not movesAvailable() then
  229.             gameOver = true
  230.             showGameOver()
  231.         elseif gameWon then
  232.             showWin()
  233.         end
  234.        
  235.         local event, key = os.pullEvent("key")
  236.        
  237.         local moved = false
  238.         if key == keys.up then
  239.             moved = move(0, -1)
  240.         elseif key == keys.down then
  241.             moved = move(0, 1)
  242.         elseif key == keys.left then
  243.             moved = move(-1, 0)
  244.         elseif key == keys.right then
  245.             moved = move(1, 0)
  246.         elseif key == keys.r then
  247.             initGrid()
  248.             addRandomTile()
  249.             addRandomTile()
  250.         elseif key == keys.q then
  251.             return
  252.         end
  253.        
  254.         if moved then
  255.             addRandomTile()
  256.         end
  257.     end
  258. end
  259.  
  260. run()
  261.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment