Prevent snake from turning around directly.

This commit is contained in:
paulusja 2026-04-23 14:52:45 +02:00
parent 129c6d5f3b
commit 5090c09123
2 changed files with 13 additions and 1 deletions

View File

@ -9,3 +9,7 @@ class SnakeState:
return SnakeSegment(center=self._get_next_pos(old_head.get_position(), old_head.get_radius()), return SnakeSegment(center=self._get_next_pos(old_head.get_position(), old_head.get_radius()),
radius=old_head.get_radius(), radius=old_head.get_radius(),
color=old_head.get_color()) color=old_head.get_color())
@property
def type(self):
return self.__class__

View File

@ -16,8 +16,16 @@ class SnakeStateMachine:
InputManager.RIGHT: SnakeRightMovementState() InputManager.RIGHT: SnakeRightMovementState()
} }
self.__input_ignore_dict = {
SnakeDownMovementState: InputManager.UP,
SnakeUpMovementState: InputManager.DOWN,
SnakeLeftMovementState: InputManager.RIGHT,
SnakeRightMovementState: InputManager.LEFT
}
def update(self, user_input): def update(self, user_input):
if user_input != self.__input_ignore_dict.get(self.__state.type, None):
self.__state = self.__next_state_dict.get(user_input, self.__state) self.__state = self.__next_state_dict.get(user_input, self.__state)