Cleanup + output formatting

pull/1/head
Jake Funke 2017-03-15 01:31:18 +00:00
parent 7faf3a4dd0
commit 17248eed50
2 changed files with 73 additions and 57 deletions

View File

@ -221,7 +221,7 @@ class Plant(object):
# Create plant mutation # Create plant mutation
# TODO: when out of debug this needs to be set to high number (1000 # TODO: when out of debug this needs to be set to high number (1000
# even maybe) # even maybe)
CONST_MUTATION_RARITY = 2000 # Increase this # to make mutation rarer (chance 1 out of x) CONST_MUTATION_RARITY = 1000 # Increase this # to make mutation rarer (chance 1 out of x)
mutation_seed = random.randint(1,CONST_MUTATION_RARITY) mutation_seed = random.randint(1,CONST_MUTATION_RARITY)
if mutation_seed == CONST_MUTATION_RARITY: if mutation_seed == CONST_MUTATION_RARITY:
# mutation gained! # mutation gained!
@ -369,7 +369,6 @@ class DataManager(object):
self.garden_update(this_plant) self.garden_update(this_plant)
# TODO: change after debug # TODO: change after debug
#time.sleep(60) #time.sleep(60)
# TODO: if plant dies it should force save.
time.sleep(5) time.sleep(5)
def load_plant(self): def load_plant(self):
@ -481,7 +480,6 @@ if __name__ == '__main__':
my_plant.start_life() my_plant.start_life()
my_data.start_threads(my_plant) my_data.start_threads(my_plant)
botany_menu = CursedMenu(my_plant,my_data.garden_file_path) botany_menu = CursedMenu(my_plant,my_data.garden_file_path)
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)
my_data.garden_update(my_plant) my_data.garden_update(my_plant)

View File

