显示射弹(乌龟)如何随时间行进
Show how a projectile (turtle) travels over time
我是 Python 的新手,目前在海龟图形方面遇到了困难。这就是我要解决的问题
On Turtellini (the planet where Python turtles live) the
transportation system propels turtles with a giant slingshot. A
particular turtle's original location (x0, y0) is (-180, -100). He is
then shot upward at an initial vertical velocity (vy) of 88 units per
second and a horizontal velocity (vx) of 20 units per second to the
right. He travels for 16 seconds. The acceleration due to gravity (g)
is 11 units per second squared. The the location of the turtle at a
given second (t) is calculated as follows: x = x0 + vx * t and y = y0
+ vy * t - g/2 * t2 . This program is to show how a turtle travels over this period of time.
输出应该是这样的:
这是我应该做的;
- 设置常数(垂直速度,水平速度,
重力)和变量(x 和 y 坐标)设置海龟
给他一个合适的形状,把他的尾巴竖起来,把他移到
初始位置,放下他的尾巴做一个重复的循环
第 1 秒到第 16 秒(含)。在循环显示的每次迭代中
x 和 y 变量的值(在 shell window 中),移动
乌龟到那些坐标,让乌龟标记他的形状,
在循环后计算 x 和 y 变量的新值
终止,将海龟移动到最后计算的坐标,
改变他的颜色,标记他的形状,然后等待鼠标点击
到目前为止我的代码:
import turtle
def main():
wn = turtle.Screen()
turtellini = turtle.Turtle()
t = int(input("Blab blab blab: "))
x0 = -180
y0 = -100
vx = 20
vy = 88
g = 11
x = (float(x0 + vx * t))
y = (float(y0 + vy * t - g / 2 * t**2))
turtellini.color("black")
turtellini.shape("turtle")
turtellini.up()
turtellini.goto(-180,-100)
turtellini.down()
for i in range(1,16,1):
turtellini.stamp()
turtellini.forward(i)
turtellini.right(i)
print(x)
print(y)
if __name__ == "__main__":
main()
我知道我做的不好;但是谁能帮我解决这个问题?
你似乎拥有大部分零件。我看到的最大问题是您没有将 x,y 计算放入循环中。循环迭代变量 i
在你的运动方程中实际上是 t
。每次计算新的 x,y 时,只需将海龟移动到该位置:
import turtle
from math import pi, atan
x0, y0 = -180, -100 # initial location
vx, vy = 20.0, 88.0 # initial velocity in units per second
travel_time = 16 # seconds
g = 11.0 # acceleration due to gravity in units per second squared
turtellini = turtle.Turtle(shape='turtle', visible=False)
turtellini.penup()
turtellini.radians() # to make turtle compatible with math.atan()
turtellini.setheading(pi / 2) # straight up
turtellini.goto(x0, y0)
turtellini.pendown()
turtellini.showturtle()
turtellini.stamp()
for t in range(1, travel_time + 1):
x = x0 + vx * t
y = y0 + vy * t - g / 2 * t**2
turtellini.goto(x, y)
print(x, y)
angle = atan((vy * t - g * t**2) / (vx * t)) # a guess!
turtellini.setheading(angle)
turtellini.stamp()
turtle.exitonclick()
与黄金标准图像不同,我假设乌龟像子弹一样符合空气动力学原理,并且在飞行过程中头部先行。我不知道,也无法快速找到弹丸飞行角度的公式,所以我根据现有公式猜测:
我是 Python 的新手,目前在海龟图形方面遇到了困难。这就是我要解决的问题
On Turtellini (the planet where Python turtles live) the transportation system propels turtles with a giant slingshot. A particular turtle's original location (x0, y0) is (-180, -100). He is then shot upward at an initial vertical velocity (vy) of 88 units per second and a horizontal velocity (vx) of 20 units per second to the right. He travels for 16 seconds. The acceleration due to gravity (g) is 11 units per second squared. The the location of the turtle at a given second (t) is calculated as follows: x = x0 + vx * t and y = y0 + vy * t - g/2 * t2 . This program is to show how a turtle travels over this period of time.
输出应该是这样的:
这是我应该做的;
- 设置常数(垂直速度,水平速度, 重力)和变量(x 和 y 坐标)设置海龟 给他一个合适的形状,把他的尾巴竖起来,把他移到 初始位置,放下他的尾巴做一个重复的循环 第 1 秒到第 16 秒(含)。在循环显示的每次迭代中 x 和 y 变量的值(在 shell window 中),移动 乌龟到那些坐标,让乌龟标记他的形状, 在循环后计算 x 和 y 变量的新值 终止,将海龟移动到最后计算的坐标, 改变他的颜色,标记他的形状,然后等待鼠标点击
到目前为止我的代码:
import turtle
def main():
wn = turtle.Screen()
turtellini = turtle.Turtle()
t = int(input("Blab blab blab: "))
x0 = -180
y0 = -100
vx = 20
vy = 88
g = 11
x = (float(x0 + vx * t))
y = (float(y0 + vy * t - g / 2 * t**2))
turtellini.color("black")
turtellini.shape("turtle")
turtellini.up()
turtellini.goto(-180,-100)
turtellini.down()
for i in range(1,16,1):
turtellini.stamp()
turtellini.forward(i)
turtellini.right(i)
print(x)
print(y)
if __name__ == "__main__":
main()
我知道我做的不好;但是谁能帮我解决这个问题?
你似乎拥有大部分零件。我看到的最大问题是您没有将 x,y 计算放入循环中。循环迭代变量 i
在你的运动方程中实际上是 t
。每次计算新的 x,y 时,只需将海龟移动到该位置:
import turtle
from math import pi, atan
x0, y0 = -180, -100 # initial location
vx, vy = 20.0, 88.0 # initial velocity in units per second
travel_time = 16 # seconds
g = 11.0 # acceleration due to gravity in units per second squared
turtellini = turtle.Turtle(shape='turtle', visible=False)
turtellini.penup()
turtellini.radians() # to make turtle compatible with math.atan()
turtellini.setheading(pi / 2) # straight up
turtellini.goto(x0, y0)
turtellini.pendown()
turtellini.showturtle()
turtellini.stamp()
for t in range(1, travel_time + 1):
x = x0 + vx * t
y = y0 + vy * t - g / 2 * t**2
turtellini.goto(x, y)
print(x, y)
angle = atan((vy * t - g * t**2) / (vx * t)) # a guess!
turtellini.setheading(angle)
turtellini.stamp()
turtle.exitonclick()
与黄金标准图像不同,我假设乌龟像子弹一样符合空气动力学原理,并且在飞行过程中头部先行。我不知道,也无法快速找到弹丸飞行角度的公式,所以我根据现有公式猜测: