I created a Snake game
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 )
I'd change it so that the keydown events doesn't have direct access to "current_direction". Instead, you assign the value to "next_direction". Then, whenever the snake hits a new grid point (that is, whenever the snake is at a point where it actually is allowed to change direction) you check the value of "next_direction" and update the snake movement as needed.
Settled 4 red convertible
Hmmm, even though I have tried to do something similar, reading your reply has inspired me to try something new. Thanks for your input!
Originally Posted by johanafm
I have played it several times for, uh, science...
Originally Posted by dalecosp
eUKHost web hosting
Hi, Really good work. I have enjoyed it a lot.
So I took what johanafm suggested and combined it with what I had tried before and now the movement is a lot more deliberate and in my opinion, smoother. Thanks for the suggestion!
Originally Posted by Asher ross
My next step is creating a level editor; I'm intending to create an "adventure" mode where the player progresses through levels with increasing difficulty. Each level will have 20 pieces of food. Once all 20 are collected, the player proceeds to the next level. The levels increase in difficulty based on things like barriers/walls. I'm also toying with the idea of enemies or just simply moving barriers, but that's a "maybe in the future" idea.
Originally Posted by traq
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)