ยปCore Development>Code coverage>Lib/turtledemo/planet_and_moon.py

Python code coverage for Lib/turtledemo/planet_and_moon.py

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