Jump to content

Issues making my first snake game


Recommended Posts


I have been working on my first full game and decided to try a snake clone. I haven't followed any kind of tutorial and have just been trying to get to grips with the framework. I am however having a few issues and have been stuck for a couple of hours to try and fix a couple of annoying bugs. The first main issue I have with my game is that collisions don't seem to be firing correctly. I have tried to set one up when the head of the snake collides with any of the tail elements which just restarts the game state for testing purposes. This seems to however try to trigger another function which should only happen when a player collides with food. I am guessing I need to setup my collision handlers differently in order to fix this.

The other bug is that when a bit of food has been eaten a new sprite it created but just before it does it gets added randomly to somewhere on the screen then disappears but I can't work out why that would be.

I setup a git repo of my project so far on github and it can be found here:


If someone could offer some advice as to what I can do to fix this I would be very grateful.


Link to comment
Share on other sites


The first bug is is caused by the tail array. When you restart and _generateFood again the first time, the tail.length will be incorrect (if you ate at least one food in the previous turn). When you call _trigger_death(), the game state restarts but tail is not reset (you have to manage that manually in your case). try:

function _trigger_death(one, two)

Here is a related topic to deal with this.

The second bug is caused by _eat_food() . When the next sprite is added after eating,  the nextY and nextX values are calculated incorrectly probably (try 100,100 and it will always spawn at 100,100). A simple way out is to set nextX and nextY to -100 so that you won't see that happening.

Link to comment
Share on other sites

I see, I think I assumed that restarted the state would reset the variables at the top of the page but I guess those are not setup by Phaser. I guess it would work if I set them up in a global scope then set their default values inside the create method. Thanks, it's a good thing to note.

I see, yeah it seems like I place it in the wrong place but didn't notice it while I was debugging.

Thanks samid737, appreciate your help on this!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...