Fix Lua game float-to-int conversion errors for renderer.circle()

All games had the same issue: renderer.circle() requires integer arguments,
but float calculations produced non-integer coordinates.

Fixed in all games using math.floor(x + 0.5) for proper rounding:
- pong.lua: Ball position
- air_hockey.lua: Puck position
- asteroids.lua: Asteroid positions
- ball.lua: Ball and trail positions, velocity line
- breakout.lua: Ball position
- flappy_bird.lua: Bird Y position
- counter.lua: Last touch marker position
- snake.lua: Food position (center calculation)
- tic_tac_toe.lua: O circle center position

Also fixed floating-point coordinate calculations in ball and line
drawing to ensure all coordinates are integers.
This commit is contained in:
Adolfo Reyna
2026-02-12 20:51:26 -05:00
parent b5e69abc83
commit b22170b62c
9 changed files with 22 additions and 22 deletions

View File

@@ -105,8 +105,8 @@ function draw()
renderer.rect(5, game.vars.paddle_left_y, PADDLE_WIDTH, PADDLE_HEIGHT, true, true)
renderer.rect(game.width() - 5 - PADDLE_WIDTH, game.vars.paddle_right_y, PADDLE_WIDTH, PADDLE_HEIGHT, true, true)
-- Draw puck
renderer.circle(game.vars.puck_x, game.vars.puck_y, PUCK_RADIUS, true, true)
-- Draw puck (convert to integers)
renderer.circle(math.floor(game.vars.puck_x + 0.5), math.floor(game.vars.puck_y + 0.5), PUCK_RADIUS, true, true)
-- Draw scores
renderer.text(game.width() / 2 - 30, 5, tostring(game.vars.paddle_left_score), true)

View File

@@ -104,10 +104,10 @@ function draw()
renderer.text(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING or state == STATE_GAME_OVER then
-- Draw asteroids
-- Draw asteroids (convert to integers)
for i = 1, #game.vars.asteroids do
local ast = game.vars.asteroids[i]
renderer.circle(ast.x, ast.y, ast.size, true, false)
renderer.circle(math.floor(ast.x + 0.5), math.floor(ast.y + 0.5), ast.size, true, false)
end
-- Draw bullets

View File

@@ -58,14 +58,14 @@ end
function draw()
renderer.clear(false)
-- Draw ball
renderer.circle(game.vars.ball_x, game.vars.ball_y, game.vars.radius, true, true)
-- Draw ball (convert to integers)
renderer.circle(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), game.vars.radius, true, true)
-- Draw trail (previous positions)
local trail_radius = game.vars.radius - 2
if trail_radius > 2 then
renderer.circle(game.vars.ball_x - game.vars.vel_x,
game.vars.ball_y - game.vars.vel_y,
renderer.circle(math.floor(game.vars.ball_x - game.vars.vel_x + 0.5),
math.floor(game.vars.ball_y - game.vars.vel_y + 0.5),
trail_radius, true, false)
end
@@ -80,5 +80,5 @@ function draw()
-- Draw velocity vector
local arrow_x = game.vars.ball_x + game.vars.vel_x * 3
local arrow_y = game.vars.ball_y + game.vars.vel_y * 3
renderer.line(game.vars.ball_x, game.vars.ball_y, arrow_x, arrow_y, true, 2)
renderer.line(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), math.floor(arrow_x + 0.5), math.floor(arrow_y + 0.5), true, 2)
end

View File

@@ -111,8 +111,8 @@ function draw()
-- Draw paddle
renderer.rect(game.vars.paddle_x, game.height() - PADDLE_HEIGHT - 2, PADDLE_WIDTH, PADDLE_HEIGHT, true, true)
-- Draw ball
renderer.circle(game.vars.ball_x, game.vars.ball_y, BALL_RADIUS, true, true)
-- Draw ball (convert to integers)
renderer.circle(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), BALL_RADIUS, true, true)
-- Draw score and lives
renderer.text(5, 5, "Score: " .. tostring(game.vars.score), true)

View File

@@ -40,8 +40,8 @@ function draw()
local pos_text = "Last: (" .. tostring(game.vars.last_x) .. ", " .. tostring(game.vars.last_y) .. ")"
renderer.text(20, game.height() - 30, pos_text, true)
-- Draw marker at last touch
renderer.circle(game.vars.last_x, game.vars.last_y, 5, true, false)
-- Draw marker at last touch (convert to integers)
renderer.circle(math.floor(game.vars.last_x + 0.5), math.floor(game.vars.last_y + 0.5), 5, true, false)
end
-- Draw instructions

View File

@@ -78,8 +78,8 @@ function draw()
renderer.text(game.width() / 2 - 50, game.height() / 2, "Tap to Start", true)
elseif state == STATE_PLAYING then
-- Draw bird
renderer.circle(20, game.vars.bird_y, BIRD_SIZE, true, true)
-- Draw bird (convert to integer)
renderer.circle(20, math.floor(game.vars.bird_y + 0.5), BIRD_SIZE, true, true)
-- Draw pipes
for i = 1, #game.vars.pipes do

View File

@@ -109,8 +109,8 @@ function draw()
renderer.rect(10, game.vars.paddle_left_y, PADDLE_WIDTH, PADDLE_HEIGHT, true, true)
renderer.rect(game.width() - 10 - PADDLE_WIDTH, game.vars.paddle_right_y, PADDLE_WIDTH, PADDLE_HEIGHT, true, true)
-- Draw ball
renderer.circle(game.vars.ball_x, game.vars.ball_y, BALL_RADIUS, true, true)
-- Draw ball (convert to integers)
renderer.circle(math.floor(game.vars.ball_x + 0.5), math.floor(game.vars.ball_y + 0.5), BALL_RADIUS, true, true)
-- Draw scores
local left_score_text = tostring(game.vars.paddle_left_score)

View File

@@ -127,9 +127,9 @@ function draw()
renderer.rect(seg.x * CELL_SIZE, seg.y * CELL_SIZE, CELL_SIZE, CELL_SIZE, true, filled)
end
-- Draw food
renderer.circle(game.vars.food_x * CELL_SIZE + CELL_SIZE / 2,
game.vars.food_y * CELL_SIZE + CELL_SIZE / 2,
-- Draw food (convert center to integers)
renderer.circle(math.floor(game.vars.food_x * CELL_SIZE + CELL_SIZE / 2 + 0.5),
math.floor(game.vars.food_y * CELL_SIZE + CELL_SIZE / 2 + 0.5),
CELL_SIZE / 2, true, true)
-- Draw score

View File

@@ -127,8 +127,8 @@ function draw()
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
renderer.circle(cell_x + CELL_SIZE / 2, cell_y + CELL_SIZE / 2, CELL_SIZE / 2 - 2, true, false)
-- 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)
end
end
end