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.start_time = int(time.time())
self.last_time = int(time.time()) self.last_time = int(time.time())
# must water plant first day # 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_timestamp = int(time.time()) # debug
self.watered_24h = False 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): def rarity_check(self):
# Generate plant rarity # Generate plant rarity
CONST_RARITY_MAX = 256.0 CONST_RARITY_MAX = 256.0
@ -225,11 +234,13 @@ class Plant(object):
def dead_check(self): def dead_check(self):
time_delta_watered = int(time.time()) - self.watered_timestamp time_delta_watered = int(time.time()) - self.watered_timestamp
# if it has been >5 days since watering, sorry plant is dead :( # 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)): if time_delta_watered > (5 * (24 * 3600)):
self.dead = True self.dead = True
return self.dead return self.dead
def kill_plant(self):
self.dead = True
def water_check(self): def water_check(self):
# if plant has been watered in 24h then it keeps growing # if plant has been watered in 24h then it keeps growing
# time_delta_watered is difference from now to last watered # time_delta_watered is difference from now to last watered
@ -285,8 +296,10 @@ class Plant(object):
# day = 3600*24 # day = 3600*24
# life_stages = (1*day, 2*day, 3*day, 4*day, 5*day) # life_stages = (1*day, 2*day, 3*day, 4*day, 5*day)
# leave this untouched bc it works for now # leave this untouched bc it works for now
while (not self.dead): # while (not self.dead):
while True:
time.sleep(1) time.sleep(1)
if not self.dead:
if self.watered_24h: if self.watered_24h:
self.ticks += 1 self.ticks += 1
if self.stage < len(self.stage_dict)-1: if self.stage < len(self.stage_dict)-1:
@ -295,10 +308,10 @@ class Plant(object):
#print self.parse_plant() #print self.parse_plant()
if self.mutate_check(): if self.mutate_check():
1==1 1==1
if self.dead_check():
1==1
if self.water_check(): if self.water_check():
1==1 1==1
if self.dead_check():
1==1
# TODO: event check # TODO: event check
class DataManager(object): class DataManager(object):
@ -406,7 +419,11 @@ if __name__ == '__main__':
my_plant.start_life() my_plant.start_life()
my_data.enable_autosave(my_plant) my_data.enable_autosave(my_plant)
botany_menu = CursedMenu(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.save_plant(my_plant)
my_data.data_write_json(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"): def show(self, options, title="Title", subtitle="Subtitle"):
'''Draws a menu with the given parameters''' '''Draws a menu with the given parameters'''
self.set_options(options) self.set_options(options)
self.update_options()
self.title = title self.title = title
self.subtitle = subtitle self.subtitle = subtitle
self.selected = 0 self.selected = 0
self.initialized = True self.initialized = True
self.draw_menu() 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): def set_options(self, options):
'''Validates that the last option is "Exit"''' '''Validates that the last option is "exit"'''
if options[-1] is not 'Exit': if options[-1] is not 'exit':
options.append('Exit') options.append('exit')
self.options = options self.options = options
def draw_menu(self): def draw_menu(self):
'''Actually draws the menu and handles branching''' '''Actually draws the menu and handles branching'''
request = "" request = ""
try: try:
while request is not "Exit": while request is not "exit":
self.draw() self.draw()
request = self.get_user_input() request = self.get_user_input()
self.handle_request(request) self.handle_request(request)
@ -61,6 +76,7 @@ class CursedMenu(object):
def draw(self): def draw(self):
'''Draw the menu and lines''' '''Draw the menu and lines'''
clear_bar = " " * (int(self.maxx/2))
self.screen.refresh() self.screen.refresh()
self.screen.border(0) self.screen.border(0)
self.screen.addstr(2,2, self.title, curses.A_STANDOUT) # Title for this menu self.screen.addstr(2,2, self.title, curses.A_STANDOUT) # Title for this menu
@ -70,18 +86,24 @@ class CursedMenu(object):
textstyle = self.normal textstyle = self.normal
if index == self.selected: if index == self.selected:
textstyle = self.highlighted 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(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(11,2, self.plant_string, curses.A_NORMAL)
self.screen.addstr(12,2, self.plant_ticks, curses.A_NORMAL) self.screen.addstr(12,2, self.plant_ticks, curses.A_NORMAL)
if not self.plant.dead: if not self.plant.dead:
if int(time.time()) <= self.plant.watered_timestamp + 24*3600: 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: else:
self.screen.addstr(6,13, " ", curses.A_NORMAL) self.screen.addstr(5,13, clear_bar, curses.A_NORMAL)
else: 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: try:
self.screen.refresh() self.screen.refresh()
except Exception as exception: except Exception as exception:
@ -92,10 +114,12 @@ class CursedMenu(object):
def update_plant_live(self): def update_plant_live(self):
# Updates plant data on menu screen, live! # Updates plant data on menu screen, live!
# Will eventually use this to display ascii art... # Will eventually use this to display ascii art...
# self.set_options(self.options)
while not self.exit: while not self.exit:
self.plant_string = self.plant.parse_plant() self.plant_string = self.plant.parse_plant()
self.plant_ticks = str(self.plant.ticks) self.plant_ticks = str(self.plant.ticks)
if self.initialized: if self.initialized:
self.update_options()
self.draw() self.draw()
time.sleep(1) time.sleep(1)
@ -103,7 +127,7 @@ class CursedMenu(object):
'''Gets the user's input and acts appropriately''' '''Gets the user's input and acts appropriately'''
user_in = self.screen.getch() # Gets user input 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: if user_in == 10:
return self.options[self.selected] return self.options[self.selected]
if user_in == 27: if user_in == 27:
@ -126,6 +150,10 @@ class CursedMenu(object):
def handle_request(self, request): def handle_request(self, request):
'''This is where you do things with the request''' '''This is where you do things with the request'''
if request is None: return 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": if request == "water":
self.plant.water() self.plant.water()
if request == "instructions": if request == "instructions":
@ -152,11 +180,10 @@ available in the readme :)
self.instructiontoggle = not self.instructiontoggle self.instructiontoggle = not self.instructiontoggle
# self.screen.clear() # self.screen.clear()
for y, line in enumerate(instructions_txt.splitlines(), 2): 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.addstr(8,15,str(self.plant.ticks), curses.A_STANDOUT) # Title for this menu
self.screen.refresh() self.screen.refresh()
def __exit__(self): def __exit__(self):
self.exit = True self.exit = True
curses.curs_set(2) curses.curs_set(2)