所以我一直在尝试让一些点不仅朝向一个圆,而且让它们环绕一个圆。为了做到这一点,我使用余弦和正弦,但我遇到了问题,让点向前移动以及设置他们的距离。有了下面的代码,圆点可以围绕着大点形成一个圆,也可以跟随它,但它们不会接近圆点,也不会,当坐标按它们与t1的距离缩放时,到达那个位置,而是做一些有趣的事情。这是指具体的行
t2.goto(2 * (t1.xcor() + math.degrees(math.cos(math.radians(t1.towards(t2)))) // 1), 2 * (t1.ycor() + math.degrees(math.sin(math.radians(t1.towards(t2)))) // 1))
我将其替换为:
t2.goto(dist * (t1.xcor() + math.degrees(math.cos(math.radians(t1.towards(t2)))) // 1), dist * (t1.ycor() + math.degrees(math.sin(math.radians(t1.towards(t2)))) // 1))
这让我看到了零星的点,试图跟随更大的点。你知道吗
这一行可以在follow()函数中找到。Create()生成较小的点,move()移动较大的点,grow()在与较小的点碰撞时生成较大的点。product()和redraw()应该是程序的第二阶段,但是这些函数与问题无关。最后,quit()退出Screen()并退出程序。你知道吗
感谢cdlane帮助您更有效地组织数据和更新屏幕。你知道吗
目前代码:
from turtle import Turtle, Screen
import sys
import math
CURSOR_SIZE = 20
def move(x, y):
""" has it follow cursor """
t1.ondrag(None)
t1.goto(x, y)
screen.update()
t1.ondrag(move)
def grow():
""" grows t1 shape """
global t1_size, g
t1_size += 0.1
t1.shapesize(t1_size / CURSOR_SIZE)
g -= .1
t1.color((r/255, g/255, b/255))
screen.update()
def follow():
""" has create()'d dots follow t1 """
global circles, dist
new_circles = []
for (x, y), stamp in circles:
t2.clearstamp(stamp)
t2.goto(x, y)
dist = t2.distance(t1) / 57.29577951308232 // 1
t2.goto(2 * (t1.xcor() + math.degrees(math.cos(math.radians(t1.towards(t2)))) // 1), 2 * (t1.ycor() + math.degrees(math.sin(math.radians(t1.towards(t2)))) // 1))
t2.setheading(t2.towards(t1))
if t2.distance(t1) < t1_size // 1:
if t2.distance(t1) > t1_size * 1.2:
t2.forward(500/t2.distance(t1)//1)
else:
t2.forward(3)
if t2.distance(t1) > t1_size // 2:
new_circles.append((t2.position(), t2.stamp()))
else:
grow() # we ate one, make t1 fatter
screen.update()
circles = new_circles
if circles:
screen.ontimer(follow, 10)
else:
phase = 1
produce()
def create():
""" create()'s dots with t2 """
count = 0
nux, nuy = -400, 300
while nuy > -400:
t2.goto(nux, nuy)
if t2.distance(t1) > t1_size // 2:
circles.append((t2.position(), t2.stamp()))
nux += 20
count += 1
if count == 40:
nuy -= 50
nux = -400
count = 0
screen.update()
def quit():
screen.bye()
sys.exit(0)
def redraw():
t2.color("black")
t2.shapesize((t2_size + 4) / CURSOR_SIZE)
t2.stamp()
t2.shapesize((t2_size + 2) / CURSOR_SIZE)
t2.color("white")
t2.stamp()
def produce():
#create boundary of star
global t2_size, ironmax
t1.ondrag(None)
t1.ht()
t2.goto(t1.xcor(), t1.ycor())
t2.color("black")
t2.shapesize((t1_size + 4) / CURSOR_SIZE)
t2.stamp()
t2.shapesize((t1_size + 2) / CURSOR_SIZE)
t2.color("white")
t2.stamp()
#start producing helium
while t2_size < t1_size:
t2.color("#ffff00")
t2.shapesize(t2_size / 20)
t2.stamp()
t2_size += .1
redraw()
screen.update()
ironmax = t2_size
t2_size = 4
while t2_size < ironmax:
t2.shapesize(t2_size / 20)
t2.color("grey")
t2.stamp()
t2_size += .1
screen.update()
# variables
t1_size = 6
circles = []
phase = 0
screen = Screen()
screen.screensize(900, 900)
#screen.mode("standard")
t2 = Turtle('circle', visible=False)
t2.shapesize(4 / CURSOR_SIZE)
t2.speed('fastest')
t2.color('purple')
t2.penup()
t2_size = 4
t1 = Turtle('circle')
t1.shapesize(t1_size / CURSOR_SIZE)
t1.speed('fastest')
r = 190
g = 100
b = 190
t1.color((r/255, g/255, b/255))
t1.penup()
t1.ondrag(move)
screen.tracer(False)
screen.listen()
screen.onkeypress(quit, "Escape")
create()
follow()
#print(phase)
screen.mainloop()
在cdlane's代码的
def follow()
中,通过将180更改为一些其他偏移量,例如195然后,Metor不会笔直(180度)朝向Deimos,而是向中心显示一些螺旋运动。你知道吗
提供了很好的例子!你知道吗
我又对这个问题进行了一次破解,只是看看流星围绕着一颗行星的问题。或者在这种情况下,月亮作为我选择的代莫斯作为我的模型。我试图按比例工作,使坐标系为1像素=1公里。起初,Deimos位于一片流星场中,每颗流星都有一个随机的航向,但它们的大小和速度都相同:
要研究的第一个变量是
METEOR_VELOCITY
。在所提供的背景下,大多数流星会撞上月球,但也有少数流星会获得轨道速度。如果你把它的价值减半,所有的流星都会撞上月球。如果你把它的值翻一番,少数流星会获得逃逸速度,离开窗口;少数流星可能撞上月球;大多数流星会形成一个轨道云,变得越来越小,越来越紧。你知道吗我扔掉了三角函数的东西,又回到度而不是弧度。我用向量加法逻辑来计算运动。你知道吗
最后,这只是一个粗糙的模型。你知道吗
相关问题 更多 >
编程相关推荐