Menu working + debug mode

pull/1/head
Jake Funke 2017-03-09 19:32:40 +00:00
parent 6bec78d2b3
commit 4a6bf14a1e
2 changed files with 69 additions and 25 deletions

View File

@ -170,10 +170,19 @@ class Plant(object):
self.start_time = int(time.time())
self.last_time = int(time.time())
# must water plant first day
self.watered_timestamp = int(time.time())-(24*3601)
self.watered_timestamp = int(time.time())-(24*3600)-1
# self.watered_timestamp = int(time.time()) # debug
self.watered_24h = False
def new_seed(self,this_filename):
# TODO: this is broken :(
# TODO: selecting new seed *kind of* clears the screen properly
# but watering it doesn't let it start until you quit and
# restart
# fuck that
os.remove(this_filename)
self.__init__(this_filename)
def rarity_check(self):
# Generate plant rarity
CONST_RARITY_MAX = 256.0
@ -225,11 +234,13 @@ class Plant(object):
def dead_check(self):
time_delta_watered = int(time.time()) - self.watered_timestamp
# if it has been >5 days since watering, sorry plant is dead :(
# if time_delta_watered > 5: #debug
if time_delta_watered > (5 * (24 * 3600)):
self.dead = True
return self.dead
def kill_plant(self):
self.dead = True
def water_check(self):
# if plant has been watered in 24h then it keeps growing
# time_delta_watered is difference from now to last watered
@ -285,20 +296,22 @@ class Plant(object):
# day = 3600*24
# life_stages = (1*day, 2*day, 3*day, 4*day, 5*day)
# leave this untouched bc it works for now
while (not self.dead):
# while (not self.dead):
while True:
time.sleep(1)
if self.watered_24h:
self.ticks += 1
if self.stage < len(self.stage_dict)-1:
if self.ticks >= life_stages[self.stage]:
self.growth()
#print self.parse_plant()
if self.mutate_check():
1==1
if self.dead_check():
1==1
if not self.dead:
if self.watered_24h:
self.ticks += 1
if self.stage < len(self.stage_dict)-1:
if self.ticks >= life_stages[self.stage]:
self.growth()
#print self.parse_plant()
if self.mutate_check():
1==1
if self.water_check():
1==1
if self.dead_check():
1==1
# TODO: event check
class DataManager(object):
@ -381,7 +394,7 @@ class DataManager(object):
json_file = os.path.join(self.botany_dir,self.this_user + '_plant_data.json')
# also updates age
d,h,m,s = self.convert_seconds(this_plant.ticks)
age_formatted = ("%dd:%dh:%dm:%ds" % (d, h, m, s))
age_formatted = ("%dd:%dh:%dm:%ds" % (d, h, m, s))
plant_info = {
"owner":this_plant.owner,
"description":this_plant.parse_plant(),
@ -406,7 +419,11 @@ if __name__ == '__main__':
my_plant.start_life()
my_data.enable_autosave(my_plant)
botany_menu = CursedMenu(my_plant)
botany_menu.show([1,"water","look","instructions"], title=' botany ', subtitle='Options')
botany_menu.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
# if not my_plant.dead:
# botany_menu.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
# else:
# botany_menu.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
my_data.save_plant(my_plant)
my_data.data_write_json(my_plant)

View File

@ -32,23 +32,38 @@ class CursedMenu(object):
def show(self, options, title="Title", subtitle="Subtitle"):
'''Draws a menu with the given parameters'''
self.set_options(options)
self.update_options()
self.title = title
self.subtitle = subtitle
self.selected = 0
self.initialized = True
self.draw_menu()
def update_options(self):
if self.plant.dead:
if "kill" in self.options:
self.options.remove("kill")
if "new" not in self.options:
self.options.insert(-1,"new")
else:
if "new" in self.options:
self.options.remove("new")
if "kill" not in self.options:
self.options.insert(-1,"kill")
#self.draw_menu()
#self.screen.clear()
def set_options(self, options):
'''Validates that the last option is "Exit"'''
if options[-1] is not 'Exit':
options.append('Exit')
'''Validates that the last option is "exit"'''
if options[-1] is not 'exit':
options.append('exit')
self.options = options
def draw_menu(self):
'''Actually draws the menu and handles branching'''
request = ""
try:
while request is not "Exit":
while request is not "exit":
self.draw()
request = self.get_user_input()
self.handle_request(request)
@ -61,6 +76,7 @@ class CursedMenu(object):
def draw(self):
'''Draw the menu and lines'''
clear_bar = " " * (int(self.maxx/2))
self.screen.refresh()
self.screen.border(0)
self.screen.addstr(2,2, self.title, curses.A_STANDOUT) # Title for this menu
@ -70,18 +86,24 @@ class CursedMenu(object):
textstyle = self.normal
if index == self.selected:
textstyle = self.highlighted
# TODO: this is hacky
self.screen.addstr(5+index,4, clear_bar, curses.A_NORMAL)
self.screen.addstr(5+index,4, "%d - %s" % (index+1, self.options[index]), textstyle)
self.screen.addstr(11,2, clear_bar, curses.A_NORMAL)
self.screen.addstr(12,2, clear_bar, curses.A_NORMAL)
self.screen.addstr(11,2, self.plant_string, curses.A_NORMAL)
self.screen.addstr(12,2, self.plant_ticks, curses.A_NORMAL)
if not self.plant.dead:
if int(time.time()) <= self.plant.watered_timestamp + 24*3600:
self.screen.addstr(6,13, " - plant watered today :)", curses.A_NORMAL)
self.screen.addstr(5,13, clear_bar, curses.A_NORMAL)
self.screen.addstr(5,13, " - plant watered today :)", curses.A_NORMAL)
else:
self.screen.addstr(6,13, " ", curses.A_NORMAL)
self.screen.addstr(5,13, clear_bar, curses.A_NORMAL)
else:
self.screen.addstr(6,13, " - you can't water a dead plant :(", curses.A_NORMAL)
self.screen.addstr(5,13, clear_bar, curses.A_NORMAL)
self.screen.addstr(5,13, " - you can't water a dead plant :(", curses.A_NORMAL)
try:
self.screen.refresh()
except Exception as exception:
@ -92,10 +114,12 @@ class CursedMenu(object):
def update_plant_live(self):
# Updates plant data on menu screen, live!
# Will eventually use this to display ascii art...
# self.set_options(self.options)
while not self.exit:
self.plant_string = self.plant.parse_plant()
self.plant_ticks = str(self.plant.ticks)
if self.initialized:
self.update_options()
self.draw()
time.sleep(1)
@ -103,7 +127,7 @@ class CursedMenu(object):
'''Gets the user's input and acts appropriately'''
user_in = self.screen.getch() # Gets user input
'''Enter and Exit Keys are special cases'''
'''Enter and exit Keys are special cases'''
if user_in == 10:
return self.options[self.selected]
if user_in == 27:
@ -126,6 +150,10 @@ class CursedMenu(object):
def handle_request(self, request):
'''This is where you do things with the request'''
if request is None: return
if request is "kill":
self.plant.kill_plant()
if request is "new":
self.plant.new_seed(self.plant.file_name)
if request == "water":
self.plant.water()
if request == "instructions":
@ -152,11 +180,10 @@ available in the readme :)
self.instructiontoggle = not self.instructiontoggle
# self.screen.clear()
for y, line in enumerate(instructions_txt.splitlines(), 2):
self.screen.addstr(y,self.maxx-50, line)
self.screen.addstr(self.maxy-12+y,self.maxx-47, line)
#self.screen.addstr(8,15,str(self.plant.ticks), curses.A_STANDOUT) # Title for this menu
self.screen.refresh()
def __exit__(self):
self.exit = True
curses.curs_set(2)