Im making a snake game and need some code to not allow the same key to be pressed in a row. So for example if 'w' is pressed, it wont be able to be pressed again until another key is pressed. Anyone now the simplest way to achieve this? Thanks
Asked
Active
Viewed 54 times
-1
-
1You can't prevent the user from pressing a key, you can only control how your program reacts to a key press. That said, I'm not sure this requires special handling in Snake, as the code for handling an arrow key press could easily be idempotent. – Brian McCutchon Oct 10 '20 at 21:07
1 Answers
1
When you receive the key press event save the last pressed key and check if the new one is the same then just return from the function.

Iosif
- 812
- 2
- 15