-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpet.py
149 lines (114 loc) · 4.35 KB
/
pet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
"""
.. module: pet.py
:platform: Windows, Linux, OSX
:synopsis: A brief description of the function.
.. moduleauthor:: Mike Qin <laigui@gmail.com>
"""
# ============================================================================
#% Imports
# ============================================================================
from random import randrange
# ============================================================================
#% Classes
# ============================================================================
class Pet():
"""A brief description of the class."""
boredom_decrement = 4
hunger_decrement = 6
boredom_threshold = 5
hunger_threshold = 10
sounds = ['Mrrp']
def __init__(self, name="Kitty"):
self.name = name
self.hunger = randrange(self.hunger_threshold)
self.boredom = randrange(self.boredom_threshold)
self.sounds = self.sounds[:]
def clock_tick(self):
self.boredom += 1
self.hunger += 1
def mood(self):
if self.hunger <= self.hunger_threshold and self.boredom <= self.boredom_threshold:
return "happy"
elif self.hunger > self.hunger_threshold:
return "hungry"
else:
return "bored"
def __str__(self):
state = " I'm " + self.name + ". "
state += " I feel " + self.mood() + ". "
# state += "Hunger {} Boredom {} Words {}".format(self.hunger, self.boredom, self.sounds)
return state
def hi(self):
print(self.sounds[randrange(len(self.sounds))])
self.reduce_boredom()
def teach(self, word):
self.sounds.append(word)
self.reduce_boredom()
def feed(self):
self.reduce_hunger()
def reduce_hunger(self):
self.hunger = max(0, self.hunger - self.hunger_decrement)
def reduce_boredom(self):
self.boredom = max(0, self.boredom - self.boredom_decrement)
class Cat(Pet):
sounds = ['Meow']
def mood(self):
if self.hunger > self.hunger_threshold:
return "hungry"
if self.boredom <2:
return "grumpy; leave me alone"
elif self.boredom > self.boredom_threshold:
return "bored"
elif randrange(2) == 0:
return "randomly annoyed"
else:
return "happy"
def chasing_rats(self):
return "What are you doing, Pinky? Taking over the world?!"
class Dog(Pet):
sounds = ['Woof', 'Ruff']
def mood(self):
if (self.hunger > self.hunger_threshold) and (self.boredom > self.boredom_threshold):
return "bored and hungry"
else:
return "happy"
class Cheshire(Cat): # this inherits from Cat, which inherits from Pet
def smile(self): # this method is specific to instances of Cheshire
print(":D :D :D")
class Bird(Pet):
sounds = ["chirp"]
def __init__(self, name="Kitty", chirp_number=2):
Pet.__init__(self, name) # call the parent class's constructor
# basically, call the SUPER -- the parent version -- of the constructor, with all the parameters that it needs.
self.chirp_number = chirp_number # now, also assign the new instance variable
def hi(self):
for i in range(self.chirp_number):
print(self.sounds[randrange(len(self.sounds))])
self.update_boredom()
# ============================================================================
#% Main Entry
# ============================================================================
if __name__ == "__main__":
p1 = Pet("Fido")
print(p1) # we've seen this stuff before!
p1.feed()
p1.hi()
print(p1)
c1 = Cat("Fluffy")
print(c1) # this uses the same __str__ method as the Pets do
c1.feed() # Totally fine, because the cat class inherits from the Pet class!
c1.hi()
print(c1)
print(c1.chasing_rats())
new_cat = Cheshire("Pumpkin") # create a Cheshire cat instance with name "Pumpkin"
new_cat.hi() # same as Cat!
new_cat.chasing_rats() # OK, because Cheshire inherits from Cat
new_cat.smile() # Only for Cheshire instances (and any classes that you make inherit
d1 = Dog("Astro")
c1.boredom = 1
print(c1.mood())
c1.boredom = 3
for i in range(10):
print(c1.mood())
print(d1.mood())