from tkinter import * class SimWorld: def __init__(self, mainWin,width=500, height=500): self.agents = [] self.width = width self.height = height self.win = mainWin self.canvas = Canvas(mainWin, width=self.width, height=self.height, bg = "white") self.canvas.pack() def addAgent(self, agent): self.agents.append(agent) agent.setWorld(self) def doAction(self): for agent in self.agents: agent.action() class Animal: speed = 5 color = "" def __init__(self, xLoc,yLoc): self.xLoc = xLoc self.yLoc = yLoc def action(self): self.randomMove() self.speak() def speak(self): print("hi I'm an Animal!", self.xLoc, self.yLoc) def setWorld(self, world): self.myWorld = world self.draw() #This function moves the animal a random direction at it's #speed. def randomMove(self): #Move the self.iconID on the canvas! pass def draw(self): print("draw!") c = self.myWorld.canvas bbox = (self.xLoc-4, self.yLoc-4, self.xLoc+4, self.yLoc+4) self.iconID = c.create_oval( bbox, fill= self.color) class Fox(Animal): speed = 8 color = "red" def __init__(self,xLoc,yLoc): super().__init__(xLoc,yLoc) def speak(self): print("Fox!", self.xLoc, self.yLoc) class Rabbit(Animal): speed = 3 color = "white" def __init__(self,xLoc,yLoc): super().__init__(xLoc,yLoc) W = Tk() sw = SimWorld(W, 300,200) sw.addAgent( Fox(10,15)) sw.addAgent( Animal(5,5) ) W.mainloop()