因此我使用Python turtle模块创建了一个蛇游戏。目前,我正在使用以下代码绘制蛇的头:
# 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"
做完一些研究后,我发现here是可以使用称为“ addshape”的方法导入图像的。但是,当我运行代码时,出现错误:
AttributeError: 'Turtle' object has no attribute 'addshape'
答案 0 :(得分:0)
addshape
和register_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()
# ...