Scale Simon Says and Tic-Tac-Toe to fill screen
simon_says.lua: - Removed hardcoded button positions - Added get_buttons() function that calculates button size and position based on screen dimensions - Buttons now use 10% screen padding and scale to fill available width - 2x2 grid automatically scales with screen size tic_tac_toe.lua: - Replaced hardcoded CELL_SIZE with dynamic calculation - Added get_cell_size() to compute size based on screen width - Added get_grid_start_x/y() to center grid both horizontally and vertically - Grid now scales to fill available space while maintaining 3x3 layout - All cell positions updated to use dynamic functions Both games now work on any screen size and scale proportionally.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user