Please someone help me with my snake game, can't get a good answer

I fear my previous post may have been too abstract to assist properly, so i’ll put it into a bit more concrete terms.

Let’s say we have a snake segment at X = 50, Y = 100.
Let’s say the snake segment has a width of 10, and a height of 10.
Let’s say the food item likewise has a width of 10 and a height of 10.

What values of X is it not valid to start drawing the food at?
What values of Y is it not valid to start drawing the food at?
How can we take those answers and work back to a formula?

Sorry man I still don’t get it.

With a width of 10, a snake segment that’s centered on 50 takes up width from 45 to 55. Any part of the apple that ends up in there overlaps, which is to be prevented. An apple that’s centered on 58 would overlap, as it goes from 53 to 63 resulting in an overlap from 53 to 55.

He’s saying that there are common techniques that help to prevent such overlaps from occurring.

1 Like

Paul has pretty much hit it on the head, with one slight caveat: Rects are painted using their top-left (assuming both height and width are positive) corner rather than their center as the defining point.

1 Like

I get that but how can there be an overlap when the canvas can only draw every 16 units up and down?

Looking at the latest version, the answer is that your code doesnt move the food to a place that isnt occupied by the snake.

So consider this scenario.
My snake is: (i’m not going to use multiples of 16, but 1, for sake of mental maths.)


8 segments long, and has just eaten the food.

Your code says “Put the food down somewhere.” Let’s say it chooses… 1,6.
Then your code later on says:

		for(let i = 0; i < snake.length; i++) {
			if(food.x == snake[i].x && food.y == snake[i].y) {
				food = {
					x : Math.floor(Math.random()*((cvsW/unit)-1)+1)*unit,
					y : Math.floor(Math.random()*((cvsH/unit)-1)+1)*unit

so let’s trace that.
i = 0, the snake position is [1,1]… Nothing happens.
i = 1, the snake position is [1,2]… Nothing happens.
i = 2, the snake position is [1,3]… Nothing happens.
i = 3, the snake position is [1,4]… Nothing happens.
i = 4, the snake position is [1,5]… Nothing happens.
i=5: at i = 5, the snake position is [1,6]. Ah! We’ve found a problem! Find a new place for the food. The code rolls a new position and chooses… [1,3] and moves on.
i=6, the snake position is [1,7]… Nothing happens.
i=7, the snake position is [1,8]… Nothing happens.

So we’ve finished our loop. The food is at 1,3. On top of our snake.

Considerations for use:
It’s time to learn Filtering arrays and While loops.
When the food is picked up, choose a place for the new food.
While that location is in the array of the snake, choose a new place.

(NOTE: The game in this state will eventually crash, as it tries to place a piece of food in a completely-snake-filled board.)

1 Like

Its crystal clear now, thank you m_hutley for explaining it they way you did. I kinda new from the beginning that my code would remove the food from the snake once and then place it somewhere else regardless of where the snake was. It was the WHILE that made me see. The ‘overlap’ just confused me but now I see what you guys were saying. So helpful:) I’m sure I’ll be back later with some more problems

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.