# # Jay Summet # CS 1301 - Object Oriented Programming Example # Released to the public domain, November 2007 # from myro import * class Animal: xLoc = 0 yLoc = 0 moveSpeed = 3 def __init__(self, X=0, Y=0): self.xLoc = X self.yLoc = Y def __str__(self): return "Animal at: " + str(self.xLoc) +","+ str(self.yLoc) def action(self): self.move() def move(self): from random import choice moveX = choice( [ -self.moveSpeed, 0, self.moveSpeed]) moveY = choice( [ -self.moveSpeed, 0, self.moveSpeed]) self.xLoc = self.xLoc + moveX self.yLoc = self.yLoc + moveY if self.xLoc > 99: self.xLoc = 99 if self.xLoc < 0: self.xLoc = 0 if self.yLoc > 99: self.yLoc = 99 if self.yLoc < 0: self.yLoc = 0 def drawOnPicture(self, Pic): pix = getPixel(Pic, self.xLoc, self.yLoc) setRed(pix,255) setBlue(pix,255) setGreen(pix,255) pix = getPixel(Pic,self.xLoc+1, self.yLoc+1) setRed(pix,255) setBlue(pix,255) setGreen(pix,255) class Rabit(Animal): def drawOnPicture(self, Pic): Animal.drawOnPicture(self, Pic) pix = getPixel(Pic, self.xLoc+1, self.yLoc) setBlue(pix,255) pix = getPixel(Pic,self.xLoc, self.yLoc+1) setBlue(pix,255) class Fox(Animal): moveSpeed = 6 def drawOnPicture(self, Pic): Animal.drawOnPicture(self, Pic) pix = getPixel(Pic, self.xLoc+1, self.yLoc) setRed(pix,255) pix = getPixel(Pic,self.xLoc, self.yLoc+1) setRed(pix,255) animals = [] for i in range(50): animals.append( Rabit(50,50)) for i in range(50): animals.append( Fox(10,10)) display = makePicture(101,101) #One pixel extra because animals #use a 2x2 square... for i in range(15): copyPic = copyPicture(display) for ani in animals: ani.drawOnPicture(copyPic) ani.action() show(copyPic) wait(0.5)