Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local board_width, board_height = 6, 13
- local block_width, block_height = 2, 1
- local board_offset_x = 19
- local board_offset_y = 3
- local colors_list = {
- colors.red,
- colors.blue,
- colors.green,
- colors.yellow,
- colors.magenta,
- colors.cyan
- }
- local board = {}
- local current_piece = {}
- local game_over = false
- local score = 0
- local level = 1
- local fall_interval = 0.5 -- Initial fall speed (in seconds)
- -- Initialize the board
- for y = 1, board_height do
- board[y] = {}
- for x = 1, board_width do
- board[y][x] = 0
- end
- end
- -- Create buffers for double buffering
- local main_buffer = window.create(term.current(), 1, 1, 51, 19)
- local back_buffer = window.create(term.current(), 1, 1, 51, 19, false)
- -- Function to create a new piece
- local function new_piece()
- current_piece = {
- x = math.floor(board_width / 2),
- y = 1,
- colors = {
- colors_list[math.random(#colors_list)],
- colors_list[math.random(#colors_list)],
- colors_list[math.random(#colors_list)]
- }
- }
- end
- -- Function to draw the adaptive plasma sinusoid background
- local function draw_plasma_background()
- local time = os.clock() * 2
- local board_pixel_width = board_width * block_width
- local board_pixel_height = board_height * block_height
- for y = 1, board_pixel_height do
- for x = 1, board_pixel_width do
- local nx = x / board_pixel_width
- local ny = y / board_pixel_height
- local v = math.sin(nx * 10 + time) + math.sin(ny * 10 + time)
- v = (v + 2) / 4 -- Normalize to 0-1 range
- local color = math.floor(v * 7) + 8 -- Map to color range 8-15 (lighter colors)
- back_buffer.setCursorPos(board_offset_x + x - 1, board_offset_y + y - 1)
- back_buffer.setBackgroundColor(color)
- back_buffer.write(" ")
- end
- end
- end
- -- Function to draw a block
- local function draw_block(x, y, color)
- back_buffer.setBackgroundColor(color)
- for by = 0, block_height - 1 do
- for bx = 0, block_width - 1 do
- back_buffer.setCursorPos(board_offset_x + (x-1)*block_width + bx, board_offset_y + (y-1)*block_height + by)
- back_buffer.write(" ")
- end
- end
- end
- -- Function to draw the game board
- local function draw_board()
- back_buffer.setBackgroundColor(colors.gray)
- back_buffer.clear()
- draw_plasma_background()
- -- Draw the board contents
- for y = 1, board_height do
- for x = 1, board_width do
- if board[y][x] ~= 0 then
- draw_block(x, y, board[y][x])
- end
- end
- end
- -- Draw the current piece
- for i = 1, 3 do
- draw_block(current_piece.x, current_piece.y + i - 1, current_piece.colors[i])
- end
- -- Draw score and level
- back_buffer.setTextColor(colors.white)
- back_buffer.setBackgroundColor(colors.gray)
- back_buffer.setCursorPos(1, 1)
- back_buffer.write("Score: " .. score)
- back_buffer.setCursorPos(1, 2)
- back_buffer.write("Level: " .. level)
- -- Swap buffers
- back_buffer.setVisible(true)
- main_buffer.setVisible(false)
- main_buffer, back_buffer = back_buffer, main_buffer
- end
- -- Function to check for matches
- local function check_matches()
- local matches = {}
- -- Check vertical matches
- for x = 1, board_width do
- for y = 1, board_height - 2 do
- if board[y][x] ~= 0 and board[y][x] == board[y+1][x] and board[y][x] == board[y+2][x] then
- matches[#matches+1] = {y, x}
- matches[#matches+1] = {y+1, x}
- matches[#matches+1] = {y+2, x}
- end
- end
- end
- -- Check horizontal matches
- for y = 1, board_height do
- for x = 1, board_width - 2 do
- if board[y][x] ~= 0 and board[y][x] == board[y][x+1] and board[y][x] == board[y][x+2] then
- matches[#matches+1] = {y, x}
- matches[#matches+1] = {y, x+1}
- matches[#matches+1] = {y, x+2}
- end
- end
- end
- -- Check diagonal matches (top-left to bottom-right)
- for y = 1, board_height - 2 do
- for x = 1, board_width - 2 do
- if board[y][x] ~= 0 and board[y][x] == board[y+1][x+1] and board[y][x] == board[y+2][x+2] then
- matches[#matches+1] = {y, x}
- matches[#matches+1] = {y+1, x+1}
- matches[#matches+1] = {y+2, x+2}
- end
- end
- end
- -- Check diagonal matches (top-right to bottom-left)
- for y = 1, board_height - 2 do
- for x = 3, board_width do
- if board[y][x] ~= 0 and board[y][x] == board[y+1][x-1] and board[y][x] == board[y+2][x-2] then
- matches[#matches+1] = {y, x}
- matches[#matches+1] = {y+1, x-1}
- matches[#matches+1] = {y+2, x-2}
- end
- end
- end
- -- Remove matches and update score
- for _, match in ipairs(matches) do
- board[match[1]][match[2]] = 0
- score = score + 10
- end
- -- Move blocks down
- for x = 1, board_width do
- local write_y = board_height
- for y = board_height, 1, -1 do
- if board[y][x] ~= 0 then
- board[write_y][x] = board[y][x]
- if write_y ~= y then
- board[y][x] = 0
- end
- write_y = write_y - 1
- end
- end
- end
- -- Update level and fall speed
- level = math.floor(score / 100) + 1
- fall_interval = math.max(0.1, 0.5 - (level - 1) * 0.05)
- return #matches > 0
- end
- -- Function to move the piece down
- local function move_piece_down()
- if current_piece.y + 2 >= board_height or board[current_piece.y+3][current_piece.x] ~= 0 then
- -- Place piece on board
- for i = 1, 3 do
- if current_piece.y + i - 1 <= board_height then
- board[current_piece.y+i-1][current_piece.x] = current_piece.colors[i]
- end
- end
- -- Check for matches
- while check_matches() do
- draw_board()
- os.sleep(0.2)
- end
- -- Create new piece
- new_piece()
- -- Check for game over
- if board[1][current_piece.x] ~= 0 then
- game_over = true
- end
- return true -- Piece has landed
- else
- current_piece.y = current_piece.y + 1
- return false -- Piece is still falling
- end
- end
- -- Function to handle user input and game updates
- local function game_loop()
- local last_fall_time = os.clock()
- local last_draw_time = os.clock()
- while not game_over do
- -- Handle input
- local timer = os.startTimer(0.05)
- local event, param = os.pullEvent()
- if event == "key" then
- local key = param
- if key == keys.left and current_piece.x > 1 and
- board[current_piece.y][current_piece.x-1] == 0 and
- board[current_piece.y+1][current_piece.x-1] == 0 and
- board[current_piece.y+2][current_piece.x-1] == 0 then
- current_piece.x = current_piece.x - 1
- elseif key == keys.right and current_piece.x < board_width and
- board[current_piece.y][current_piece.x+1] == 0 and
- board[current_piece.y+1][current_piece.x+1] == 0 and
- board[current_piece.y+2][current_piece.x+1] == 0 then
- current_piece.x = current_piece.x + 1
- elseif key == keys.down then
- move_piece_down()
- elseif key == keys.up then
- table.insert(current_piece.colors, table.remove(current_piece.colors, 1))
- end
- end
- -- Update game state
- local current_time = os.clock()
- if current_time - last_fall_time >= fall_interval then
- move_piece_down()
- last_fall_time = current_time
- end
- -- Redraw the board (limit to 30 FPS)
- if current_time - last_draw_time >= 1/30 then
- draw_board()
- last_draw_time = current_time
- end
- end
- end
- -- Main game function
- local function play_game()
- new_piece()
- draw_board()
- game_loop()
- -- Game over screen
- main_buffer.setBackgroundColor(colors.black)
- main_buffer.clear()
- main_buffer.setCursorPos(1, 8)
- main_buffer.setTextColor(colors.white)
- main_buffer.write("Game Over!")
- main_buffer.setCursorPos(1, 10)
- main_buffer.write("Final Score: " .. score)
- main_buffer.setCursorPos(1, 11)
- main_buffer.write("Final Level: " .. level)
- os.sleep(3)
- end
- -- Start the game
- play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement