import graphics import random # Definition of the pet class class pet: #shared values go here maxcrowding = 10 mutRange = 0.1 # range of values for mutation # __init__ function initializes/creates new pets def __init__(self,n = None): # each pet has its own value: if(n == None): n = input("Pet's name? ") self.name = n # the name is passed in as an argument self.hungry = 50 self.happy = 50 # different pets metabolize food with different effciencies self.metabolismMeat = random.uniform(0,1) self.metabolismVegitables = random.uniform(0,1) def printPet(self): print(self.name," is ") print("Hunger: ",self.hungry) print("Happy: ",self.happy) print("Carnivore: ",self.metabolismMeat) cir = Circle(Point(100,50), 25) r = 128*self.metabolismMeat g = 128*self.metabolismVegitables cir = Circle(Point(100,50), 25) cir.setFill(color_rgb(r,g,128)) cir.draw(win) cir2 = Circle(Point(90,40), 3) cir2.setFill('black') cir2.draw(win) cir3 = Circle(Point(110,40), 3) cir3.setFill('black') cir3.draw(win) def printName(self): print(self.name) def feedMeat(self): self.hungry -= (5 * self.metabolismMeat) print(self.name, " says 'Yum' ") def feedVegitables(self): self.hungry -= (5 * self.metabolismVegitables) print(self.name, " says 'Yummy' ") # Play makes a pet happier and hungrier def play(self): self.happy += 5 self.hungry += 3 def reproduce(self): temp = pet() temp.metabolismMeat = self.metabolismMeat temp.metabolismVegitables = self.metabolismVegitables temp.mutate() return temp def mutate(self): self.metabolismMeat += random.uniform(-1*self.mutRange, self.mutRange) if(self.metabolismMeat < 0): # limits on allowed values self.metabolismMeat = 0 self.metabolismVegitables += random.uniform(-1*self.mutRange, self.mutRange) from graphics import * win = GraphWin() # Create a list pets = [] # Add a first pet pets.append(pet()) choice = '0' while(choice != 'q'): for i in range(0, len(pets) ): print(i, end = " ") pets[i].printName() # list the pets petNumber = int(input("Which pet? ")) pets[petNumber].printPet() # detailed info on selected pet choice = input("a - feed steak, b - feed carrot, c - play, r - reproduce, q - quit: ") if(choice == 'a'): pets[petNumber].feedMeat() if(choice == 'b'): pets[petNumber].feedVegitables() if(choice == 'r'): pets.append(pets[petNumber].reproduce()) if(choice == 'd'): # d for delete pets.remove(pets[petNumber]) win.close()