basic threading implemented

This commit is contained in:
phixxy 2026-03-17 14:39:13 -07:00
parent 267a3e2bed
commit daa77b26d6

25
vpet.py
View file

@ -1,5 +1,6 @@
import time
import os
import threading
import json
from pathlib import Path
@ -13,6 +14,8 @@ class Pet:
self.hygiene = 100
self.is_alive = True
self.birthday = time.time()
self.last_decay = time.time()
self.decay_delay = 10
self.status = "Happy"
def rename(self):
@ -30,6 +33,7 @@ class Pet:
self.hygiene = min(100, self.hygiene + 13)
def decay(self):
self.last_decay = time.time()
self.happiness = max(0, self.happiness - 5)
self.energy = max(0, self.energy - 3)
self.hunger = min(100, self.hunger + 2)
@ -43,7 +47,6 @@ class Pet:
print(time.time() - self.birthday)
def make_bar(self, percent):
bar = "[" + "X" * (percent//10) + "-" * (10-percent//10) + "]"
return bar
@ -66,6 +69,7 @@ class Pet:
return line
def print_stats(self):
os.system("clear")
full_line = "----------------------------"
print(full_line)
print(f" o)__ Name:" + "{:>10}".format(f"{self.name}"))
@ -83,7 +87,6 @@ class Pet:
fp = Path("~/.vpet.sav").expanduser()
with open(fp, "w") as f:
json.dump(self.__dict__,f)
print("Done")
def load(self):
print("Loading game...")
@ -95,19 +98,31 @@ class Pet:
data = json.load(f)
self.__dict__.update(data)
return 1
def run(p):
time.sleep(p.decay_delay)
p.running = True
while p.running:
if time.time() - p.last_decay >= p.decay_delay:
p.decay()
p.print_stats()
time.sleep(1)
print("Done")
p = Pet("sally", "frog")
if not p.load():
p.rename()
os.system("clear")
p.running = True
run_t = threading.Thread(target=run, args=[p])
run_t.start()
while 1:
p.print_stats()
x = input()
os.system("clear")
if x == 'p':
p.play()
elif x == 'q':
p.save()
p.running = False
exit()
elif x == 'f':
p.feed()