微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何创建自定义海龟形状

如何解决如何创建自定义海龟形状

我正在尝试创建一个以 png 照片作为形状的海龟。 我曾尝试使用这种方法

import turtle

screen = turtle.Screen()

image = ("rocketship.png")

screen.addshape(image)
turtle.shape(image)

但是它显示了这个:

  File "C:\Users\Drukker\AppData\Local\Programs\Python\python39\Among_us.py",line 10,in <module>
    screen.addshape(image)
  File "C:\Users\Drukker\AppData\Local\Programs\Python\python39\lib\turtle.py",line 1136,in register_shape
    raise TurtleGraphicsError("Bad arguments for register_shape.\n"
turtle.TurtleGraphicsError: Bad arguments for register_shape.
Use  help(register_shape)```

Does anyone kNow a solution?
Thanks

解决方法

两天前我遇到了同样的问题,您必须将 png 转换为 gif。一个很好的网站是 https://ezgif.com/apng-to-gif。如果你下载并保存它,你必须在计算机中找到它,使用代码......这是我所做的:

import os
from turtle import *
sc = Screen()
sc.setup(600,600)
image = os.path.expanduser("~\OneDrive\Desktop\AlienGameImage.gif")
sc.addshape(image)
t = Turtle()
t.shape(image)
mainloop()

出现 raise TurtleGraphicsError("Bad arguments for register_shape.\n" 的原因是它不接受 PNG 图像以将其注册为形状。

,

问题在于 .addshape() 方法。

如果它不是 gif 文件,则必须指定 shape 属性。

在此处查看详细信息:https://docs.python.org/3/library/turtle.html#turtle.addshape

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。