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