Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local term = require("term")
- local event = require("event")
- local gpu = component.gpu
- -- Check if the beep component is available
- local beep = component.isAvailable("beep") and component.beep or nil
- -- Define the melody frequencies and durations
- local melody = {
- {frequency = 440, duration = 0.2}, -- A4 for 0.2 seconds
- }
- -- Function to draw the game board
- local function drawBoard(board)
- term.clear()
- local width, height = gpu.getResolution()
- local cellWidth = math.floor(width / 4)
- local cellHeight = math.floor(height / 4)
- -- Set background color
- gpu.setBackground(0xBBBBBB)
- gpu.fill(1, 1, width, height, " ")
- for y = 1, 4 do
- for x = 1, 4 do
- local value = board[y][x]
- local xPos = (x - 1) * cellWidth
- local yPos = (y - 1) * cellHeight
- gpu.setBackground(0xCCCCCC)
- gpu.fill(xPos, yPos, cellWidth, cellHeight, " ")
- if value > 0 then
- local colors = {
- [2] = 0xFFFF00, -- Yellow
- [4] = 0xFFA500, -- Orange
- [8] = 0xFF0000, -- Red
- [16] = 0xFF00FF, -- Magenta
- [32] = 0x800080, -- Purple
- [64] = 0x0000FF, -- Blue
- [128] = 0xADD8E6, -- Light Blue
- [256] = 0x00FFFF, -- Cyan
- [512] = 0x00FF00, -- Lime
- [1024] = 0x008000, -- Green
- [2048] = 0xFFFFFF -- White
- }
- gpu.setBackground(colors[value])
- gpu.fill(xPos + 1, yPos + 1, cellWidth - 2, cellHeight - 2, " ")
- gpu.setForeground(0x000000)
- gpu.set(xPos + math.floor(cellWidth / 2) - math.floor(string.len(tostring(value)) / 2), yPos + math.floor(cellHeight / 2), tostring(value))
- end
- end
- end
- end
- -- Function to initialize the game board
- local function initializeBoard()
- local board = {}
- for i = 1, 4 do
- board[i] = {}
- for j = 1, 4 do
- board[i][j] = 0
- end
- end
- return board
- end
- -- Function to add a new random tile (2 or 4) to the board
- local function addRandomTile(board)
- local emptyCells = {}
- for y = 1, 4 do
- for x = 1, 4 do
- if board[y][x] == 0 then
- table.insert(emptyCells, {x, y})
- end
- end
- end
- if #emptyCells > 0 then
- local randomCell = emptyCells[math.random(#emptyCells)]
- local value = math.random() < 0.9 and 2 or 4
- board[randomCell[2]][randomCell[1]] = value
- return randomCell[1], randomCell[2], value
- end
- return nil, nil, nil
- end
- -- Function to check if the game is over
- local function isGameOver(board)
- for y = 1, 4 do
- for x = 1, 4 do
- if board[y][x] == 0 then
- return false
- end
- if x < 4 and board[y][x] == board[y][x + 1] then
- return false
- end
- if y < 4 and board[y][x] == board[y + 1][x] then
- return false
- end
- end
- end
- return true
- end
- -- Function to perform a move (up, down, left, right)
- local function move(board, direction)
- local function compress(row)
- local newRow = {}
- for _, value in ipairs(row) do
- if value > 0 then
- table.insert(newRow, value)
- end
- end
- while #newRow < 4 do
- table.insert(newRow, 0)
- end
- return newRow
- end
- local function merge(row)
- local newRow = {}
- local score = 0
- local merged = {}
- for i = 1, 4 do
- if row[i] > 0 then
- if newRow[#newRow] == row[i] and not merged[i - 1] then
- newRow[#newRow] = row[i] * 2
- score = score + row[i] * 2
- merged[i - 1] = true
- else
- table.insert(newRow, row[i])
- end
- end
- end
- while #newRow < 4 do
- table.insert(newRow, 0)
- end
- return newRow, score
- end
- local function reverse(row)
- local reversedRow = {}
- for i = #row, 1, -1 do
- table.insert(reversedRow, row[i])
- end
- return reversedRow
- end
- local function transpose(board)
- local transposedBoard = {}
- for i = 1, 4 do
- transposedBoard[i] = {}
- for j = 1, 4 do
- transposedBoard[i][j] = board[j][i]
- end
- end
- return transposedBoard
- end
- local function applyMove(board, direction)
- local score = 0
- if direction == 1 then -- Up
- board = transpose(board)
- for i = 1, 4 do
- local newRow, rowScore = merge(board[i])
- score = score + rowScore
- board[i] = newRow
- end
- board = transpose(board)
- elseif direction == 2 then -- Down
- board = transpose(board)
- for i = 1, 4 do
- local newRow, rowScore = merge(reverse(board[i]))
- score = score + rowScore
- board[i] = reverse(newRow)
- end
- board = transpose(board)
- elseif direction == 3 then -- Left
- for i = 1, 4 do
- local newRow, rowScore = merge(board[i])
- score = score + rowScore
- board[i] = newRow
- end
- elseif direction == 4 then -- Right
- for i = 1, 4 do
- local newRow, rowScore = merge(reverse(board[i]))
- score = score + rowScore
- board[i] = reverse(newRow)
- end
- end
- return board, score
- end
- local newBoard, score = applyMove(board, direction)
- return newBoard, score
- end
- -- Function to draw the rainbow "nono2048" logo
- local function drawLogo(x, y)
- local colors = {
- 0xFF0000, -- Red
- 0xFF7F00, -- Orange
- 0xFFFF00, -- Yellow
- 0x00FF00, -- Green
- 0x0000FF, -- Blue
- 0x4B0082, -- Indigo
- 0x9400D3 -- Violet
- }
- local logo = "nono2048"
- local offset = 0
- for i = 1, #logo do
- local char = string.sub(logo, i, i)
- gpu.setForeground(colors[(i + offset) % #colors + 1])
- gpu.setBackground(0x888888) -- Set background color to gray
- gpu.set(x + i - 1, y, char)
- offset = offset + 1
- end
- end
- -- Function to play the melody
- local function playMelody()
- if beep then
- for _, note in ipairs(melody) do
- local frequencies = {}
- frequencies[note.frequency] = note.duration
- beep.beep(frequencies)
- os.sleep(0.1) -- Delay between notes (optional)
- end
- else
- print("Beep component not available, unable to play sound.")
- end
- end
- -- Main function to run the game
- local function main()
- local board = initializeBoard()
- local score = 0
- addRandomTile(board)
- addRandomTile(board)
- drawBoard(board)
- local width, height = gpu.getResolution()
- gpu.setForeground(0x000000)
- gpu.set(1, height, "Score: " .. score)
- -- Draw the logo in the bottom right corner
- local logoX = width - 8
- local logoY = height - 1
- drawLogo(logoX, logoY)
- -- Main game loop
- while not isGameOver(board) do
- local _, _, char, code = event.pull("key_down")
- local moved = false
- local moveScore = 0
- if code == 200 then -- Up arrow key
- board, moveScore = move(board, 1)
- moved = true
- elseif code == 208 then -- Down arrow key
- board, moveScore = move(board, 2)
- moved = true
- elseif code == 203 then -- Left arrow key
- board, moveScore = move(board, 3)
- moved = true
- elseif code == 205 then -- Right arrow key
- board, moveScore = move(board, 4)
- moved = true
- elseif code == 46 or code == 57 then -- X or NumPad5 to exit the game
- break
- end
- if moved then
- addRandomTile(board)
- score = score + moveScore
- drawBoard(board)
- gpu.setForeground(0x000000)
- gpu.set(1, height, "Score: " .. score)
- -- Play sound effect
- playMelody()
- -- Redraw the logo to ensure it stays on the screen
- drawLogo(logoX, logoY)
- end
- end
- print("Game over! Final score:", score)
- end
- -- Start the game
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement