【问题标题】:How to move a turtle stamp in python如何在python中移动乌龟印章
【发布时间】:2014-08-01 20:21:12
【问题描述】:

如何在 python turtle 模块中移动图章?

到目前为止,这是我的代码:

import turtle

def draw_start():
    turtle.pu()
    turtle.setpos(-350,300)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(520)


def draw_finish():
    turtle.speed(15)
    turtle.pu()
    for i in range(18):
        turtle.setpos(200,(300-(i*30)))
        square()
    for j in range(18):
        turtle.setpos(215,(285-(j*30)))
        square()

def stamp_turtle(x,y,color):
    turtle.pu()
    turtle.setheading(0)
    turtle.shape("turtle")
    turtle.color(color)
    turtle.setpos(x,y)
    stamp_list.append(turtle.stamp())

def square():
    turtle.pu()
    turtle.fill(True)    
    for i in range(4):
        turtle.forward(15)
        turtle.right(90)
    turtle.fill(False)

print "Welcome to Turtle Racing : "
number_of_turtles = int(raw_input("How many turtles (between 3 and 6) : "))
bet_amount = int(raw_input("How much do you want to bet? $ "))
bet_turtle = raw_input("Which turtle (1 to 5)? ")

color_list=["red","blue","green","brown","yellow","purple"]
stamp_list=[]
draw_start()
draw_finish()
for i in range(number_of_turtles):
    stamp_turtle(-370,280-i*90,color_list[i])`

【问题讨论】:

  • 我已经浏览并阅读了几乎整本书,但没有任何帮助。 @迈克贝尔
  • 要快速删除所有标记的海龟,只需使用存储在stamp_list 中的每个 id 调用 turtle.clearstamp()。之后,您可以在稍微不同的位置再次标记它们。您需要以某种方式跟踪每个人的位置,以便您可以稍微修改一下,然后再将它们全部重新绘制到更新位置。
  • 使用这里的方法似乎对我来说很好,但也许我不了解问题的全部内容。 docs.python.org/2/library/turtle.html#turtle-motion

标签: python turtle-graphics stamp


【解决方案1】:

答案是你不移动邮票,你移动海龟!邮票必须被移除和重新盖章,而海龟可以在不重新绘制的情况下移动:

import random
import turtle

STAMP_SIZE = 20
SQUARE_SIZE = 15
FINISH_LINE = 200
COLOR_LIST = ['red', 'blue', 'green', 'brown', 'yellow', 'purple']

def draw_start():
    turtle.speed('fastest')
    turtle.penup()
    turtle.setpos(-350, 300)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(520)

def draw_finish():
    turtle.shape('square')
    turtle.shapesize(SQUARE_SIZE / STAMP_SIZE)
    turtle.penup()

    for i in range(18):
        turtle.setpos(FINISH_LINE, (300 - (i * SQUARE_SIZE * 2)))
        turtle.stamp()

    for j in range(18):
        turtle.setpos(FINISH_LINE + SQUARE_SIZE, ((300 - SQUARE_SIZE) - (j * SQUARE_SIZE  * 2)))
        turtle.stamp()

    turtle.hideturtle()

def move_turtle(who):
    who.forward(random.randint(1, 10))
    if who.xcor() < FINISH_LINE:
        turtle.ontimer(lambda who=who: move_turtle(who), 50)

print('Welcome to Turtle Racing!')
number_of_turtles = int(input('How many turtles (between 3 and 6): '))

draw_start()
draw_finish()

turtle_list = []

for idx in range(number_of_turtles):
    racer = turtle.Turtle('turtle', visible=False)
    racer.speed('fastest')  # affects drawing speed, not forward motion
    racer.penup()
    racer.setpos(-350 - STAMP_SIZE, 280 - idx * 90)
    racer.color(COLOR_LIST[idx])
    racer.showturtle()

    turtle_list.append(racer)

for racer in turtle_list:
    turtle.ontimer(lambda who=racer: move_turtle(who), 100)

turtle.exitonclick()

冲压加快速度的地方在于,如果您尝试绘制终点线,则需要更长的时间。

请注意,虽然您的原始代码是 Python 2,但我的答案是 Python 3,因此如果您仍在使用旧版本,您可能需要进行一些调整。

【讨论】:

  • 谢谢!是的,大约 2 年 10 个月前,当我还在 CompSci 的第一年时,我有点想通了。好久不见了……
【解决方案2】:

移动海龟标记:擦除它,调用 penup() 并在其他地方重新标记:

import turtle
import time
a = turtle.Turtle()
a.penup()
a.goto(0, -200)
a.pendown()
a.stamp()
time.sleep(1)
a.clear()
a.penup()
a.goto(10,10)
a.stamp()

邮票从 0,200 开始,然后消失并在 10,10 重新出现

【讨论】:

  • 你删除了乌龟可能画的所有东西。您应该做的是捕获a.stamp() 的结果并要求该标记 ID 清除(仅)自身。
最近更新 更多