import tkinter
import math
############################################################
# PLANE CLASS
############################################################
class Plane ():
def __init__(self, map, x, y, direction, speed):
# Stash args as ivars
self._x = x
self._y = y
self._direction = direction
self._speed = speed
# Install in map
map.append (self)
# "Draw" ourself
def draw (self):
print ("Plane at " + str(self._x) + "," + str(self._y))
# Timer callback dispatched to us
def tick (self):
self._x += self._speed * math.cos (math.radians(360 + 90 - self._direction))
self._y -= self._speed * math.sin (math.radians(360 + 90 - self._direction))
############################################################
# HELICOPTER CLASS
############################################################
class Helicopter(Plane):
def __init__(self, map, x, y, direction, speed):
Plane.__init__ (self, map, x, y, direction, speed)
def draw (self):
print ("Helicopter at " + str(self._x) + "," + str(self._y))
############################################################
# MAP CLASS
############################################################
class Map():
def __init__(self):
self._planes = list()
def append(self, plane):
self._planes.append (plane)
def draw (self):
for p in self._planes:
p.draw()
# Receive tick, pass it to our planes
def tick(self):
for p in self._planes:
p.tick()
############################################################
# MAIN PROGRAM
############################################################
map = Map()
# Create some planes in the map
Plane (map, 30, 20, 0, 1)
Plane (map, 100, 20, 45, 5)
Plane (map, 140, 40, 90, 2)
Helicopter (map, 200, 30, 180, 1)
Helicopter (map, 240, 60, 300, 3)
print ("\nInitial:")
map.draw()
print ("\nAfter 1 tick:")
map.tick()
map.draw()
print ("\nAfter 2 ticks:")
map.tick()
map.draw()
# Receive tick, pass it to map,
# then set up the next callback in 1 second
def tick():
print ("\nAnimating:")
map.tick()
map.draw()
top.after(1000, tick)
# Uses the after() function from tkinter
# Also creates useless main window here
top = tkinter.Tk()
# Commented out initially so can run on interactive command line
# top.after(1000, tick)
# top.mainloop()