From a058476b426af70a617a1eebf60088faabd2ee97 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 12 Feb 2026 21:10:16 -0500 Subject: [PATCH] Scale 2048 to fill screen 2048.lua: - Removed hardcoded TILE_SIZE and TILE_SPACING constants - Added get_tile_size() function that calculates tile dimensions based on screen width - Added get_grid_start_x/y() to center grid - Updated draw() to use dynamic sizing for all tiles - 4x4 grid now scales proportionally on any screen size --- games/lua_examples/2048.lua | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/games/lua_examples/2048.lua b/games/lua_examples/2048.lua index 8b04f3f..0e436f3 100644 --- a/games/lua_examples/2048.lua +++ b/games/lua_examples/2048.lua @@ -9,8 +9,25 @@ local STATE_WIN = 3 -- Game constants local GRID_SIZE = 4 -local TILE_SIZE = 20 -local TILE_SPACING = 2 + +-- Calculate tile size based on screen size +local function get_tile_size() + -- Use available space with padding + local padding = math.floor(game.width() / 8) + local available = game.width() - (padding * 2) + local tile_size = math.floor(available / GRID_SIZE) + return tile_size +end + +local function get_grid_start_x() + local tile_size = get_tile_size() + local grid_width = tile_size * GRID_SIZE + return math.floor((game.width() - grid_width) / 2) +end + +local function get_grid_start_y() + return 30 +end function init() game.vars.state = STATE_MENU @@ -97,6 +114,9 @@ function draw() renderer.clear(false) local state = game.vars.state + local tile_size = get_tile_size() + local start_x = get_grid_start_x() + local start_y = get_grid_start_y() if state == STATE_MENU then renderer.text(game.width() / 2 - 15, game.height() / 2 - 30, "2048", true) @@ -104,21 +124,18 @@ function draw() elseif state == STATE_PLAYING or state == STATE_WIN or state == STATE_GAME_OVER then -- Draw grid - local start_x = (game.width() - (GRID_SIZE * (TILE_SIZE + TILE_SPACING))) / 2 - local start_y = 20 - for y = 1, GRID_SIZE do for x = 1, GRID_SIZE do - local tile_x = start_x + (x - 1) * (TILE_SIZE + TILE_SPACING) - local tile_y = start_y + (y - 1) * (TILE_SIZE + TILE_SPACING) + local tile_x = start_x + (x - 1) * tile_size + local tile_y = start_y + (y - 1) * tile_size local value = game.vars.grid[y][x] if value == 0 then -- Empty tile - renderer.rect(tile_x, tile_y, TILE_SIZE, TILE_SIZE, true, false) + renderer.rect(tile_x, tile_y, tile_size, tile_size, true, false) else -- Filled tile - renderer.rect(tile_x, tile_y, TILE_SIZE, TILE_SIZE, true, true) + renderer.rect(tile_x, tile_y, tile_size, tile_size, true, true) -- Draw value (simplified) local text = tostring(value)