Scale Memory Match to fill screen

memory_match.lua:
- Removed hardcoded CARD_SIZE and CARD_SPACING
- Added get_card_size() function that calculates card dimensions based on available screen space
- Grid now fills screen width (minus padding) and uses available height
- Added get_grid_start_x/y() to center grid
- All card positions and hit detection updated to use dynamic sizing
- Cards scale proportionally on any screen size while maintaining 4x4 layout
This commit is contained in:
Adolfo Reyna
2026-02-12 21:09:46 -05:00
parent 62716c7dc4
commit 2a9b17a539

View File

@@ -9,10 +9,33 @@ local STATE_GAME_OVER = 2
-- Game constants
local GRID_COLS = 4
local GRID_ROWS = 4
local CARD_SIZE = 28
local CARD_SPACING = 4
local FLIP_DURATION = 15 -- Frames to show flip animation
-- Calculate card size based on screen size
local function get_card_size()
-- Use available space with padding
local padding = math.floor(game.width() / 8)
local available_width = game.width() - (padding * 2)
local available_height = game.height() - 80 -- Leave room for text
-- Calculate based on grid
local card_width = math.floor(available_width / GRID_COLS)
local card_height = math.floor(available_height / GRID_ROWS)
-- Use the smaller dimension for square cards
return math.min(card_width, card_height)
end
local function get_grid_start_x()
local card_size = get_card_size()
local grid_width = card_size * GRID_COLS
return math.floor((game.width() - grid_width) / 2)
end
local function get_grid_start_y()
return 40
end
function init()
game.vars.state = STATE_MENU
game.vars.score = 0
@@ -105,6 +128,9 @@ function draw()
renderer.clear(false) -- Black background
local state = game.vars.state
local card_size = get_card_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 - 40, game.height() / 2 - 40, "MEMORY MATCH", true)
@@ -113,24 +139,21 @@ function draw()
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw grid
local start_x = (game.width() - (GRID_COLS * (CARD_SIZE + CARD_SPACING))) / 2
local start_y = 30
for row = 0, GRID_ROWS - 1 do
for col = 0, GRID_COLS - 1 do
local idx = row * GRID_COLS + col + 1
local card = game.vars.cards[idx]
local x = start_x + col * (CARD_SIZE + CARD_SPACING)
local y = start_y + row * (CARD_SIZE + CARD_SPACING)
local x = start_x + col * card_size
local y = start_y + row * card_size
if card.face_up or card.matched then
-- Show card value
renderer.rect(x, y, CARD_SIZE, CARD_SIZE, true, true)
renderer.rect(x, y, card_size, card_size, true, true)
local text = tostring(card.id)
renderer.text(x + CARD_SIZE / 2 - 2, y + CARD_SIZE / 2 - 2, text, false)
renderer.text(x + math.floor(card_size / 2 - 2), y + math.floor(card_size / 2 - 2), text, false)
else
-- Face down (outline)
renderer.rect(x, y, CARD_SIZE, CARD_SIZE, true, false)
renderer.rect(x, y, card_size, card_size, true, false)
end
end
end
@@ -147,16 +170,17 @@ function draw()
end
function get_card_at(x, y)
local start_x = (game.width() - (GRID_COLS * (CARD_SIZE + CARD_SPACING))) / 2
local start_y = 30
local card_size = get_card_size()
local start_x = get_grid_start_x()
local start_y = get_grid_start_y()
for row = 0, GRID_ROWS - 1 do
for col = 0, GRID_COLS - 1 do
local card_x = start_x + col * (CARD_SIZE + CARD_SPACING)
local card_y = start_y + row * (CARD_SIZE + CARD_SPACING)
local card_x = start_x + col * card_size
local card_y = start_y + row * card_size
if x >= card_x and x < card_x + CARD_SIZE and
y >= card_y and y < card_y + CARD_SIZE then
if x >= card_x and x < card_x + card_size and
y >= card_y and y < card_y + card_size then
return row * GRID_COLS + col + 1
end
end