basic threading implemented
This commit is contained in:
parent
267a3e2bed
commit
daa77b26d6
1 changed files with 20 additions and 5 deletions
23
vpet.py
23
vpet.py
|
|
@ -1,5 +1,6 @@
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
@ -13,6 +14,8 @@ class Pet:
|
||||||
self.hygiene = 100
|
self.hygiene = 100
|
||||||
self.is_alive = True
|
self.is_alive = True
|
||||||
self.birthday = time.time()
|
self.birthday = time.time()
|
||||||
|
self.last_decay = time.time()
|
||||||
|
self.decay_delay = 10
|
||||||
self.status = "Happy"
|
self.status = "Happy"
|
||||||
|
|
||||||
def rename(self):
|
def rename(self):
|
||||||
|
|
@ -30,6 +33,7 @@ class Pet:
|
||||||
self.hygiene = min(100, self.hygiene + 13)
|
self.hygiene = min(100, self.hygiene + 13)
|
||||||
|
|
||||||
def decay(self):
|
def decay(self):
|
||||||
|
self.last_decay = time.time()
|
||||||
self.happiness = max(0, self.happiness - 5)
|
self.happiness = max(0, self.happiness - 5)
|
||||||
self.energy = max(0, self.energy - 3)
|
self.energy = max(0, self.energy - 3)
|
||||||
self.hunger = min(100, self.hunger + 2)
|
self.hunger = min(100, self.hunger + 2)
|
||||||
|
|
@ -43,7 +47,6 @@ class Pet:
|
||||||
print(time.time() - self.birthday)
|
print(time.time() - self.birthday)
|
||||||
|
|
||||||
def make_bar(self, percent):
|
def make_bar(self, percent):
|
||||||
|
|
||||||
bar = "[" + "X" * (percent//10) + "-" * (10-percent//10) + "]"
|
bar = "[" + "X" * (percent//10) + "-" * (10-percent//10) + "]"
|
||||||
return bar
|
return bar
|
||||||
|
|
||||||
|
|
@ -66,6 +69,7 @@ class Pet:
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def print_stats(self):
|
def print_stats(self):
|
||||||
|
os.system("clear")
|
||||||
full_line = "----------------------------"
|
full_line = "----------------------------"
|
||||||
print(full_line)
|
print(full_line)
|
||||||
print(f" o)__ Name:" + "{:>10}".format(f"{self.name}"))
|
print(f" o)__ Name:" + "{:>10}".format(f"{self.name}"))
|
||||||
|
|
@ -83,7 +87,6 @@ class Pet:
|
||||||
fp = Path("~/.vpet.sav").expanduser()
|
fp = Path("~/.vpet.sav").expanduser()
|
||||||
with open(fp, "w") as f:
|
with open(fp, "w") as f:
|
||||||
json.dump(self.__dict__,f)
|
json.dump(self.__dict__,f)
|
||||||
print("Done")
|
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
print("Loading game...")
|
print("Loading game...")
|
||||||
|
|
@ -96,18 +99,30 @@ class Pet:
|
||||||
self.__dict__.update(data)
|
self.__dict__.update(data)
|
||||||
return 1
|
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")
|
p = Pet("sally", "frog")
|
||||||
if not p.load():
|
if not p.load():
|
||||||
p.rename()
|
p.rename()
|
||||||
os.system("clear")
|
p.running = True
|
||||||
|
run_t = threading.Thread(target=run, args=[p])
|
||||||
|
run_t.start()
|
||||||
while 1:
|
while 1:
|
||||||
p.print_stats()
|
p.print_stats()
|
||||||
x = input()
|
x = input()
|
||||||
os.system("clear")
|
|
||||||
if x == 'p':
|
if x == 'p':
|
||||||
p.play()
|
p.play()
|
||||||
elif x == 'q':
|
elif x == 'q':
|
||||||
p.save()
|
p.save()
|
||||||
|
p.running = False
|
||||||
exit()
|
exit()
|
||||||
elif x == 'f':
|
elif x == 'f':
|
||||||
p.feed()
|
p.feed()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue