试图使圆点轨道更大点

时间:2018-06-03 07:11:29

标签: python python-3.x turtle-graphics

所以我一直试图让一些点不仅走向一个圆圈,而且还让它们围绕它旋转。要做到这一点,我使用余弦和正弦,但是我遇到的问题是让点向前移动以及设置它们的距离。使用下面的代码,点可以围绕较大的点形成一个圆圈,也可以跟随它,但是当它们与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()在与较小的点碰撞时增大较大的点。 Produce()和redraw()应该是程序的第2阶段,但这些函数与问题无关。最后,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()

2 个答案:

答案 0 :(得分:1)

我对此采取了另一个裂缝,只是看着在行星周围蜂拥而至的流星问题。或者在这种情况下,月亮我选择Deimos作为我的模型。我试图按比例工作,使坐标系1像素= 1公里。起初,Deimos坐落在一个流星的领域,每个流星都有一个随机的标题,但它们都具有相同的大小和速度:

from turtle import Turtle, Screen
from random import random

METEOR_VELOCITY = 0.011  # kilometers per second

METEOR_RADIUS = 0.5  # kilometers

SECONDS_PER_FRAME = 1000  # each updates represents this many seconds passed

UPDATES_PER_SECOND = 100

DEIMOS_RADIUS = 6.2  # kilometers

G = 0.000003  # Deimos gravitational constant in kilometers per second squared

CURSOR_SIZE = 20

def follow():

    global meteors

    new_meteors = []

    t = SECONDS_PER_FRAME

    for (x, y), velocity, heading, stamp in meteors:

        meteor.clearstamp(stamp)
        meteor.goto(x, y)
        meteor.setheading(heading)
        meteor.forward(velocity * t)

        meteor.setheading(meteor.towards(deimos))
        meteor.forward(G * t * t)

        meteor.setheading(180 + meteor.towards(x, y))

        if meteor.distance(deimos) > DEIMOS_RADIUS * 2:
            new_meteors.append((meteor.position(), velocity, meteor.heading(), meteor.stamp()))

    screen.update()

    meteors = new_meteors
    if meteors:
        screen.ontimer(follow, 1000 // UPDATES_PER_SECOND)

def create():
    """ create()'s dots with meteor """

    count = 0
    nux, nuy = -400, 300

    while nuy > -400:
        meteor.goto(nux, nuy)

        if meteor.distance(deimos) > DEIMOS_RADIUS * 2:
            heading = random() * 360
            meteor.setheading(heading)  # all meteors have random heading but fixed velocity
            meteors.append((meteor.position(), METEOR_VELOCITY, meteor.heading(), meteor.stamp()))

        nux += 20
        count += 1
        if count % 40 == 0:
            nuy -= 50
            nux = -400

    screen.update()

meteors = []

screen = Screen()
screen.screensize(1000, 1000)
screen.setworldcoordinates(-500, -500, 499, 499)  # 1 pixel = 1 kilometer

meteor = Turtle('circle', visible=False)
meteor.shapesize(2 * METEOR_RADIUS / CURSOR_SIZE)
meteor.speed('fastest')
meteor.color('purple')
meteor.penup()

deimos = Turtle('circle')
deimos.shapesize(2 * DEIMOS_RADIUS / CURSOR_SIZE)
deimos.color("orange")
deimos.penup()

screen.tracer(False)

create()
follow()

screen.mainloop()

要调查的第一个变量是METEOR_VELOCITY。在提供的设置下,大多数流星会撞到月球,但有一些流星会获得轨道速度。如果你减半它的值,所有的流星都将坠入月球。如果你加倍它的值,一些流星获得逃逸速度,离开窗口;少数人可能会撞上月球;大多数将形成一个变得越来越小的轨道云。

我扔了三角函数并恢复到度数而不是弧度。我使用向量加法逻辑来计算运动。

最后,它只是一个粗糙的模型。

答案 1 :(得分:1)

cdlane's代码的def follow()中将180更改为其他一些偏移,例如195,

meteor.setheading(195 + meteor.towards(x, y))
然后,演员不会直接(180度)朝向Deimos,而是会向中心展示一些螺旋运动。

提供了很好的例子!