import time import os import json from pathlib import Path class Pet: def __init__(self, name, species): self.name = name self.species = species self.hunger = 50 self.happiness = 80 self.energy = 100 self.hygiene = 100 self.is_alive = True self.birthday = time.time() self.status = "Happy" def rename(self): print("Name your pet: ", end="") name = input()[0:10] self.name = name def feed(self): self.hunger = max(0, self.hunger - 25) def play(self): self.happiness = min(100, self.happiness + 5) def clean(self): self.hygiene = min(100, self.hygiene + 13) def decay(self): self.happiness = max(0, self.happiness - 5) self.energy = max(0, self.energy - 3) self.hunger = min(100, self.hunger + 2) self.hygiene = max(0, self.hygiene - 4) def sleep(self): self.energy = 100 self.status = "asleep" def get_age(self): print(time.time() - self.birthday) def make_bar(self, percent): bar = "[" + "X" * (percent//10) + "-" * (10-percent//10) + "]" return bar def stat_format(self, stat): if stat == "Energy": bar = self.make_bar(self.energy) per = self.energy elif stat == "Happiness": bar = self.make_bar(self.happiness) per = self.happiness elif stat == "Hunger": bar = self.make_bar(self.hunger) per = self.hunger elif stat == "Hygiene": bar = self.make_bar(self.hygiene) per = self.hygiene else: return "You requested a stat that doesn't exist: {stat}" line = "{:<10}".format(f"{stat}:") + bar + "{:>5}".format(f"{per}%") return line def print_stats(self): full_line = "----------------------------" print(full_line) print(f" o)__ Name:" + "{:>10}".format(f"{self.name}")) print(f" (_ _`\\ Status:" + "{:>8}".format(f"{self.status}")) print(f" z/z\\__) Age:" + "{:>11}".format("555 days")) print(full_line) print(self.stat_format("Energy")) print(self.stat_format("Hunger")) print(self.stat_format("Happiness")) print(self.stat_format("Hygiene")) print(full_line) def save(self): print("Saving and quitting...") fp = Path("~/.vpet.sav").expanduser() with open(fp, "w") as f: json.dump(self.__dict__,f) print("Done") def load(self): print("Loading game...") fp = Path("~/.vpet.sav").expanduser() if not Path.exists(fp): print("No save found.") return 0 with open(fp, "r") as f: data = json.load(f) self.__dict__.update(data) return 1 p = Pet("sally", "frog") if not p.load(): p.rename() os.system("clear") while 1: p.print_stats() x = input() os.system("clear") if x == 'p': p.play() elif x == 'q': p.save() exit() elif x == 'f': p.feed() elif x == 's': p.sleep() elif x == 'c': p.clean() else: print("Unknown Command")