Python 海龟在门之间跳跃
我如何画一只在门之间跳跃的乌龟Python 海龟在门之间跳跃,python,turtle-graphics,Python,Turtle Graphics,我如何画一只在门之间跳跃的乌龟 为了可视化门,我需要用数字画X,每个数字代表一扇门,我如何用下面1到100的数字画线 如何画一个半圆来可视化这一步?而不仅仅是一个箭头在一条线上向后移动 编辑#1: 预期结果: 对于运动=[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)
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()
您可以使用turtle.circle命令绘制圆弧,但需要额外的参数来表示180度。你可以用海龟来写数字 例如:
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()
导入海龟
从时间上导入睡眠
移动=[2,5,8,4,10,3]
对于i,步骤枚举(移动):
如果i>0:
如果移动[i-1]<步骤:
#乌龟。前进(第10步)
乌龟。左(90)
其他:
乌龟,对(90)
海龟圈(第10步,180步)
乌龟
乌龟前进(10)
编写(“{}”.format(i))
海龟背(10)
乌龟
其他:
乌龟。前进(第10步)
乌龟。设置标题(0)
睡眠(1)
turtle.exitonclick()
您可以使用turtle.circle命令绘制圆弧,但需要额外的参数来表示180度。你可以用海龟来写数字
例如:
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()
导入海龟
从时间上导入睡眠
移动=[2,5,8,4,10,3]
对于i,步骤枚举(移动):
如果i>0:
如果移动[i-1]<步骤:
#乌龟。前进(第10步)
乌龟。左(90)
其他:
乌龟,对(90)
海龟圈(第10步,180步)
乌龟
乌龟前进(10)
编写(“{}”.format(i))
海龟背(10)
乌龟
其他:
乌龟。前进(第10步)
乌龟。设置标题(0)
睡眠(1)
turtle.exitonclick()
这种情况下,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()
在这种情况下,
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()
感谢您的帮助,编辑了我的代码,它在x轴上的移动量超过了应有的移动量谢谢您的帮助,编辑了我的代码,它在x轴上的移动量超过了应有的移动量