Currently, there is a subtle but real flaw in the controls for the game. If the player presses two keys in quick succession, only the last one is registered -- one, or more, are simply dropped.
You can see the effect easily if you lower the framerate to something extreme, e.g. 1 (i.e. 1 frame per second) by changing the clock.tick(20) line to clock.tick(1) . If you quickly press down and then right, only right will be registered.
Thankfully, pygame makes it revlatively easy to fix this.
Inside the while loop -- i.e., for each frame -- we want to see if there are arrow keydown events to consume. If so, we want to move a step in each direction pressed. If no keydown events have occured, the snake should continue in last direction it was sent.
Above the for event... loop, add add a line to create an empty list:
vels = []
Enter fullscreen mode
Exit fullscreen mode
Then, inside the following conditional, instead of setting val = ...…