Skip to content

Prevent the snake from going back on itself#3

Open
ali1234 wants to merge 1 commit into32blit:mainfrom
ali1234:fixes
Open

Prevent the snake from going back on itself#3
ali1234 wants to merge 1 commit into32blit:mainfrom
ali1234:fixes

Conversation

@ali1234
Copy link

@ali1234 ali1234 commented Sep 12, 2021

There are two ways that could happen:

If you are going up and press left and down on the same frame, pressing
left switches your movement to left, and then the check for vertical
movement passes and you start going down in the same update.

The other way is if two updates happen in one game tick.

Fix both of these by remembering the last move. Switch the direction
variable to an enum to make it easier to test the current and previous
direction, and use a switch statement to calculate the actual changes
to the position.

There are two ways that could happen:

If you are going up and press left and down on the same frame, pressing
left switches your movement to left, and then the check for vertical
movement passes and you start going down in the same update.

The other way is if two updates happen in one game tick.

Fix both of these by remembering the last move. Switch the direction
variable to an enum to make it easier to test the current and previous
direction, and use a switch statement to calculate the actual changes
to the position.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant