import turtle
s1 = turtle.Screen()
b = turtle.Turtle()
b.shape("turtle")
b.color("blue")
b.speed(9)
s1.bgpic("grass.gif")
########## turtles
t1 = turtle.Turtle()
t1.shape('circle')
t1.pu()
t1.goto(100,200)
t2 = turtle.Turtle()
t2.shape('circle')
t2.pu()
t2.goto(-100,-200)
t3 = turtle.Turtle()
t3.shape('circle')
t3.pu()
t3.goto(200,-200)
t4 = turtle.Turtle()
t4.shape('circle')
t4.pu()
t4.goto(0,200)
t5 = turtle.Turtle()
t5.shape('circle')
t5.pu()
t5.goto(400,300)
用于将乌龟改为下载的gif图像的python turtle命令是什么?我需要将t1-t5更改为gif图像而不是圆圈。
答案 0 :(得分:2)
我使用以下代码将图像导入程序:
output: {
....
library: 'server',
libraryTarget: 'commonjs2'
}
target: 'node'
要将乌龟变成.gif图像,可以将图像添加到形状中。
import turtle
wn = turtle.Screen()
wn.bgcolor('white')
your_image = r"C:\Users\your_name\Pictures\your_image.gif"
wn.addshape(your_image)
答案 1 :(得分:0)
对我来说,我想使用 PNG ,而不是 GIF 。最后,我找到了答案,并记录了 GIF 和 PNG 解决方案,以期帮助他人。
t = turtle.Turtle()
# you can change the shape with this command, but you need to tell it what the 'your_shape_name' is.
t.shape('your_shape_name')
首先,您需要register_shape
之前的turtle.Screen()
,
等等!在开始之前,请先查看源代码,如下所示:
# Lib/turtle.py
class TurtleScreen(TurtleScreenBase):
...
def register_shape(self, name, shape=None):
if shape is None:
# image
if name.lower().endswith(".gif"):
shape = Shape("image",
self._image(name) # return ``TK.PhotoImage(file=filename)`
)
else:
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
+ "Use help(register_shape)" )
elif isinstance(shape, tuple):
shape = Shape("polygon", shape)
## else shape assumed to be Shape-instance
self._shapes[name] = shape
...
addshape = register_shape # <---- so they are the same
确定,然后开始
screen = turtle.Screen()
t = turtle.Turtle() # a Turtle instance
# t.shape('your_shape_name') # you can change the shape with this command, but you need tell it what is 'your_shape_name'
if '1. source is png (or anything that is compatible with `tk.PhotoImage`)':
your_shape=turtle.Shape('image', tk.PhotoImage(file='your.png'))) # name must is 'image'
screen.register_shape(name='your_cool_shape_name', shape=your_shape)
t.shape('your_cool_shape_name')
# t.goto(1, 2)
if '2 source is gif':
from pathlib import Path
your_gif = str(Path('.../your.gif'))
screen.register_shape(your_gif)
t.shape(your_gif)