@ -20,7 +20,7 @@ class CursedMenu(object):
self.exit = False self.exit = False
self.instructiontoggle = False self.instructiontoggle = False
self.gardenmenutoggle = False self.gardenmenutoggle = False
self.looktoggle = False self.infotoggle = 0
self.maxy, self.maxx = self.screen.getmaxyx() self.maxy, self.maxx = self.screen.getmaxyx()
# Highlighted and Normal line definitions # Highlighted and Normal line definitions
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
@ -31,6 +31,7 @@ class CursedMenu(object):
screen_thread.daemon = True screen_thread.daemon = True
screen_thread.start() screen_thread.start()
self.screen.clear() self.screen.clear()
self.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
def show(self, options, title, subtitle): def show(self, options, title, subtitle):
# Draws a menu with parameters # Draws a menu with parameters
@ -45,8 +46,6 @@ class CursedMenu(object):
def update_options(self): def update_options(self):
# Makes sure you can get a new plant if it dies # Makes sure you can get a new plant if it dies
if self.plant.dead: if self.plant.dead:
# if "start over" in self.options:
# self.options.remove("start over")
if "start over" not in self.options: if "start over" not in self.options:
self.options.insert(-1,"start over") self.options.insert(-1,"start over")
else: else:
@ -67,7 +66,7 @@ class CursedMenu(object):
def draw(self): def draw(self):
# Draw the menu and lines # Draw the menu and lines
# TODO: this needs to either display the default menu screen or the # TODO: this needs to either display the default menu screen or the
# garden/leaderboard thing based on self.gardenmenutoggle # garden/leaderboard thing
# TODO: display refresh is hacky. Could be more precise # TODO: display refresh is hacky. Could be more precise
self.screen.refresh() self.screen.refresh()
self.screen.border(0) self.screen.border(0)
@ -76,7 +75,9 @@ class CursedMenu(object):
self.screen.refresh() self.screen.refresh()
except Exception as exception: except Exception as exception:
# Makes sure data is saved in event of a crash due to window resizing # Makes sure data is saved in event of a crash due to window resizing
self.screen.clear()
self.screen.addstr(0,0,"Enlarge terminal!") self.screen.addstr(0,0,"Enlarge terminal!")
self.screen.refresh()
self.__exit__() self.__exit__()
traceback.print_exc() traceback.print_exc()
@ -92,18 +93,21 @@ class CursedMenu(object):
# Also calls __exit__, but adds traceback after # Also calls __exit__, but adds traceback after
except Exception as exception: except Exception as exception:
self.screen.clear()
self.screen.addstr(0,0,"Enlarge terminal!")
self.screen.refresh()
self.__exit__() self.__exit__()
traceback.print_exc() #traceback.print_exc()
def draw_default(self): def draw_default(self):
# Draws default menu # draws default menu
clear_bar = " " * (int(self.maxx*2/3)) clear_bar = " " * (int(self.maxx*2/3))
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
self.screen.addstr(4,2, self.subtitle, curses.A_BOLD) #Subtitle for this menu self.screen.addstr(4,2, self.subtitle, curses.A_BOLD) #Subtitle for this menu
# Clear menu on screen # clear menu on screen
for index in range(len(self.options)+1): for index in range(len(self.options)+1):
self.screen.addstr(5+index,4, clear_bar, curses.A_NORMAL) self.screen.addstr(5+index,4, clear_bar, curses.A_NORMAL)
# Display all the menu items, showing the 'pos' item highlighted # display all the menu items, showing the 'pos' item highlighted
for index in range(len(self.options)): for index in range(len(self.options)):
textstyle = self.normal textstyle = self.normal
if index == self.selected: if index == self.selected:
@ -113,8 +117,10 @@ class CursedMenu(object):
self.screen.addstr(11,2, clear_bar, curses.A_NORMAL) self.screen.addstr(11,2, clear_bar, curses.A_NORMAL)
self.screen.addstr(12,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, "plant: ", curses.A_DIM)
self.screen.addstr(12,2, self.plant_ticks, curses.A_NORMAL) self.screen.addstr(11,9, self.plant_string, curses.A_NORMAL)
self.screen.addstr(12,2, "score: ", curses.A_DIM)
self.screen.addstr(12,9, 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:
@ -127,8 +133,8 @@ class CursedMenu(object):
self.screen.addstr(5,13, " - you can't water a dead plant :(", curses.A_NORMAL) self.screen.addstr(5,13, " - you can't water a dead plant :(", curses.A_NORMAL)
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...
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)
@ -138,21 +144,27 @@ class CursedMenu(object):
time.sleep(1) time.sleep(1)
def get_user_input(self): def get_user_input(self):
# Gets the user's input and acts appropriately # gets the user's input and acts appropriately
try:
user_in = self.screen.getch() # Gets user input user_in = self.screen.getch() # Gets user input
except Exception as e:
# Enter and exit Keys are special cases self.__exit__()
# 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:
return self.options[-1] return self.options[-1]
if user_in == curses.KEY_RESIZE:
self.maxy,self.maxx = self.screen.getmaxyx()
self.screen.clear()
self.screen.refresh()
# This is a number; check to see if we can set it # this is a number; check to see if we can set it
if user_in >= ord('1') and user_in <= ord(str(min(9,len(self.options)+1))): if user_in >= ord('1') and user_in <= ord(str(min(9,len(self.options)+1))):
self.selected = user_in - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index self.selected = user_in - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
return return
# Increment or Decrement # increment or Decrement
if user_in == curses.KEY_DOWN: # down arrow if user_in == curses.KEY_DOWN: # down arrow
self.selected += 1 self.selected += 1
if user_in == curses.KEY_UP: # up arrow if user_in == curses.KEY_UP: # up arrow
@ -174,25 +186,25 @@ class CursedMenu(object):
return plant_table return plant_table
def draw_garden(self): def draw_garden(self):
# Draws neighborhood # draws neighborhood
clear_bar = " " * (self.maxx-2) + "\n" clear_bar = " " * (self.maxx-2) + "\n"
control_keys = [curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_RIGHT] control_keys = [curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_RIGHT]
# load data # load data
with open(self.garden_file_path, 'rb') as f: with open(self.garden_file_path, 'rb') as f:
this_garden = pickle.load(f) this_garden = pickle.load(f)
# format data # format data
if not self.gardenmenutoggle: if self.infotoggle != 2:
plant_table_formatted = self.format_garden_data(this_garden) plant_table_formatted = self.format_garden_data(this_garden)
self.gardenmenutoggle = not self.gardenmenutoggle self.infotoggle = 2
else: else:
plant_table_formatted = clear_bar plant_table_formatted = clear_bar
for plant in this_garden: for plant in this_garden:
if not this_garden[plant]["dead"]: if not this_garden[plant]["dead"]:
plant_table_formatted += clear_bar plant_table_formatted += clear_bar
self.gardenmenutoggle = not self.gardenmenutoggle self.infotoggle = 0
for y, line in enumerate(plant_table_formatted.splitlines(), 2): for y, line in enumerate(plant_table_formatted.splitlines(), 2):
self.screen.addstr(y+17, 2, line) self.screen.addstr(y+12, 2, line)
self.screen.refresh() self.screen.refresh()
def get_plant_description(self, this_plant): def get_plant_description(self, this_plant):
@ -209,14 +221,18 @@ class CursedMenu(object):
"You're ready for a new start with this plant.", "You're ready for a new start with this plant.",
"You're tired of waiting for your seed to grow.", "You're tired of waiting for your seed to grow.",
"You wish your seed could tell you what it needs.", "You wish your seed could tell you what it needs.",
"You can feel the spirit inside your seed.",
], ],
1:[ 1:[
"The seedling fills you with hope.", "The seedling fills you with hope.",
"The seedling shakes in the wind.",
"You can make out a tiny leaf - or is that a thorn?", "You can make out a tiny leaf - or is that a thorn?",
"You can feel the seedling looking back at you.", "You can feel the seedling looking back at you.",
"You kiss your seedling good night.", "You kiss your seedling good night.",
"You think about all the seedlings who came before it.", "You think about all the seedlings who came before it.",
"You and your seedling make a great team.", "You and your seedling make a great team.",
"Your seedling grows slowly and quietly.",
"You briefly meditate on the paths your life could take.",
], ],
2:[ 2:[
"The " + this_species + " makes you feel relaxed.", "The " + this_species + " makes you feel relaxed.",
@ -224,27 +240,35 @@ class CursedMenu(object):
"You quietly sit with your " + this_species + " for a few minutes.", "You quietly sit with your " + this_species + " for a few minutes.",
"Your " + this_species + " looks pretty good.", "Your " + this_species + " looks pretty good.",
"You play loud techno to your " + this_species + ".", "You play loud techno to your " + this_species + ".",
"You play piano to your " + this_species + ".",
"You play rap music to your " + this_species + ".",
"You whistle a tune to your " + this_species + ".",
], ],
3:[ 3:[
"Your " + this_species + " is growing nicely!", "Your " + this_species + " is growing nicely!",
"You're proud of the dedication it took to grow your " + this_species + ".", "You're proud of the dedication it took to grow your " + this_species + ".",
"The " + this_species + " looks good.", "The " + this_species + " looks good.",
"You think how good this " + this_species + " would look on steroids.", "You think about how good this " + this_species + " will look.",
"The buds of your " + this_species + " are about to bloom.", "The buds of your " + this_species + " are about to bloom.",
"You play your favorite song for your " + this_species + ".",
], ],
4:[ 4:[
"The " + this_color + " flowers look nice on your " + this_species +"!", "The " + this_color + " flowers look nice on your " + this_species +"!",
"The " + this_color + " flowers have bloomed and fill you with desire.", "The " + this_color + " flowers have bloomed and fill you with positivity.",
"The " + this_color + " flowers of your " + this_species + " remind you of your childhood.", "The " + this_color + " flowers of your " + this_species + " remind you of your childhood.",
"The " + this_color + " flowers of your " + this_species + " smell amazing.",
"The " + this_species + " has grown beautiful " + this_color + " flowers.", "The " + this_species + " has grown beautiful " + this_color + " flowers.",
"The " + this_color + " petals remind you of your favorite shirt.", "The " + this_color + " petals remind you of that favorite shirt you lost.",
"The " + this_color + " flowers remind you of your crush.",
], ],
5:[ 5:[
"You fondly remember all of the time you spent caring for your " + this_species + ".", "You fondly remember all of the time you spent caring for your " + this_species + ".",
"Your " + this_species + " looks old and wise.",
"Seed pods have grown on your " + this_species + ".", "Seed pods have grown on your " + this_species + ".",
"The " + this_species + " fills you with love.", "The " + this_species + " fills you with love.",
"The " + this_species + " reminds you of your first crush.", "Your " + this_species + " reminds you of your childhood backyard.",
"The " + this_species + " reminds you of your family.",
"The " + this_species + " reminds you of a forgotten memory.",
"You grow nostalgic about the early days with your " + this_species + ".",
], ],
99:[ 99:[
"You wish you had taken better care of your plant.", "You wish you had taken better care of your plant.",
@ -256,7 +280,6 @@ class CursedMenu(object):
} }
# self.life_stages is tuple containing length of each stage # self.life_stages is tuple containing length of each stage
# (seed, seedling, young, mature, flowering) # (seed, seedling, young, mature, flowering)
# if stage == 0 == seed
if this_plant.dead: if this_plant.dead:
this_stage = 99 this_stage = 99
@ -275,11 +298,6 @@ class CursedMenu(object):
output_text += this_stage_descriptions[description_num] + "\n" output_text += this_stage_descriptions[description_num] + "\n"
# issue 1 - referencing anything past 4 on life stages breaks
# issue 2 - 80% using plant ticks doesn't really work since it shifts
# each time. need to use the difference between 2 stages, and then
# plant ticks minus last stage
if this_stage == 1: if this_stage == 1:
species_options = [this_plant.species_dict[this_plant.species], species_options = [this_plant.species_dict[this_plant.species],
this_plant.species_dict[(this_plant.species+3) % len(this_plant.species_dict)], this_plant.species_dict[(this_plant.species+3) % len(this_plant.species_dict)],
@ -306,16 +324,20 @@ class CursedMenu(object):
def draw_plant_description(self, this_plant): def draw_plant_description(self, this_plant):
clear_bar = " " * (self.maxx-2) + "\n" clear_bar = " " * (self.maxx-2) + "\n"
control_keys = [curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_RIGHT]
# load data # load data
# format data # format data
if not self.looktoggle: if self.infotoggle != 1:
# TODO: clear the bar first
# TODO: when garden grows this won't clear everything.
output_string = clear_bar * 8
for y, line in enumerate(output_string.splitlines(), 2):
self.screen.addstr(y+12, 2, line)
self.screen.refresh()
output_string = self.get_plant_description(this_plant) output_string = self.get_plant_description(this_plant)
self.looktoggle = not self.looktoggle self.infotoggle = 1
else: else:
output_string = clear_bar output_string = clear_bar * 4
output_string += clear_bar*3 self.infotoggle = 0
self.looktoggle = not self.looktoggle
for y, line in enumerate(output_string.splitlines(), 2): for y, line in enumerate(output_string.splitlines(), 2):
self.screen.addstr(y+12, 2, line) self.screen.addstr(y+12, 2, line)
@ -347,34 +369,30 @@ available in the readme :)
self.screen.refresh() self.screen.refresh()
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 == None: return if request == None: return
if request == "start over": if request == "start over":
self.plant.start_over() self.plant.start_over()
if request == "water": if request == "water":
self.plant.water() self.plant.water()
if request == "look": if request == "look":
# try: try:
self.draw_plant_description(self.plant) self.draw_plant_description(self.plant)
# except Exception as exception: except Exception as exception:
# self.screen.addstr(0,0,"Enlarge terminal!") self.screen.refresh()
# self.__exit__()
# traceback.print_exc() # traceback.print_exc()
if request == "instructions": if request == "instructions":
try: try:
self.draw_instructions() self.draw_instructions()
except Exception as exception: except Exception as exception:
# Makes sure data is saved in event of a crash due to window resizing self.screen.refresh()
self.screen.addstr(0,0,"Enlarge terminal!") # traceback.print_exc()
self.__exit__()
traceback.print_exc()
if request == "garden": if request == "garden":
try: try:
self.draw_garden() self.draw_garden()
except Exception as exception: except Exception as exception:
self.screen.addstr(0,0,"Enlarge terminal!") self.screen.refresh()
self.__exit__() # traceback.print_exc()
traceback.print_exc()
def __exit__(self): def __exit__(self):
self.exit = True self.exit = True