~kkissling/python-snippets/testzweig

« back to all changes in this revision

Viewing changes to pyturtle/tdemo_planet_and_moon.py

  • Committer: Grant Bowman
  • Date: 2010-04-19 11:22:17 UTC
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: grantbow@gmail.com-20100419112217-rw0i2xlr622wc3ag
rough conversion

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
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]
 
9
 
 
10
"""       turtle-example-suite:
 
11
 
 
12
        tdemo_planets_and_moon.py
 
13
 
 
14
Gravitational system simulation using the
 
15
approximation method from Feynman-lectures,
 
16
p.9-8, using turtlegraphics.
 
17
 
 
18
Example: heavy central body, light planet,
 
19
very light moon!
 
20
Planet has a circular orbit, moon a stable
 
21
orbit around the planet.
 
22
 
 
23
You can hold the movement temporarily by pressing
 
24
the left mouse button with mouse over the
 
25
scrollbar of the canvas.
 
26
 
 
27
"""
 
28
from turtle import Shape, Turtle, mainloop, Vec2D as Vec
 
29
from time import sleep
 
30
 
 
31
G = 8
 
32
 
 
33
class GravSys(object):
 
34
    def __init__(self):
 
35
        self.planets = []
 
36
        self.t = 0
 
37
        self.dt = 0.01
 
38
    def init(self):
 
39
        for p in self.planets:
 
40
            p.init()
 
41
    def start(self):
 
42
        for i in range(10000):
 
43
            self.t += self.dt
 
44
            for p in self.planets:
 
45
                p.step()
 
46
 
 
47
class Star(Turtle):
 
48
    def __init__(self, m, x, v, gravSys, shape):
 
49
        Turtle.__init__(self, shape=shape)
 
50
        self.penup()
 
51
        self.m = m
 
52
        self.setpos(x)
 
53
        self.v = v
 
54
        gravSys.planets.append(self)
 
55
        self.gravSys = gravSys
 
56
        self.resizemode("user")
 
57
        self.pendown()
 
58
    def init(self):
 
59
        dt = self.gravSys.dt
 
60
        self.a = self.acc()
 
61
        self.v = self.v + 0.5*dt*self.a
 
62
    def acc(self):
 
63
        a = Vec(0,0)
 
64
        for planet in self.gravSys.planets:
 
65
            if planet != self:
 
66
                v = planet.pos()-self.pos()
 
67
                a += (G*planet.m/abs(v)**3)*v
 
68
        return a
 
69
    def step(self):
 
70
        dt = self.gravSys.dt
 
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]))
 
74
        self.a = self.acc()
 
75
        self.v = self.v + dt*self.a
 
76
 
 
77
## create compound yellow/blue turtleshape for planets
 
78
 
 
79
def main():
 
80
    s = Turtle()
 
81
    s.reset()
 
82
    s.tracer(0,0)
 
83
    s.ht()
 
84
    s.pu()
 
85
    s.fd(6)
 
86
    s.lt(90)
 
87
    s.begin_poly()
 
88
    s.circle(6, 180)
 
89
    s.end_poly()
 
90
    m1 = s.get_poly()
 
91
    s.begin_poly()
 
92
    s.circle(6,180)
 
93
    s.end_poly()
 
94
    m2 = s.get_poly()
 
95
 
 
96
    planetshape = Shape("compound")
 
97
    planetshape.addcomponent(m1,"orange")
 
98
    planetshape.addcomponent(m2,"blue")
 
99
    s.getscreen().register_shape("planet", planetshape)
 
100
    s.tracer(1,0)
 
101
 
 
102
    ## setup gravitational system
 
103
    gs = GravSys()
 
104
    sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle")
 
105
    sun.color("yellow")
 
106
    sun.shapesize(1.8)
 
107
    sun.pu()
 
108
    earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet")
 
109
    earth.pencolor("green")
 
110
    earth.shapesize(0.8)
 
111
    moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet")
 
112
    moon.pencolor("blue")
 
113
    moon.shapesize(0.5)
 
114
    gs.init()
 
115
    gs.start()
 
116
    return "Done!"
 
117
 
 
118
if __name__ == '__main__':
 
119
    msg = main()
 
120
    print msg
 
121
    mainloop()