门之间的乌龟跳

时间:2017-02-28 13:01:32

标签: python turtle-graphics

如何画一只在门之间跳跃的乌龟

  1. 为了看到我需要用数字绘制X的门,每个数字代表一个门,我如何用下面1到100的数字画线?

  2. 如何绘制半圆来观察步骤?而不只是一条向后退线的箭头

  3. 编辑#1:

    enter image description here

    预期结果: 移动= [4,8,7,15]

    0 4 (40.00,0.00)
    1 8 (80.00,0.00)
    2 7 (70.00,0.00)
    3 15 (150.00,-0.00)
    

    结果是:

    0 4 (40.00,0.00)
    1 8 (80.00,0.00)
    2 7 (240.00,0.00)
    3 15 (100.00,-0.00) 
    

    更新代码:

    import turtle
    from time import sleep
    
    canvas = turtle.Screen()
    canvas.title('')
    canvas.setup(900, 400)
    DELTA = 10
    movement = [4, 8, 7, 15]
    t = turtle.Turtle()
    t.penup()
    t.goto(movement[0] * DELTA, 0)
    t.pendown()
    
    
    def write_step(n):
        t.penup()
        px_distance = 20
        t.forward(px_distance)
        t.write("{}".format(n))
        t.back(px_distance)
        t.pendown()
    
    
    for i, step in enumerate(movement):
        print(i, step, t.pos())
        if i > 0:
            if movement[i - 1] > step:
                t.left(90)
                t.circle(step * DELTA, -180)
            else:
                t.right(90)
                t.circle(step * DELTA, 180)
            write_step(i)
        else:
            t.forward(step * DELTA)
        t.setheading(0)
        sleep(1)
    
    canvas.exitonclick()
    

3 个答案:

答案 0 :(得分:1)

您可以使用turtle.circle命令执行弧,但使用额外参数表示180度。您可以使用turtle.write编写数字。

例如:

import turtle
from time import sleep

movement = [2, 5, 8, 4, 10, 3]

for i, step in enumerate(movement):
    if i > 0:
        if movement[i - 1] < step:
            #turtle.forward(step * 10)
            turtle.left(90)
        else:
            turtle.right(90)
        turtle.circle(step*10, 180)
        turtle.penup()
        turtle.forward(10)
        turtle.write("{}".format(i))
        turtle.back(10)
        turtle.pendown()
    else:
        turtle.forward(step * 10)
    turtle.setheading(0)
    sleep(1)

turtle.exitonclick()

答案 1 :(得分:1)

这种情况可能会setworldcoordinates()简化您的逻辑并为您提供您真正想要的东西:

from turtle import Turtle, Screen

FONT = ("Arial", 12, "normal")

def write_step(n, turtle):
    turtle.penup()
    y = turtle.ycor()
    turtle.sety(-7)
    turtle.write(n, align="center", font=FONT)
    turtle.sety(y)
    turtle.pendown()

canvas = Screen()
canvas.title('')
canvas.setup(1800, 300)
canvas.setworldcoordinates(0, -10, 100, 10)

movement = [5, 15, 10, 25, 50, 40]

yertle = Turtle(shape="circle")

for n in range(100):
    yertle.goto(n, -9)
    if n % 5 == 0:
        yertle.write(n, align="center", font=FONT)

yertle.penup()
yertle.hideturtle()
yertle.goto(movement[0], -5)
yertle.showturtle()
yertle.pendown()

for i, step in enumerate(movement):
    if step != movement[-1]:
        distance = 0
        next_move = movement[i + 1]
        if next_move > step:
            distance = next_move - step
            yertle.right(90)
            yertle.circle(distance / 2, -180, steps=60)
        else:
            distance = step - next_move
            yertle.left(90)
            yertle.circle(distance / 2, 180, steps=60)
        write_step(i + 1, yertle)
    yertle.setheading(0)

canvas.exitonclick()

答案 2 :(得分:0)

import turtle
from time import sleep

canvas = turtle.Screen()
canvas.title('')
canvas.setup(1800, 300)
DELTA = 10
movement = [5, 15, 10, 25, 50, 40]
t2 = turtle.Turtle()
for i in range(100):
    t2.goto(i * DELTA, -50)
    if i % 10 == 0:
        t2.write(i)
t = turtle.Turtle()
t.penup()
t.goto(movement[0] * DELTA, 0)
t.pendown()


def write_step(n):
    t.penup()
    px_distance = 20
    t.forward(px_distance)
    t.write("{}".format(n))
    t.back(px_distance)
    t.pendown()

for i, step in enumerate(movement):
    if step != movement[-1]:
        distance = 0
        next_move = movement[i + 1]
        if next_move > step:
            distance = DELTA * (next_move - step)
            t.right(90)
            t.circle(distance / 2, -180)
        else:
            distance = DELTA * (step - next_move)
            t.left(90)
            t.circle(distance / 2, 180)
        write_step(i + 1)
    t.setheading(0)
    sleep(0.1)

canvas.exitonclick()