Python龟在不同位置印上相同的形状/图像

2024-10-01 07:48:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在画布上的多个位置印上同一个形状(我正在给海龟添加的图像)。但是,似乎每次我在第二个形状上盖章时,第一个就消失了

有没有办法使用相同的形状/图像在画布上显示多个邮票?当使用海龟中已有的形状时,这似乎不是问题


Tags: 图像画布形状海龟消失办法邮票
1条回答
网友
1楼 · 发布于 2024-10-01 07:48:34

您应该能够在同一图像的任意数量的副本上盖章,而不会使它们消失,直到您请求它们这样做。对于自定义多边形形状:

from turtle import Screen, Shape, Turtle

POLYGON = (
    (10, 10),
    (0, 10),
    (-10, 0),
    (0, -10),
    (10, -10),
)

screen = Screen()

screen.register_shape('house', Shape('polygon', POLYGON))

turtle = Turtle('house')
turtle.penup()

for x in range(-200, 200, 30):
    turtle.setx(x)
    turtle.stamp()

turtle.hideturtle()
screen.exitonclick()

enter image description here

或对于自定义图像形状:

from turtle import Screen, Turtle

IMAGE = "home-5-24.gif"  # https://www.iconsdb.com/gray-icons/home-5-icon.html

screen = Screen()

screen.register_shape(IMAGE)

turtle = Turtle(IMAGE)
turtle.penup()

for x in range(-200, 200, 30):
    turtle.setx(x)
    turtle.stamp()

turtle.hideturtle()
screen.exitonclick()

enter image description here

相关问题 更多 >