AttributeError: 'Turtle' object has no attribute 'addshape'

AttributeError: 'Turtle' object has no attribute 'addshape'

所以我正在使用 Python 乌龟模块创建蛇游戏。目前我在画蛇的头,使用的是这段代码:

# Snake head
head = turtle.Turtle()                      # create an instance of the class turtle called 'head'
head.speed(0)                               # call the speed method
head.shape("square")                        # defines the shape of the snakes head
head.color("black")                         # defines the colour of the snakes head
head.penup()                                # stop the snake from drawing when moving
head.goto(0,0)                              # moves the snakes head to the coordinates 0,0 on the screen.
head.direction = "stop"                     # stops the turtles head from moving strait away

我不想画画,而是想导入一张图片并将其用作蛇头。这是到目前为止的代码

image1 = "D:\Desktop\computing\Python\snake game\img\snake_head.png"
head = turtle.Turtle()
head.speed(0)
head.addshape(image1)
head.goto(0,0)
head.direction = "stop"

经过一些研究我发现 ,您可以使用一种名为 "addshape" 的方法来导入图像。但是,当我 运行 代码时,我收到错误消息:

AttributeError: 'Turtle' object has no attribute 'addshape'

addshaperegister_shape 方法是同义词,您可以使用任何一个。但它们是屏幕的方法,而不是乌龟。最重要的是,图像必须是 GIF 格式,其他任何格式(如 *.png)都不起作用:

from turtle import Screen, Turtle

IMAGE = "D:\Desktop\computing\Python\snake game\img\snake_head.gif"

screen = Screen()
screen.addshape(IMAGE)

head = Turtle(IMAGE)
head.speed('fastest')
head.penup()
# ...