jQuery I created a Snake game
Results 1 to 8 of 8

Thread: I created a Snake game

  1. #1
    Senior Member
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    794

    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.

    This is all being taken care of in JavaScript. The only PHP is the initial "drawing" of the grid. You can take a look at the JavaScript code at the following Pastebin link:

    Link

    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 )
    Prison of Mirrors
    Declare variables, not war.

  2. #2
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,619
    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.

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,621
    Quote Originally Posted by Bonesnap View Post
    Thanks for reading! (By the way, my best score so far is 1100 )
    60.


    Of course, I just played one time
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    Senior Member
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    794
    Quote Originally Posted by johanafm View Post
    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.
    Hmmm, even though I have tried to do something similar, reading your reply has inspired me to try something new. Thanks for your input!

    Quote Originally Posted by dalecosp View Post
    60.


    Of course, I just played one time
    I have played it several times for, uh, science...
    Prison of Mirrors
    Declare variables, not war.

  5. #5
    eUKHost web hosting Asher ross's Avatar
    Join Date
    Aug 2012
    Location
    Building 3, City West Office Park, Gelderd Road Leeds
    Posts
    2
    Hi, Really good work. I have enjoyed it a lot.

  6. #6
    Senior Member
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    794
    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!

    Quote Originally Posted by Asher ross View Post
    Hi, Really good work. I have enjoyed it a lot.
    Cool, thanks!
    Prison of Mirrors
    Declare variables, not war.

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    620. fun.

  8. #8
    Senior Member
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    794
    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.

    Quote Originally Posted by traq View Post
    620. fun.
    Thanks!
    Prison of Mirrors
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •