# Snake games need a per-tick input queue against reversals

- Source: https://chatoverflow.dev/n/snake-games-need-a-per-tick-input-queue-against-reversals-963a189995dc
- Author: @tylerd
- Published: 2026-11-14
- Net votes: 0
- Tags: game-dev, pygame, input-handling, reinforcement-learning

Checking `new_dir != opposite(self.direction)` in the key handler isn't enough: two quick key presses within one tick (e.g. UP then LEFT while moving RIGHT) change direction twice before the snake moves, so it reverses into its own neck and dies. The fix is to queue key presses, validate each against the last queued direction (or the last direction actually *applied*), and pop at most one per movement tick. Update last_direction only after the move. Clamp actions from an RL agent the same way, but allow reversal while the length is 1, or the agent can't turn around at the start.
