Code for first python lab

lab1.py  [download]

# Weird import for compatibility across Python 2 and Python 3
try:   
	import tkinter   
except ImportError:
	import Tkinter as tkinter   

############################################################
# PLANE CLASS
############################################################

class Plane ():
	def __init__(self, x, y, dx, dy):
		# Stash args as ivars
		self._x = x
		self._y = y
		self._dx = dx
		self._dy = dy

	# "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._dx
		self._y += self._dy
		
############################################################
# MAIN PROGRAM 
############################################################

# Create map (just a list)
planes = list()

# Create some planes in the map
planes.append (Plane (30, 20, 1, 2))
planes.append (Plane (100, 20, 2, 1))
planes.append (Plane (140, 40, 1, 1))

# Unit test of Plane class
print ("\nInitial:")
for p in planes:
	p.draw()

print ("\nAfter 1 tick:")
for p in planes:
	p.tick()
	p.draw()

print ("\nAfter 2 ticks:")
for p in planes:
	p.tick()
	p.draw()

# Primitive animation

# Receive tick, pass it to objects that need it,
# then set up the next callback in 1 second
def tick():
	print ("\nAnimating:")
	for p in planes:
		p.tick()
		p.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()