from graphics import* from math import* from random import* class Face: def __init__(self,win,center,size): self.eyeSize = 0.15*size self.eyeOff = size/3.0 mouthSize = 0.8*size mouthOff = size/2.0 self.size = size self.center = center self.win = win self.head = Circle(self.center,self.size) self.head.draw(win) self.leftEye = Circle(self.center,self.eyeSize) self.leftEye.move(-self.eyeOff,-self.eyeOff) self.rightEye = Circle(self.center,self.eyeSize) self.rightEye.move(self.eyeOff,-self.eyeOff) self.leftEye.draw(win) self.rightEye.draw(win) p1 = self.center.clone() p1.move(-mouthSize/2,mouthOff) p2 = self.center.clone() p2.move(mouthSize/2,mouthOff) self.mouth = Line(p1,p2) self.mouth.draw(win) def moveface(self,x,y): self.head.move(x,y) self.leftEye.move(x,y) self.rightEye.move(x,y) self.mouth.move(x,y) self.center.move(x,y) def getCenter(self): return self.center def flinch(self): self.leftEye.undraw() self.leftEye = Line(Point(self.center.getX()-self.eyeSize,self.center.getY()),Point(self.center.getX()+self.eyeSize,self.center.getY())) self.leftEye.move(-self.eyeOff,-self.eyeOff) self.leftEye.draw(self.win) self.rightEye.undraw() self.rightEye = Line(Point(self.center.getX()-self.eyeSize,self.center.getY()),Point(self.center.getX()+self.eyeSize,self.center.getY())) self.rightEye.move(self.eyeOff,-self.eyeOff) self.rightEye.draw(self.win) def unflinch(self): self.leftEye.undraw() self.rightEye.undraw() self.leftEye = Circle(self.center,self.eyeSize) self.leftEye.move(-self.eyeOff,-self.eyeOff) self.rightEye = Circle(self.center,self.eyeSize) self.rightEye.move(self.eyeOff,-self.eyeOff) self.leftEye.draw(self.win) self.rightEye.draw(self.win) def main(): win = GraphWin("",300,300) center = Point(150,150) size = 20 face = Face(win,Point(280*random(),280*random()),size) i = 0 x = 0.1 y = 0.2 face.flinch() win.getMouse() face.unflinch() for i in range(100000): face.unflinch() face.moveface(x,y) center = face.getCenter() if center.getX() +size>=300 or center.getX() -size<=0: x = -x face.flinch() if center.getY() +size>=300 or center.getY() -size<=0: y = -y face.flinch() win.flush() main()