Update Snake: wrap edges and halve speed

snake.lua:
- Changed wall collision to wrap around (toroidal grid)
- Snake now travels through edges instead of crashing
- Doubled move_speed from 10 to 20 frames (half speed)
- Updated speed increase cap from 3 to 5 frames (slower at max)
- Snake can now traverse indefinitely without losing to walls
This commit is contained in:
Adolfo Reyna
2026-02-12 21:10:44 -05:00
parent a058476b42
commit a274fb04a1

View File

@@ -30,7 +30,7 @@ function init()
game.vars.food_y = 14
game.vars.frame_count = 0
game.vars.move_speed = 10 -- Frames between moves
game.vars.move_speed = 20 -- Frames between moves (doubled from 10 for half speed)
-- Enable continuous frame updates
game.set_frame_updates(true)
@@ -167,15 +167,13 @@ end
function move_snake()
local head = game.vars.snake[1]
local new_head = {
x = head.x + game.vars.dir_x,
y = head.y + game.vars.dir_y
x = (head.x + game.vars.dir_x) % GRID_W,
y = (head.y + game.vars.dir_y) % GRID_H
}
-- Check wall collision
if new_head.x < 0 or new_head.x >= GRID_W or new_head.y < 0 or new_head.y >= GRID_H then
game_over()
return
end
-- Wrap around negative values (Lua modulo behavior)
if new_head.x < 0 then new_head.x = new_head.x + GRID_W end
if new_head.y < 0 then new_head.y = new_head.y + GRID_H end
-- Check self collision
for i = 1, #game.vars.snake do
@@ -193,8 +191,8 @@ function move_snake()
game.vars.score = game.vars.score + 10
spawn_food()
-- Increase speed slightly
if game.vars.move_speed > 3 then
-- Increase speed slightly (but keep it slower overall)
if game.vars.move_speed > 5 then
game.vars.move_speed = game.vars.move_speed - 1
end
end