Hacker News new | ask | show | jobs
by fb03 3278 days ago
Not the author, but I've implemented my own snake game and this bug is fairly easy to kill.

(1) - You can only change directions that will change your axis, if you're going left your possible changes are up and down, ....

(2) - You should be only able to consume one direction change per game tick, what is happening here is that rule (1) is working but you can change into the other axis, and then back to the other axis again before the game loop computes another frame. Direction change can be read asynchronously but the actual change should be tied by each tick.

1 comments

Maybe it makes it too easy, but I would enjoy the mechanic that quickly pressing UP LEFT when moving right would make the snake head move one square up then turn left and continue left. I suppose that would require some special casing, and you wouldn't want to support longer queued direction changes. Maybe a nice implementation would be that for inputs queued within a single game tick, execute the first input for one square of movement, then the last input for the next tick.