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:
Adolfo Reyna
2026-02-12 21:06:54 -05:00
parent b22170b62c
commit 62716c7dc4
2 changed files with 61 additions and 31 deletions

View File

@@ -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