0

I have a python program using turtle to make the game "Snake". It is fully functional. At this point I'm adding in .gif images to spruce it up. The problem arises when I'm trying to use a custom shape as the head of the snake. If I use a basic shape from turtle like "triangle" or "arrow" it works fine. It turns correctly and is printed over the first body segment as intended. When I change it to my custom image, the problem is that it is printed out under the first body segment and cannot turn.

Sample of controls

    if direction == "up":
        if snake_direction != "down":
            snake_direction = "up"
            head_stamper.setheading(90)

Stampers used to make numerous sections, and a head over the first segment.

        for segment in snake:
            stamper.goto(segment[0], segment[1]) #These are the body segments, they work fine.
            stamper.stamp()
            head_stamper.goto(new_head)

Showing both stampers here.

# Stamper for each body section
stamper = turtle.Turtle()
stamper.shape(bod_segment)
stamper.shapesize(25 / 20)
stamper.penup()

# Special stamper just for the snake's head.
head_stamper = turtle.Turtle()
# head_stamper has no issues when I make the shape "arrow" or "triangle", etc.
head_stamper.shape(head_segment)
stamper.shapesize(25 / 20)
head_stamper.penup()

I think this is all the code relevant to the problem.

1 Answers1

0

When I change it to my custom image, the problem is that it is printed out under the first body segment and cannot turn.

As far as not turning, this is addressed in the Python turtle documentation of register_shape() aka addshape():

Note: Image shapes do not rotate when turning the turtle, so they do not display the heading of the turtle!

As far as the overlap problem, I can only guess. Generally the rule in turtle is the last thing that moved is on top. So turning your generic head shape lands it on top, but since your image shape doesn't actually turn, it winds up on the bottom. Again, just a guess.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I've tested it a little bit more, and I think you're right. Thanks! I think my solution will have to be drawing a custom shape with turtle itself. – mayor_mccheese Jan 07 '22 at 22:44