3
# [SNIPPET_NAME: Turtle Gravity]
4
# [SNIPPET_CATEGORIES: PyTurtle]
5
# [SNIPPET_DESCRIPTION: Use turtle to move a moon and planet around a star.]
6
# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html]
7
# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>]
8
# [SNIPPET_LICENSE: PSF]
10
""" turtle-example-suite:
12
tdemo_planets_and_moon.py
14
Gravitational system simulation using the
15
approximation method from Feynman-lectures,
16
p.9-8, using turtlegraphics.
18
Example: heavy central body, light planet,
20
Planet has a circular orbit, moon a stable
21
orbit around the planet.
23
You can hold the movement temporarily by pressing
24
the left mouse button with mouse over the
25
scrollbar of the canvas.
28
from turtle import Shape, Turtle, mainloop, Vec2D as Vec
29
from time import sleep
33
class GravSys(object):
39
for p in self.planets:
42
for i in range(10000):
44
for p in self.planets:
48
def __init__(self, m, x, v, gravSys, shape):
49
Turtle.__init__(self, shape=shape)
54
gravSys.planets.append(self)
55
self.gravSys = gravSys
56
self.resizemode("user")
61
self.v = self.v + 0.5*dt*self.a
64
for planet in self.gravSys.planets:
66
v = planet.pos()-self.pos()
67
a += (G*planet.m/abs(v)**3)*v
71
self.setpos(self.pos() + dt*self.v)
72
if self.gravSys.planets.index(self) != 0:
73
self.setheading(self.towards(self.gravSys.planets[0]))
75
self.v = self.v + dt*self.a
77
## create compound yellow/blue turtleshape for planets
96
planetshape = Shape("compound")
97
planetshape.addcomponent(m1,"orange")
98
planetshape.addcomponent(m2,"blue")
99
s.getscreen().register_shape("planet", planetshape)
102
## setup gravitational system
104
sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle")
108
earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet")
109
earth.pencolor("green")
111
moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet")
112
moon.pencolor("blue")
118
if __name__ == '__main__':