As a precursor to a more ambitious project, I created a very rudimentary prototype Snake game. It runs pretty much as expected with many bugs (you can check out the game here. One of the bugs that I'd like to squash is the movement of the snake. Right now I have the timer move the snake every .25 seconds. You hit the arrow key and the direction changes - most of the time. Right now the checks I have in place prevent the snake from moving in on itself and ending the game. For example if it's currently moving right, if you hit the left key, nothing will happen. This works as expected but still leaves open the ability to very quickly hit the up key and then left before the snake actually moves up, thus ending the game because the snake tried to move to a tile that had a body part. To combat this, I modified some of the movement code so that anytime a direction is pressed, it clears the timeout and moves the snake deliberately.
This fixes the issue but now I have introduced another problem: with the snake being able to be moved so precisely, the fun/challenge of the game has been taken away (in my opinion). If you can simply move the snake at any time, then the timing between moves is rather pointless, since you can essentially tell the snake where to move any time you want. You may also notice that I have added an additional check for the up direction to prevent holding down the key and moving the snake incredibly quickly. I haven't put that check in for the other directions just to contrast the behaviour.
I'm sure there is a much more efficient way to keep track of everything and I do intend on rewriting/refactoring everything, but I just whipped this up quickly on the weekend.
Are there any suggestions on how to handle the movement so I don't move the snake onto itself by accident while maintaining the traditional, timed movement of the snake? There are some remnants of old code where I was trying to do a "current direction" and "intended direction" but it didn't work out.
Thanks for reading! (By the way, my best score so far is 1100 )