Hunting bugs
As proud and satisfied as you may be with your first project, nothing is ever perfect. If you've spent some time actually playing the game, you may have noticed an odd event when quickly mashing the buttons, looking something like this:
The image represents the difference between two sequential updates. It seems that earlier it was facing the right direction and then it's facing left and missing its tail. What happened? Try to figure it out on your own before continuing, as it perfectly illustrates the experience of fixing game flaws.
Playing around with it some more reveals certain details that narrow down our problem. Let's break down what happens when a player starts mashing keys quickly:
The snake is facing right.
Any arrow key other than the left or right is pressed.
The direction of the snake gets set to something else, let's say up.
The right key is pressed before the game has a chance to update.
Since the snake's direction is no longer set to right or left,
if
statement in...