diff --git a/games/lua_examples/simon_says.lua b/games/lua_examples/simon_says.lua index efcd144..57a7d3c 100644 --- a/games/lua_examples/simon_says.lua +++ b/games/lua_examples/simon_says.lua @@ -8,18 +8,22 @@ local STATE_SHOWING = 2 local STATE_GAME_OVER = 3 -- Game constants -local BUTTON_SIZE = 40 -local BUTTON_SPACING = 10 local SHOW_DURATION = 30 -- Frames to show each button local WAIT_DURATION = 20 -- Frames between shows --- Button positions (4 buttons in grid) -local BUTTONS = { - {x = 20, y = 20, color = 1}, -- Top-left - {x = 80, y = 20, color = 2}, -- Top-right - {x = 20, y = 80, color = 3}, -- Bottom-left - {x = 80, y = 80, color = 4} -- Bottom-right -} +-- Button positions (calculated dynamically based on screen size) +local function get_buttons() + local padding = math.floor(game.width() / 10) + local button_size = math.floor((game.width() - padding * 3) / 2) + local spacing = padding + + return { + {x = padding, y = padding, size = button_size, color = 1}, -- Top-left + {x = padding + button_size + spacing, y = padding, size = button_size, color = 2}, -- Top-right + {x = padding, y = padding + button_size + spacing, size = button_size, color = 3}, -- Bottom-left + {x = padding + button_size + spacing, y = padding + button_size + spacing, size = button_size, color = 4} -- Bottom-right + } +end function init() game.vars.state = STATE_MENU @@ -140,6 +144,7 @@ function draw() renderer.clear(false) -- Black background local state = game.vars.state + local buttons = get_buttons() if state == STATE_MENU then renderer.text(game.width() / 2 - 40, game.height() / 2 - 40, "SIMON SAYS", true) @@ -149,9 +154,9 @@ function draw() else -- Draw buttons with highlight for i = 1, 4 do - local btn = BUTTONS[i] + local btn = buttons[i] local filled = (game.vars.show_button == i) - renderer.rect(btn.x, btn.y, BUTTON_SIZE, BUTTON_SIZE, true, filled) + renderer.rect(btn.x, btn.y, btn.size, btn.size, true, filled) end -- Draw score @@ -170,10 +175,11 @@ function draw() end function get_button_at(x, y) + local buttons = get_buttons() for i = 1, 4 do - local btn = BUTTONS[i] - if x >= btn.x and x < btn.x + BUTTON_SIZE and - y >= btn.y and y < btn.y + BUTTON_SIZE then + local btn = buttons[i] + if x >= btn.x and x < btn.x + btn.size and + y >= btn.y and y < btn.y + btn.size then return i end end diff --git a/games/lua_examples/tic_tac_toe.lua b/games/lua_examples/tic_tac_toe.lua index 470d645..77740ef 100644 --- a/games/lua_examples/tic_tac_toe.lua +++ b/games/lua_examples/tic_tac_toe.lua @@ -8,8 +8,29 @@ local STATE_GAME_OVER = 2 -- Game constants local GRID_SIZE = 3 -local CELL_SIZE = 30 -local CELL_SPACING = 2 + +-- Calculate cell size based on screen size +local function get_cell_size() + -- Use available space minus padding + local padding = math.floor(game.width() / 8) + local available = game.width() - (padding * 2) + local cell_size = math.floor(available / GRID_SIZE) + return cell_size +end + +local function get_grid_start_y() + -- Center grid vertically with some top margin + local cell_size = get_cell_size() + local grid_height = cell_size * GRID_SIZE + 2 * 2 -- spacing + return math.floor((game.height() - grid_height) / 2) +end + +local function get_grid_start_x() + -- Center grid horizontally + local cell_size = get_cell_size() + local grid_width = cell_size * GRID_SIZE + 2 * 2 -- spacing + return math.floor((game.width() - grid_width) / 2) +end function init() game.vars.state = STATE_MENU @@ -100,6 +121,10 @@ function draw() renderer.clear(false) local state = game.vars.state + local cell_size = get_cell_size() + local start_x = get_grid_start_x() + local start_y = get_grid_start_y() + local cell_spacing = 2 if state == STATE_MENU then renderer.text(game.width() / 2 - 50, game.height() / 2 - 30, "TIC-TAC-TOE", true) @@ -108,27 +133,24 @@ function draw() elseif state == STATE_PLAYING or state == STATE_GAME_OVER then -- Draw grid - local start_x = (game.width() - (GRID_SIZE * (CELL_SIZE + CELL_SPACING))) / 2 - local start_y = 30 - for row = 0, GRID_SIZE - 1 do for col = 0, GRID_SIZE - 1 do local cell_idx = row * GRID_SIZE + col + 1 - local cell_x = start_x + col * (CELL_SIZE + CELL_SPACING) - local cell_y = start_y + row * (CELL_SIZE + CELL_SPACING) + local cell_x = start_x + col * (cell_size + cell_spacing) + local cell_y = start_y + row * (cell_size + cell_spacing) -- Draw cell - renderer.rect(cell_x, cell_y, CELL_SIZE, CELL_SIZE, true, false) + renderer.rect(cell_x, cell_y, cell_size, cell_size, true, false) -- Draw X or O local value = game.vars.grid[cell_idx] if value == 1 then -- Draw X - renderer.line(cell_x + 2, cell_y + 2, cell_x + CELL_SIZE - 2, cell_y + CELL_SIZE - 2, true, 1) - renderer.line(cell_x + CELL_SIZE - 2, cell_y + 2, cell_x + 2, cell_y + CELL_SIZE - 2, true, 1) + renderer.line(cell_x + 2, cell_y + 2, cell_x + cell_size - 2, cell_y + cell_size - 2, true, 1) + renderer.line(cell_x + cell_size - 2, cell_y + 2, cell_x + 2, cell_y + cell_size - 2, true, 1) elseif value == 2 then -- Draw O (convert center to integers) - renderer.circle(math.floor(cell_x + CELL_SIZE / 2 + 0.5), math.floor(cell_y + CELL_SIZE / 2 + 0.5), CELL_SIZE / 2 - 2, true, false) + renderer.circle(math.floor(cell_x + cell_size / 2 + 0.5), math.floor(cell_y + cell_size / 2 + 0.5), math.floor(cell_size / 2 - 2 + 0.5), true, false) end end end @@ -148,16 +170,18 @@ function draw() end function get_cell_at(x, y) - local start_x = (game.width() - (GRID_SIZE * (CELL_SIZE + CELL_SPACING))) / 2 - local start_y = 30 + local cell_size = get_cell_size() + local start_x = get_grid_start_x() + local start_y = get_grid_start_y() + local cell_spacing = 2 for row = 0, GRID_SIZE - 1 do for col = 0, GRID_SIZE - 1 do - local cell_x = start_x + col * (CELL_SIZE + CELL_SPACING) - local cell_y = start_y + row * (CELL_SIZE + CELL_SPACING) + local cell_x = start_x + col * (cell_size + cell_spacing) + local cell_y = start_y + row * (cell_size + cell_spacing) - if x >= cell_x and x < cell_x + CELL_SIZE and - y >= cell_y and y < cell_y + CELL_SIZE then + if x >= cell_x and x < cell_x + cell_size and + y >= cell_y and y < cell_y + cell_size then return row * GRID_SIZE + col + 1 end end