#---------------------------------------------------------------------- # William T. Verts - Fish Tank problem in Object-Oriented form # # Copyright (C) April 28, 2017|December 7, 2018 -- Dr. William T. Verts #---------------------------------------------------------------------- import random,time #---------------------------------------------------------------------- # Standard functions given as part of the assignment #---------------------------------------------------------------------- def INT(N): return int(round(N)) def Circles(Canvas,Xc,Yc,R,FillColor=white,EdgeColor=black): Diameter = INT(2*R) addOvalFilled(Canvas,INT(Xc-R),INT(Yc-R),Diameter,Diameter,FillColor) addOval (Canvas,INT(Xc-R),INT(Yc-R),Diameter,Diameter,EdgeColor) return def Ellipses(Canvas,Xc,Yc,Xr,Yr,FillColor=white,EdgeColor=black): addOvalFilled(Canvas,INT(Xc-Xr),INT(Yc-Yr),INT(2*Xr),INT(2*Yr),FillColor) addOval (Canvas,INT(Xc-Xr),INT(Yc-Yr),INT(2*Xr),INT(2*Yr),EdgeColor) return def Line (Canvas,X1,Y1,X2,Y2,NewColor=black): addLine(Canvas, INT(X1), INT(Y1), INT(X2), INT(Y2), NewColor) return def Fish (Canvas,Xc,Yc,NewColor,Scale=1.0): # FishLeft:Scale<0 FishRight:Scale>0 def F(N): return N*Scale def S(N): return N*abs(Scale) Ellipses(Canvas,Xc-F(14),Yc, S(3), S(14), white, black) # Tail Ellipses(Canvas,Xc, Yc, S(17), S(9),NewColor,black) # Body Circles (Canvas,Xc+F(10),Yc-S(2),S(3), white, black) # Eye Line (Canvas,Xc+F(15),Yc+S(4),Xc+F(7),Yc+S(4),white) # Mouth Line (Canvas,Xc+F(5), Yc+S(6),Xc+F(5),Yc-S(6),white) # Gill return #---------------------------------------------------------------------- # Object-Oriented Fish definition #---------------------------------------------------------------------- class TFish(object): def __init__ (self, NewX, NewY, NewXDir, NewYDir, NewColor, NewScale=1.0): self.X = NewX self.Y = NewY self.XDir = NewXDir self.YDir = NewYDir self.BodyColor = NewColor self.Scale = NewScale return def Paint (self, Canvas): if self.XDir < 0: Fish(Canvas, self.X, self.Y, self.BodyColor, -self.Scale) else: Fish(Canvas, self.X, self.Y, self.BodyColor, self.Scale) return def Update (self, XMin, YMin, XMax, YMax): self.X = self.X + self.XDir self.Y = self.Y + self.YDir if (self.X < XMin): self.XDir = +1 elif (self.X > XMax): self.XDir = -1 if (self.Y < YMin): self.YDir = +1 elif (self.Y > YMax): self.YDir = -1 if random.randrange(10) == 0: self.XDir = random.randrange(-1,+2) if random.randrange(20) == 0: self.YDir = random.randrange(-1,+2) return #---------------------------------------------------------------------- # Main program #---------------------------------------------------------------------- def FishTank(Canvas, TotalFish=20): TankColor = makeColor(0,0,128) Colors = [black,blue,green,cyan,red,magenta,yellow] Scales = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] W = getWidth(Canvas) H = getHeight(Canvas) AllFish = [] for I in range(TotalFish): NewX = W/2 + random.randrange(-25,+26) NewY = H/2 + random.randrange(-25,+26) NewXDir = random.randrange(-1,2) NewYDir = random.randrange(-1,2) NewColor = random.choice(Colors) NewScale = random.choice(Scales) AllFish = AllFish + [TFish(NewX,NewY,NewXDir,NewYDir,NewColor,NewScale)] while True: setAllPixelsToAColor(Canvas,TankColor) for AFish in AllFish: AFish.Paint(Canvas) # Same as: TFish.Paint(AFish,Canvas) AFish.Update(0,0,W,H) # Same as: TFish.Update(AFish,0,0,W,H) repaint(Canvas) time.sleep(0.05) return def Main(): TotalFish = requestIntegerInRange("Enter Number of Fish", 1, 1000) FishTank(makeEmptyPicture(640,480), TotalFish) return