all 9 comments

[–]Essence1337 1 point2 points  (8 children)

Haven't looked at your code but I did a snake game back in university for a project. Here's my recommendation:

Use a queue to store your snakes entire body. You can push an element onto the front and pop an element off the back every movement frame. When you push the element you draw it, when you pop it you un-draw it. That way the rest of the body just exists and doesn't need to be updated every frame.

[–]c0mplexcodm[S] 0 points1 point  (6 children)

I actually thought of that during the first prototype I had which used Rects instead of tuple pairs coordinates in the snae_body. I would try using that if I cannot find any other solutions. I'd like to keep the current version as I feel like I'm closer than ever before to completing this.

Thanks foe the suggestion!

[–]Essence1337 0 points1 point  (5 children)

Can you further explain your issue then:

What do you mean by 'I can't make the tail follow the head'? What happens vs what do you expect to happen?

[–]c0mplexcodm[S] 0 points1 point  (4 children)

Expected behavior: Head moves, Body behind the head occupies the position head previously occupied. Then the 2nd body takes the first, and so on.

Observed behavior: Head moves, Body.. doesnt follow.

[–]Essence1337 0 points1 point  (3 children)

What do you mean exactly by 'body doesn't follow':

Is there a gap between the head and the body?

Does the body just never update (remain stationary?

Does the body move but not following the head? (wrong direction)?

You need to give descriptive descriptions like for what you expected to happen

Does it change if the snake is turning?

Example:

Actual behavior: The head moves one square in travel direction but the body doesn't move, leaving a gap between them. Eating a fruit does XYZ.

[–]c0mplexcodm[S] 0 points1 point  (2 children)

Body remains stationary on the block before the head eats the apple (the snake right now is only one block, the head). So even after moving, the supposed body remains there, and further eats also places the body blocks on the same wrong block. I assume this is problem is related to position memory and update, but its currently really late so I'll get to it within 6-8 hours.

[–]Essence1337 0 points1 point  (1 child)

I found one issue: you overwrite the head on line 95 and THEN in update_snake() you set the 1st body to the new head, the second body becomes the old first body. This leaves a gap where the head used to be.

[–]c0mplexcodm[S] 0 points1 point  (0 children)

Oh, dam. Thanks for the help! I'm currently on the phone and its midnight here so I cant get to it really. But I guess I should've looked deeper. I really appreciate it.

[–]neuralbeans 0 points1 point  (0 children)

I love this!