From b22170b62cd0f04f2afef302f371845ac5734dfc Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 12 Feb 2026 20:51:26 -0500 Subject: [PATCH] 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. --- games/lua_examples/air_hockey.lua | 4 ++-- games/lua_examples/asteroids.lua | 4 ++-- games/lua_examples/ball.lua | 10 +++++----- games/lua_examples/breakout.lua | 4 ++-- games/lua_examples/counter.lua | 4 ++-- games/lua_examples/flappy_bird.lua | 4 ++-- games/lua_examples/pong.lua | 4 ++-- games/lua_examples/snake.lua | 6 +++--- games/lua_examples/tic_tac_toe.lua | 4 ++-- 9 files changed, 22 insertions(+), 22 deletions(-) diff --git a/games/lua_examples/air_hockey.lua b/games/lua_examples/air_hockey.lua index ed22db0..7ae417c 100644 --- a/games/lua_examples/air_hockey.lua +++ b/games/lua_examples/air_hockey.lua @@ -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) diff --git a/games/lua_examples/asteroids.lua b/games/lua_examples/asteroids.lua index fefcc1d..0e2b27a 100644 --- a/games/lua_examples/asteroids.lua +++ b/games/lua_examples/asteroids.lua @@ -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 diff --git a/games/lua_examples/ball.lua b/games/lua_examples/ball.lua index 737c18b..9744c43 100644 --- a/games/lua_examples/ball.lua +++ b/games/lua_examples/ball.lua @@ -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 diff --git a/games/lua_examples/breakout.lua b/games/lua_examples/breakout.lua index 904e8d7..9a888c4 100644 --- a/games/lua_examples/breakout.lua +++ b/games/lua_examples/breakout.lua @@ -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) diff --git a/games/lua_examples/counter.lua b/games/lua_examples/counter.lua index c439ea3..aff5876 100644 --- a/games/lua_examples/counter.lua +++ b/games/lua_examples/counter.lua @@ -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 diff --git a/games/lua_examples/flappy_bird.lua b/games/lua_examples/flappy_bird.lua index 99a3b36..d34de73 100644 --- a/games/lua_examples/flappy_bird.lua +++ b/games/lua_examples/flappy_bird.lua @@ -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 diff --git a/games/lua_examples/pong.lua b/games/lua_examples/pong.lua index e52f100..6b10432 100644 --- a/games/lua_examples/pong.lua +++ b/games/lua_examples/pong.lua @@ -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) diff --git a/games/lua_examples/snake.lua b/games/lua_examples/snake.lua index a491c9d..67db017 100644 --- a/games/lua_examples/snake.lua +++ b/games/lua_examples/snake.lua @@ -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 diff --git a/games/lua_examples/tic_tac_toe.lua b/games/lua_examples/tic_tac_toe.lua index ae599a6..470d645 100644 --- a/games/lua_examples/tic_tac_toe.lua +++ b/games/lua_examples/tic_tac_toe.lua @@ -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