Advertisement
nonogamer9

nono2048 (OpenComputers Port)

Mar 17th, 2024 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.73 KB | Gaming | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local event = require("event")
  4. local gpu = component.gpu
  5. -- Check if the beep component is available
  6. local beep = component.isAvailable("beep") and component.beep or nil
  7.  
  8. -- Define the melody frequencies and durations
  9. local melody = {
  10.     {frequency = 440, duration = 0.2},  -- A4 for 0.2 seconds
  11. }
  12.  
  13. -- Function to draw the game board
  14. local function drawBoard(board)
  15.     term.clear()
  16.     local width, height = gpu.getResolution()
  17.     local cellWidth = math.floor(width / 4)
  18.     local cellHeight = math.floor(height / 4)
  19.    
  20.     -- Set background color
  21.     gpu.setBackground(0xBBBBBB)
  22.     gpu.fill(1, 1, width, height, " ")
  23.  
  24.     for y = 1, 4 do
  25.         for x = 1, 4 do
  26.             local value = board[y][x]
  27.             local xPos = (x - 1) * cellWidth
  28.             local yPos = (y - 1) * cellHeight
  29.  
  30.             gpu.setBackground(0xCCCCCC)
  31.             gpu.fill(xPos, yPos, cellWidth, cellHeight, " ")
  32.  
  33.             if value > 0 then
  34.                 local colors = {
  35.                     [2] = 0xFFFF00,      -- Yellow
  36.                     [4] = 0xFFA500,      -- Orange
  37.                     [8] = 0xFF0000,      -- Red
  38.                     [16] = 0xFF00FF,     -- Magenta
  39.                     [32] = 0x800080,     -- Purple
  40.                     [64] = 0x0000FF,     -- Blue
  41.                     [128] = 0xADD8E6,    -- Light Blue
  42.                     [256] = 0x00FFFF,    -- Cyan
  43.                     [512] = 0x00FF00,    -- Lime
  44.                     [1024] = 0x008000,   -- Green
  45.                     [2048] = 0xFFFFFF    -- White
  46.                 }
  47.                 gpu.setBackground(colors[value])
  48.                 gpu.fill(xPos + 1, yPos + 1, cellWidth - 2, cellHeight - 2, " ")
  49.                 gpu.setForeground(0x000000)
  50.                 gpu.set(xPos + math.floor(cellWidth / 2) - math.floor(string.len(tostring(value)) / 2), yPos + math.floor(cellHeight / 2), tostring(value))
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. -- Function to initialize the game board
  57. local function initializeBoard()
  58.     local board = {}
  59.     for i = 1, 4 do
  60.         board[i] = {}
  61.         for j = 1, 4 do
  62.             board[i][j] = 0
  63.         end
  64.     end
  65.     return board
  66. end
  67.  
  68. -- Function to add a new random tile (2 or 4) to the board
  69. local function addRandomTile(board)
  70.     local emptyCells = {}
  71.     for y = 1, 4 do
  72.         for x = 1, 4 do
  73.             if board[y][x] == 0 then
  74.                 table.insert(emptyCells, {x, y})
  75.             end
  76.         end
  77.     end
  78.     if #emptyCells > 0 then
  79.         local randomCell = emptyCells[math.random(#emptyCells)]
  80.         local value = math.random() < 0.9 and 2 or 4
  81.         board[randomCell[2]][randomCell[1]] = value
  82.         return randomCell[1], randomCell[2], value
  83.     end
  84.     return nil, nil, nil
  85. end
  86.  
  87. -- Function to check if the game is over
  88. local function isGameOver(board)
  89.     for y = 1, 4 do
  90.         for x = 1, 4 do
  91.             if board[y][x] == 0 then
  92.                 return false
  93.             end
  94.             if x < 4 and board[y][x] == board[y][x + 1] then
  95.                 return false
  96.             end
  97.             if y < 4 and board[y][x] == board[y + 1][x] then
  98.                 return false
  99.             end
  100.         end
  101.     end
  102.     return true
  103. end
  104.  
  105. -- Function to perform a move (up, down, left, right)
  106. local function move(board, direction)
  107.     local function compress(row)
  108.         local newRow = {}
  109.         for _, value in ipairs(row) do
  110.             if value > 0 then
  111.                 table.insert(newRow, value)
  112.             end
  113.         end
  114.         while #newRow < 4 do
  115.             table.insert(newRow, 0)
  116.         end
  117.         return newRow
  118.     end
  119.  
  120.     local function merge(row)
  121.         local newRow = {}
  122.         local score = 0
  123.         local merged = {}
  124.         for i = 1, 4 do
  125.             if row[i] > 0 then
  126.                 if newRow[#newRow] == row[i] and not merged[i - 1] then
  127.                     newRow[#newRow] = row[i] * 2
  128.                     score = score + row[i] * 2
  129.                     merged[i - 1] = true
  130.                 else
  131.                     table.insert(newRow, row[i])
  132.                 end
  133.             end
  134.         end
  135.         while #newRow < 4 do
  136.             table.insert(newRow, 0)
  137.         end
  138.         return newRow, score
  139.     end
  140.  
  141.     local function reverse(row)
  142.         local reversedRow = {}
  143.         for i = #row, 1, -1 do
  144.             table.insert(reversedRow, row[i])
  145.         end
  146.         return reversedRow
  147.     end
  148.  
  149.     local function transpose(board)
  150.         local transposedBoard = {}
  151.         for i = 1, 4 do
  152.             transposedBoard[i] = {}
  153.             for j = 1, 4 do
  154.                 transposedBoard[i][j] = board[j][i]
  155.             end
  156.         end
  157.         return transposedBoard
  158.     end
  159.  
  160.     local function applyMove(board, direction)
  161.         local score = 0
  162.         if direction == 1 then -- Up
  163.             board = transpose(board)
  164.             for i = 1, 4 do
  165.                 local newRow, rowScore = merge(board[i])
  166.                 score = score + rowScore
  167.                 board[i] = newRow
  168.             end
  169.             board = transpose(board)
  170.         elseif direction == 2 then -- Down
  171.             board = transpose(board)
  172.             for i = 1, 4 do
  173.                 local newRow, rowScore = merge(reverse(board[i]))
  174.                 score = score + rowScore
  175.                 board[i] = reverse(newRow)
  176.             end
  177.             board = transpose(board)
  178.         elseif direction == 3 then -- Left
  179.             for i = 1, 4 do
  180.                 local newRow, rowScore = merge(board[i])
  181.                 score = score + rowScore
  182.                 board[i] = newRow
  183.             end
  184.         elseif direction == 4 then -- Right
  185.             for i = 1, 4 do
  186.                 local newRow, rowScore = merge(reverse(board[i]))
  187.                 score = score + rowScore
  188.                 board[i] = reverse(newRow)
  189.             end
  190.         end
  191.         return board, score
  192.     end
  193.  
  194.     local newBoard, score = applyMove(board, direction)
  195.     return newBoard, score
  196. end
  197.  
  198. -- Function to draw the rainbow "nono2048" logo
  199. local function drawLogo(x, y)
  200.     local colors = {
  201.         0xFF0000, -- Red
  202.         0xFF7F00, -- Orange
  203.         0xFFFF00, -- Yellow
  204.         0x00FF00, -- Green
  205.         0x0000FF, -- Blue
  206.         0x4B0082, -- Indigo
  207.         0x9400D3  -- Violet
  208.     }
  209.     local logo = "nono2048"
  210.     local offset = 0
  211.     for i = 1, #logo do
  212.         local char = string.sub(logo, i, i)
  213.         gpu.setForeground(colors[(i + offset) % #colors + 1])
  214.         gpu.setBackground(0x888888)  -- Set background color to gray
  215.         gpu.set(x + i - 1, y, char)
  216.         offset = offset + 1
  217.     end
  218. end
  219.  
  220. -- Function to play the melody
  221. local function playMelody()
  222.     if beep then
  223.         for _, note in ipairs(melody) do
  224.             local frequencies = {}
  225.             frequencies[note.frequency] = note.duration
  226.             beep.beep(frequencies)
  227.             os.sleep(0.1) -- Delay between notes (optional)
  228.         end
  229.     else
  230.         print("Beep component not available, unable to play sound.")
  231.     end
  232. end
  233.  
  234. -- Main function to run the game
  235. local function main()
  236.     local board = initializeBoard()
  237.     local score = 0
  238.     addRandomTile(board)
  239.     addRandomTile(board)
  240.     drawBoard(board)
  241.  
  242.     local width, height = gpu.getResolution()
  243.     gpu.setForeground(0x000000)
  244.     gpu.set(1, height, "Score: " .. score)
  245.  
  246.     -- Draw the logo in the bottom right corner
  247.     local logoX = width - 8
  248.     local logoY = height - 1
  249.     drawLogo(logoX, logoY)
  250.  
  251.     -- Main game loop
  252.     while not isGameOver(board) do
  253.         local _, _, char, code = event.pull("key_down")
  254.         local moved = false
  255.         local moveScore = 0
  256.         if code == 200 then -- Up arrow key
  257.             board, moveScore = move(board, 1)
  258.             moved = true
  259.         elseif code == 208 then -- Down arrow key
  260.             board, moveScore = move(board, 2)
  261.             moved = true
  262.         elseif code == 203 then -- Left arrow key
  263.             board, moveScore = move(board, 3)
  264.             moved = true
  265.         elseif code == 205 then -- Right arrow key
  266.             board, moveScore = move(board, 4)
  267.             moved = true
  268.         elseif code == 46 or code == 57 then -- X or NumPad5 to exit the game
  269.             break
  270.         end
  271.         if moved then
  272.             addRandomTile(board)
  273.             score = score + moveScore
  274.             drawBoard(board)
  275.             gpu.setForeground(0x000000)
  276.             gpu.set(1, height, "Score: " .. score)
  277.             -- Play sound effect
  278.             playMelody()
  279.             -- Redraw the logo to ensure it stays on the screen
  280.             drawLogo(logoX, logoY)
  281.         end
  282.     end
  283.  
  284.     print("Game over! Final score:", score)
  285. end
  286.  
  287. -- Start the game
  288. main()
  289.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement