-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtamapy.py
More file actions
271 lines (214 loc) · 6.61 KB
/
Copy pathtamapy.py
File metadata and controls
271 lines (214 loc) · 6.61 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import json
import os
from datetime import datetime, timedelta
SAVE_FILE = "tamapy_state.json"
def print_squares(stat_value, stat_name):
"""
Render a bar from 0–6 for a given stat name.
stat_value: int in [0,6]
stat_name: one of "happy", "full", "clean", "health"
"""
stat_value = max(0, min(6, stat_value))
label_map = {
"happy": "HAPPY",
"full": "FULL",
"clean": "CLEAN",
"health": "HEALTHY",
}
label = label_map.get(stat_name, stat_name.upper())
filled = "■" * stat_value
empty = "□" * (6 - stat_value)
if stat_value == 0:
icon = "💀"
elif stat_value <= 2:
icon = "🔴"
elif stat_value <= 4:
icon = "🟠"
else:
icon = "🟢"
print(f"{icon} {label:<7} {filled}{empty}")
class Tamagotchi:
"""
Tick-based Tamagotchi with slow decay.
Stats:
happy, full, clean, health: 0–6
Hidden stat:
poo: 0–6 (NOT shown to user, NOT saved)
Death condition:
happy == 0 AND health == 0 AND full == 0 AND clean == 0
Age:
+1 pet year every 5 minutes of real time (only while game is running)
"""
def __init__(self, name: str):
self.name = name
self.start = datetime.now()
self.now = self.start
# Stats
self.happy = 6
self.full = 6
self.clean = 6
self.health = 6
# Hidden stat
self.poo = 0
# Age system
self.age_years = 0
self.last_age_update = self.start
# Tick counter
self.tick_count = 0
@property
def is_dead(self):
return (
self.happy == 0
and self.health == 0
and self.full == 0
and self.clean == 0
)
def _dec_stat(self, attr):
value = getattr(self, attr)
if value > 0:
setattr(self, attr, value - 1)
def _inc_stat(self, attr):
value = getattr(self, attr)
if value < 6:
setattr(self, attr, value + 1)
# Actions
def feed(self):
if not self.is_dead:
self._inc_stat("full")
def clean_poo(self):
if not self.is_dead:
if self.poo > 0:
self.poo -= 1
self._inc_stat("clean")
def take_medicine(self):
"""
Medicine heals AND cures sickness by resetting poo.
"""
if not self.is_dead:
self._inc_stat("health")
self.poo = 0 # cure sickness source
def play(self):
if not self.is_dead:
self._inc_stat("happy")
# Age system
def update_age(self):
now = datetime.now()
elapsed_minutes = (now - self.last_age_update).total_seconds() / 60
if elapsed_minutes >= 5:
gained_years = int(elapsed_minutes // 5)
self.age_years += gained_years
self.last_age_update = now
# Tick logic
def tick(self):
if self.is_dead:
return
self.tick_count += 1
self.now = datetime.now()
# Update age
self.update_age()
# Poo increases every 2 ticks (hidden)
if self.tick_count % 2 == 0 and self.poo < 6:
self.poo += 1
# Natural sickness: poo >= 3
if self.poo >= 3:
self._dec_stat("health")
# Slow decay: rotate stats
order = ["happy", "full", "clean", "health"]
idx = (self.tick_count - 1) % len(order)
target = order[idx]
if target == "health":
if self.health <= 2 and self.health > 0:
self._dec_stat("health")
else:
self._dec_stat(target)
# If poo maxed, cleanliness drops
if self.poo == 6 and self.clean > 0:
self._dec_stat("clean")
# Persistence
def to_dict(self):
return {
"name": self.name,
"start": self.start.isoformat(),
"now": self.now.isoformat(),
"happy": self.happy,
"full": self.full,
"clean": self.clean,
"health": self.health,
"tick_count": self.tick_count,
"age_years": self.age_years,
"last_age_update": self.last_age_update.isoformat(),
}
@classmethod
def from_dict(cls, data: dict):
obj = cls(data["name"])
obj.start = datetime.fromisoformat(data["start"])
obj.now = datetime.fromisoformat(data["now"])
obj.happy = data["happy"]
obj.full = data["full"]
obj.clean = data["clean"]
obj.health = data["health"]
obj.tick_count = data.get("tick_count", 0)
obj.age_years = data.get("age_years", 0)
obj.last_age_update = datetime.fromisoformat(
data.get("last_age_update", data["now"])
)
# Hidden stat resets on load
obj.poo = 0
return obj
def save(self, path: str = SAVE_FILE):
with open(path, "w", encoding="utf-8") as f:
json.dump(self.to_dict(), f, indent=2)
@staticmethod
def load(path: str = SAVE_FILE):
if not os.path.exists(path):
return None
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return Tamagotchi.from_dict(data)
# UI
def print_status(self):
print(f"\n=== {self.name}'s status ===")
print(f"Age: {self.age_years} 🐾")
print_squares(self.happy, "happy")
print_squares(self.full, "full")
print_squares(self.clean, "clean")
print_squares(self.health, "health")
if self.is_dead:
print("\n💀 Your Tamapy has died. Game over.\n")
def main():
print("🐣 Welcome to Tamapy!\n")
tama = Tamagotchi.load()
if tama:
print(f"Loaded existing Tamapy: {tama.name}")
else:
name = input("Choose a name for your Tamapy: ").strip()
if not name:
name = "Tamapy"
tama = Tamagotchi(name)
tama.save()
while True:
tama.print_status()
if tama.is_dead:
break
print("What do you want to do?")
print("[f]eed [p]lay [c]lean [m]edicine [q]uit")
choice = input("> ").strip().lower()
if choice == "f":
tama.feed()
elif choice == "p":
tama.play()
elif choice == "c":
tama.clean_poo()
elif choice == "m":
tama.take_medicine()
elif choice == "q":
print("Saving and quitting...")
tama.save()
break
else:
print("Unknown action.")
continue
tama.tick()
tama.save()
if __name__ == "__main__":
main()