2
""" turtle-example-suite:
4
tdemo_planets_and_moon.py
6
Gravitational system simulation using the
7
approximation method from Feynman-lectures,
8
p.9-8, using turtlegraphics.
10
Example: heavy central body, light planet,
12
Planet has a circular orbit, moon a stable
13
orbit around the planet.
15
You can hold the movement temporarily by pressing
16
the left mouse button with mouse over the
17
scrollbar of the canvas.
20
from turtle import Shape, Turtle, mainloop, Vec2D as Vec
21
from time import sleep
25
class GravSys(object):
31
for p in self.planets:
34
for i in range(10000):
36
for p in self.planets:
40
def __init__(self, m, x, v, gravSys, shape):
41
Turtle.__init__(self, shape=shape)
46
gravSys.planets.append(self)
47
self.gravSys = gravSys
48
self.resizemode("user")
53
self.v = self.v + 0.5*dt*self.a
56
for planet in self.gravSys.planets:
58
v = planet.pos()-self.pos()
59
a += (G*planet.m/abs(v)**3)*v
63
self.setpos(self.pos() + dt*self.v)
64
if self.gravSys.planets.index(self) != 0:
65
self.setheading(self.towards(self.gravSys.planets[0]))
67
self.v = self.v + dt*self.a
69
## create compound yellow/blue turtleshape for planets
88
planetshape = Shape("compound")
89
planetshape.addcomponent(m1,"orange")
90
planetshape.addcomponent(m2,"blue")
91
s.getscreen().register_shape("planet", planetshape)
94
## setup gravitational system
96
sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle")
100
earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet")
101
earth.pencolor("green")
103
moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet")
104
moon.pencolor("blue")
110
if __name__ == '__main__':