-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplanets.py
36 lines (32 loc) · 1.12 KB
/
planets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import threading
import turtle
import mttkinter
class Planet(turtle.Turtle):
def __init__(self, y, speed, orbit):
super().__init__()
self.shape('circle')
self.pu()
self.sety(y)
self.pd()
self.speed(speed)
self.orbit = orbit
def revolve(self):
self.circle(self.orbit)
def planets():
"""simulates motion of Mercury, Venus, Earth, and Mars"""
mercury = Planet(y=-58, speed=7.5, orbit=58)
venus = Planet(y=-108, speed=3, orbit=108)
earth = Planet(y=-150, speed=2, orbit=150)
mars = Planet(y=-228, speed=1, orbit=228)
planets = (mercury, venus, earth, mars)
for planet in planets:
planet.thread = threading.Thread(target=planet.revolve)
planet.thread.start()
# We have to do at least one more turtle (or tkinter)
# operation after starting the threads, or the main
# thread will try to exit immediately, which will
# cause it to block on the background threads, which
# will cause the background threads to queue up events
# that never get run, and hello deadlock.
turtle.Screen().exitonclick()
planets()