How would change the snake face/body/and tail?

I have been following the snake tutorial:

I am wondering how would I go about if I wanted to change the aesthetics of the snake itself. I would like to make a snake head, a snake tail, and to increase the snake body when it eats food.

Would I have to to drag the images and make a sprite?

Help would be much appreciated!

You could do it with a Tilemap still. Draw a head upwards and put that as image 3 and one pointing to the right as image 4. In the code, you would set the newhead tile to one of the types and flip it depending on direction with code something like this:

if self.dir.y > 0 or self.dir.y < 0 then
  local flip_horizontally = (self.dir.y < 0)
  tilemap.set_tile("#grid", "layer1", newhead.x, newhead.y, 3, flip_horizontally, false)
else
  local flip_vertically = (self.dir.x < 0)
  tilemap.set_tile("#grid", "layer1", newhead.x, newhead.y, 4, false, flip_vertically)
end

Something similar could be done with the last self.segment entry to get a tail.

And if you make three different body sizes, you would just switch out 2-index in the loop over self.segment and replace that with a dynamic number based on how many different sizes of body width you have.

I hope that helps you a bit on the way! Good luck :smile:

2 Likes

Since it’s tile-based, you could go in and change the tiles themselves. This wouldn’t necessarily change the face and tail though… You would add 2 extra tiles to that tilesheet to represent the tail and head and always place the head tile at the front position of the list and the tail tile at the end position of the body list.

But if you’re trying to make a snake game with more dynamic elements like a smoothly(non-snapped) moving snake, or one with an eating animation, it may be better to use sprites for the snake instead. I believe there is a way to animate tiles though. - Just checked! There are ways, but I’d say less favorable than just putting a sprite over the snake :slight_smile: .

The way I would achieve it would be to just overlay the positions on the screen where the snake body is with sprites. Something like taking an for i,v loop which places a body sprite rotated towards the previous position of the snake. Then for the body and head, I’d get the front position and end position and place them over those positions- whilst being able to easily change their animations.

3 Likes