def read_treasure(treas): title = thorpy.make_text(treas.title, font_size=thorpy.style.FONT_SIZE + 3, font_color=TCOLOR) line = thorpy.Line.make(100, "h") text = thorpy.make_text(thorpy.pack_text(100, treas.text)) elements = [title, line, text] class Choice: ok = False def func_cancel(): Choice.ok = True thorpy.functions.quit_menu_func() box = thorpy.make_ok_cancel_box(elements, "No, let it there", "Yes, take it") box.e_ok.user_func = thorpy.functions.quit_menu_func box.e_ok.user_params = {} box.e_cancel.user_func = func_cancel box.e_cancel.user_params = {} box.center() ## box.add_reaction(thorpy.ConstantReaction(pygame.KEYDOWN, ## thorpy.functions.quit_menu_func, ## {"key":pygame.K_SPACE})) m = thorpy.Menu([box]) m.play() return Choice.ok
def __init__(self, game): self.game = game self.game.gui = self self.e_chars = {c: None for c in self.game.chars()} # import scenario def l_func_after(): ## self.game.element.blit() self.game.cam.show(self.game.screen) self.game.blit_things() self.e_pause.blit() pygame.display.flip() def launch_stats(): scenario.launch(self.get_stats(), l_func_after) def save_game(): game.autosave() thorpy.launch_blocking_alert("Game saved") l_func_after() self.e_controls = thorpy.make_button("See instructions", scenario.launch, { "e": game, "func": l_func_after }) # self.e_save = thorpy.make_button("Save game", save_game) self.e_stats = thorpy.make_button("Statistics", launch_stats) self.e_options = thorpy.make_button("Options", launch_options) # self.e_pause = thorpy.make_ok_cancel_box( [self.e_controls, self.e_save, self.e_stats, self.e_options], "Continue game", "Quit game") # self.pause_launcher = thorpy.get_launcher(self.e_pause, launching=game.element) self.e_options.user_params = {"game": game, "epause": self.e_pause} reac_esc = thorpy.ConstantReaction(pygame.KEYDOWN, self.pause_launcher.launch, {"key": pygame.K_ESCAPE}) self.game.element.add_reaction(reac_esc) self.e_pause.set_main_color((200, 200, 255, 100)) def quit_game(): thorpy.launch_blocking_choices( "This will quit the game. Make sure you saved the game.", [("Ok", thorpy.functions.quit_func), ("Cancel", None)]) l_func_after() self.e_pause.e_cancel.user_func = quit_game self.e_pause.e_cancel.user_params = {}
def show_options(self): e_life_size = thorpy.SliderX(100, (6, 20), "Life font size", type_=int, initial_value=self.life_font_size) ## e_life_color = thorpy.ColorSetter(text="Life font color", ## value=self.life_font_color) e_title = thorpy.make_text("Units life") e_box = thorpy.make_ok_cancel_box([e_title, e_life_size]) e_box.center() result = thorpy.launch_blocking(e_box) if result.how_exited == "done": self.life_font_size = e_life_size.get_value() ## self.life_font_color = e_life_color.get_value() self.refresh_graphics_options() self.me.draw() self.menu.blit() pygame.display.flip()
def plant_flag(flag): writing() varset = thorpy.VarSet() varset.add("title", value=str(flag.title), text="Flag name:", limits=(200, None)) varset.add("text", value=str(flag.text), text="Text:", limits=(200, None)) ps = thorpy.ParamSetter.make([varset]) title = thorpy.make_text("Plant flag", font_size=thorpy.style.FONT_SIZE + 3, font_color=TCOLOR) line = thorpy.Line.make(100, "h") elements = [title, line, ps] class Choice: ok = False def func_cancel(): Choice.ok = True thorpy.functions.quit_menu_func() box = thorpy.make_ok_cancel_box(elements, "Ok", "Cancel") box.e_ok.user_func = thorpy.functions.quit_menu_func box.e_ok.user_params = {} box.e_cancel.user_func = func_cancel box.e_cancel.user_params = {} box.center() ## box.add_reaction(thorpy.ConstantReaction(pygame.KEYDOWN, ## thorpy.functions.quit_menu_func, ## {"key":pygame.K_SPACE})) m = thorpy.Menu([box]) m.play() ps.save() playing() if not Choice.ok: flag.title = varset.get_value("title") flag.text = varset.get_value("text") return Choice.ok
def add_journal_entry(game): writing() varset = thorpy.VarSet() varset.add("title", value="Entry " + str(len(game.journal.entries) + 1), text="Title:", limits=(300, None)) varset.add("text", value="", text="Text:", limits=(300, None)) ps = thorpy.ParamSetter.make([varset]) title = thorpy.make_text("Add journal entry", font_size=thorpy.style.FONT_SIZE + 3, font_color=TCOLOR) line = thorpy.Line.make(100, "h") elements = [title, line, ps] class Choice: ok = False def func_cancel(): Choice.ok = True thorpy.functions.quit_menu_func() box = thorpy.make_ok_cancel_box(elements, "Ok", "Cancel") box.e_ok.user_func = thorpy.functions.quit_menu_func box.e_ok.user_params = {} box.e_cancel.user_func = func_cancel box.e_cancel.user_params = {} box.center() m = thorpy.Menu([box]) m.play() ps.save() playing() if not Choice.ok: game.journal.add_entry(varset.get_value("title"), varset.get_value("text")) return Choice.ok
def manage_stocks(stock1, stock2): c = stock1.food + stock2.food #remains constant class Os1: v = stock1.food class Os2: v = stock2.food title = thorpy.make_text("Food stock management", font_size=thorpy.style.FONT_SIZE + 3, font_color=TCOLOR) line = thorpy.Line.make(300, "h") def take_all(): stock1.refood_from(stock2) thorpy.functions.quit_menu_func() ## print(stock1,stock2) e_take = thorpy.make_button("Take all", func=take_all) s1 = thorpy.SliderX.make(length=350, limvals=(0, stock1.max_food), text=stock1.name, type_=int, initial_value=int(stock1.food)) s2 = thorpy.SliderX.make(length=350, limvals=(0, stock2.max_food), text=stock2.name, type_=int, initial_value=int(stock2.food)) e1 = thorpy.Box.make(elements=[s1]) e2 = thorpy.Box.make(elements=[s2]) arrow = thorpy.Image.make("doublearrow32.bmp", colorkey=(255, 255, 255)) # box = thorpy.make_ok_cancel_box([title, line, e_take, e1, arrow, e2]) box.set_main_color((200, 200, 255, 150)) box.center() # def refresh_sliders(e): ns1 = s1.get_value() ns2 = s2.get_value() if ns1 + ns2 > c: s1.unblit_and_reblit_func(s1.set_value, value=Os1.v) s2.unblit_and_reblit_func(s2.set_value, value=Os2.v) return if e.el is s1: new_value = c - ns1 if new_value > s2.limvals[1]: new_value = s2.limvals[1] elif new_value < s2.limvals[0]: new_value = s2.limvals[0] s2.unblit_and_reblit_func(s2.set_value, value=new_value) elif e.el is s2: new_value = c - ns2 if new_value > s1.limvals[1]: new_value = s1.limvals[1] elif new_value < s1.limvals[0]: new_value = s1.limvals[0] s1.unblit_and_reblit_func(s1.set_value, value=new_value) Os1.v, Os2.v = s1.get_value(), s2.get_value() reaction = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=refresh_sliders, event_args={"id": thorpy.constants.EVENT_SLIDE}) box.add_reaction(reaction) def func_ok(): stock1.food = s1.get_value() stock2.food = s2.get_value() thorpy.functions.quit_menu_func() box.e_cancel.user_func = thorpy.functions.quit_menu_func box.e_cancel.user_params = {} box.e_ok.user_func = func_ok box.e_ok.user_params = {} menu = thorpy.Menu(box) menu.play()
application = thorpy.Application((500,500), "Advanced menus") # ****************** First launcher : button 1 ****************** #This launcher launches a simple button my_element = thorpy.make_button("I am a useless button\nClick outside to quit.") button1 = thorpy.make_button("Launcher 1") #we set click_quit=True below, because we did not provide a "ok" and/or "cancel" #button to the user. Element disappears When user clicks outside it. thorpy.set_launcher(button1, my_element, click_quit=True) # ****************** Second launcher : button 2 ****************** #here the element to be launched is a box with ok and cancel buttons + custom #elements. We can also use make_ok_box, with only 1 text. #Note that DONE_EVENT and CANCEL_EVENT are posted accordingly at unlaunch. box = thorpy.make_ok_cancel_box([thorpy.make_button(str(i)) for i in range(8)], ok_text="Ok", cancel_text="Cancel") button2 = thorpy.make_button("Launcher 2") thorpy.set_launcher(button2, box) # ****************** Third launcher : button 3 ****************** #This launcher launches a box, set it green, and changes screen color when #unlaunched. button3 = thorpy.make_button("Launcher 3") other_box = thorpy.make_ok_box([thorpy.make_text("Color is gonna change...")]) my_launcher = thorpy.set_launcher(button3, other_box)#this time get the launcher #we specify some custom operations that have to be done before/after launching: def my_func_before(): my_launcher.launched.set_main_color((0,255,0)) #change launched box color my_launcher.default_func_before() #default stuff def my_func_after(): background.set_main_color((0,100,100)) #change background color