def __init__(self, parent, screen, background, font): super().__init__(parent, screen, background, font) self.rows = 15 self.columns = 15 self.mine_count = (self.rows * self.columns) / 10 self.gameboard = Minefield(self, screen, background, font, self.rows, self.columns, self.mine_count) self.title = thorpy.make_text('Minesweeper', font_size=20, font_color=(0, 0, 150)) self.start_button = thorpy.make_button( 'New Game', func=MainMenu.activate_gameboard, params={'self': self}) self.options_button = thorpy.make_button( 'Options', func=MainMenu.activate_options, params={'self': self}) self.quit_button = thorpy.make_button('Quit', func=MainMenu.quit, params={'self': self}) self.box = thorpy.Box(elements=[ self.title, self.start_button, self.options_button, self.quit_button ], size=(screen.get_width(), screen.get_height())) thorpy.store(self.box) self.menu = thorpy.Menu(self.box) for element in self.menu.get_population(): element.surface = self.screen
def __init__(self, player_name, min_val, max_val, trials): #init some parameters of the game ... self.player_name = player_name self.min_val = min_val #the minimum value to guess self.max_val = max_val #the maximum value to guess self.init_trials = trials #keep the original trials amount in memory self.trials = trials #remaining number of trials #the number to guess: self.number = random.randint(self.min_val, self.max_val) self.guess = None #the current player guess self.e_quit = thorpy.make_button("Quit", func=thorpy.functions.quit_menu_func) self.e_restart = thorpy.make_button("Restart", func=self.restart) #a ghost for storing quit and restart: self.e_group_menu = thorpy.make_group([self.e_quit, self.e_restart]) #a counter displaying the trials and some hint/infos about the game self.e_counter = thorpy.make_text(text=self.get_trials_text(), font_color=(0, 0, 255)) #the inserter element in which player can insert his guess self.e_insert = thorpy.Inserter(name="Try:") self.e_background = thorpy.Background( color=(200, 200, 255), elements=[self.e_counter, self.e_insert, self.e_group_menu]) thorpy.store(self.e_background, gap=20) #reaction called each time the player has inserted something reaction_insert = thorpy.ConstantReaction( reacts_to=thorpy.constants.THORPY_EVENT, reac_func=self.reac_insert_func, event_args={ "id": thorpy.constants.EVENT_INSERT, "el": self.e_insert }) self.e_background.add_reaction(reaction_insert)
def get_object(img, oktext, funcok, name, skills, star): eimage = thorpy.Image.make(img) eok = thorpy.make_button(oktext, func=funcok) ename = thorpy.make_text(name) ## sk1 = thorpy.make_text(s1) # star_full = thorpy.change_color_on_img(star, (255, 0, 255), (255, 201, 14), (255, 255, 255)) star_empty = thorpy.change_color_on_img(star, (255, 0, 255), (255, 255, 255), (255, 255, 255)) w, h = star.get_size() stars = thorpy.Element.make(size=(5 * w, 2 * h * len(skills))) skill_elements = [] for skillname in skills: value = skills[skillname] sname = thorpy.make_text(skillname + ": ") nfill = int(value * 5) nempty = 5 - nfill elements = [sname] for i in range(nfill): elements.append(thorpy.Image.make(star_full)) for i in range(nempty): elements.append(thorpy.Image.make(star_empty)) skill_elements.append(thorpy.make_group(elements)) eskill = thorpy.Box.make(skill_elements) # e = thorpy.Element.make(elements=[eimage, ename, eskill, eok]) thorpy.store(e, mode="h") e.fit_children() return e
def get_vessel_element(v): red = (255,50,50) green = (50,255,50) f = round(v.engine.force * 10000 * 1.5) c = round(v.engine.max_fuel/100.) t = round(v.turn * 100) d = round(v.friction * 100,2) m = round(v.mass * 4000) e_engine = thorpy.SkillBar.make("Power: "+str(f)+" kW",MIN_POWER,MAX_POWER, green, size=(150,30)) e_engine.set_life(v.engine.force) ## e_engine = thorpy.make_text("Power: "+str(f)+" kW") e_turn = thorpy.SkillBar.make("Agility: "+str(t), parameters.MIN_TURN, parameters.MAX_TURN, green, size=(150,30)) e_turn.set_life(v.turn/parameters.TURN/5.) #!! ## e_turn = thorpy.make_text("Agility: "+str(t)) e_consumption = thorpy.SkillBar.make("Max fuel: "+str(c)+" L", parameters.MIN_FUEL, parameters.MAX_FUEL, green, size=(150,30)) e_consumption.set_life(v.engine.max_fuel) ## e_consumption = thorpy.make_text("Consumption: "+str(c)+" kW/L") e_friction = thorpy.SkillBar.make("Friction: "+str(d), 0., 1., red, size=(150,30)) e_friction.set_life(v.friction/parameters.FRICTION/5.) ## e_friction = thorpy.make_text("Friction: "+str(d)) e_mass = thorpy.SkillBar.make("Mass: "+str(m)+" kg", parameters.MIN_MASS, parameters.MAX_MASS, red, size=(150,30)) e_mass.set_life(v.mass/parameters.MASS/5.) ## e_mass = thorpy.make_text("Mass: "+str(m)+" kg") box = thorpy.Box.make([e_engine,e_turn,e_consumption,e_friction,e_mass]) thorpy.store(box,align="right",x=box.get_fus_rect().right-3,y=10) box.fit_children() return box
def place_elements_on_background(self): thorpy.store(self.background, self.buttons[0:2], x=game_sets["GAME_SCREEN_WIDTH"] / 2, y=game_sets["GAME_SCREEN_HEIGHT"] / 2 - 100, align="center") thorpy.store(self.background, self.buttons[2:4], x=game_sets["GAME_SCREEN_WIDTH"] / 2, y=game_sets["GAME_SCREEN_HEIGHT"] / 2 + 100, align="center") thorpy.store(self.background, self.inserters, x=game_sets["GAME_SCREEN_WIDTH"] / 2, y=game_sets["GAME_SCREEN_HEIGHT"] / 2 - 100, align="center") thorpy.store(self.background, [self.texts[0]], x=game_sets["GAME_SCREEN_WIDTH"] / 2, y=game_sets["GAME_SCREEN_HEIGHT"] / 2 - 200, align="center") thorpy.store(self.background, self.texts[1:3], x=game_sets["GAME_SCREEN_WIDTH"] / 2 - 225, y=game_sets["GAME_SCREEN_HEIGHT"] / 2 + 200, align="left")
def main(): pygame.init() # Iniciamos pygame. infoObject = pygame.display.Info() icon = pygame.image.load('images/ant.png') preferredSize = int(infoObject.current_h * 0.88) # Obtenemos la altura de la pantalla. application = thorpy.Application((preferredSize, preferredSize), "Langton's Ant", "pygame") # Verifica si el tamaño ingresado es válido y si lo es, inicia la simulación. def start(): if tamaño.get_value().isdigit() and iteraciones.get_value().isdigit(): tam = int(tamaño.get_value()) it = int(iteraciones.get_value()) if tam > 0 and it > 0: if tam > int(0.65 * (preferredSize // 2)): thorpy.launch_blocking_alert( title="¡Tamaño no soportado!", text= "El tamaño actual generaría una grilla con celdas de tamaño cero.", parent=background) tamaño.set_value("") else: simulation.simulate(tam, it, preferredSize) else: thorpy.launch_blocking_alert(title="¡Tamaño no soportado!", text="El tamaño no es válido.", parent=background) else: thorpy.launch_blocking_alert( title="¡Valores incorrectos!", text="Los valores introducidos no son válidos.", parent=background) tamaño.set_value("") iteraciones.set_value("") iteraciones = thorpy.Inserter(name="Iteraciones: ", value="1") # Campo de número de iteraciones. tamaño = thorpy.Inserter(name="Tamaño: ", value="10") # Campo de tamaño. boton = thorpy.make_button("Aceptar", start) # Botón aceptar. title_element = thorpy.make_text("Configurar simulación", 22, (255, 255, 255)) central_box = thorpy.Box(elements=[iteraciones, tamaño, boton]) # Contenedor central. central_box.fit_children(margins=(30, 30)) central_box.center() # center on screen central_box.set_main_color((220, 220, 220, 180)) background = thorpy.Background((0, 0, 0), elements=[title_element, central_box]) thorpy.store(background) menu = thorpy.Menu(background) menu.play() # Lanzamos el menú. application.quit()
def __init__(self, size, hints): #faire pressed! thorpy.Element.__init__(self, elements=[thorpy.make_text("Rule:")]) self.finish() self.set_size(size) self.add_elements([ thorpy.Image.make(h.img, colorkey=(255, 255, 255)) for h in hints ]) thorpy.store(self, mode="h")
def get_infoalert_text(*texts, start="normal"): h = get_help_text(*texts, start) e = thorpy.Element("",[h]) ## e.set_font_size(HFS) ## e.set_font_color(HFC) thorpy.store(e) e.set_main_color((200,200,200,100)) e.fit_children() return e
def displayLauncher(): # Declare Variables index = None #int game = None #String application = None #Application(thorpy) buttons = [None, None, None] #Clickable(thorpy) Array scoreboardButton = None #Clickable(thorpy) quitButton = None #Clickable(thorpy) buttonGroup = None #Group(thorpy) menu = None #Menu(thorpy) background = None #Background(thorpy) painter = None #Painters(thorpy) title = None #Text(thorpy) # Create GUI and set theme application = thorpy.Application(size=(1280, 800), caption="Mini-Arcade") thorpy.theme.set_theme("human") # Create title title = thorpy.make_text("Mini-Arcade\n", 35, (0, 0, 0)) # Create buttons painter = thorpy.painters.roundrect.RoundRect(size=(150, 75), color=(102, 204, 255), radius=0.3) for index, game in enumerate(Launcher.gameTitle): buttons[index] = Launcher.createButton(game, index, painter) scoreboardButton = thorpy.Clickable("Scoreboard") scoreboardButton.user_func = Launcher.launchScoreboard scoreboardButton.set_painter(painter) scoreboardButton.finish() scoreboardButton.set_font_size(23) scoreboardButton.set_font_color_hover((255, 255, 255)) quitButton = thorpy.Clickable("Exit") quitButton.user_func = thorpy.functions.quit_menu_func quitButton.set_painter(painter) quitButton.finish() quitButton.set_font_size(23) quitButton.set_font_color_hover((255, 255, 255)) # Format the buttons buttonGroup = thorpy.make_group(buttons) # Set background background = thorpy.Background( color=(255, 255, 255), elements=[title, buttonGroup, scoreboardButton, quitButton]) thorpy.store(background) # Create menu and display menu = thorpy.Menu(background) menu.play() # Exiting application.quit()
def prepare_menu_buttons(self, buttons, box, menu, x, y, g): [button.set_font_size(30) for button in buttons] [button.scale_to_title() for button in buttons] box.set_main_color((0, 0, 0, 0)) thorpy.store(box, mode="h", gap=g) box.fit_children() box.set_topleft((x, y)) for element in menu.get_population(): element.surface = self.window
def add_elements(self, new_elements): self.box.unblit() self.box.update() self.box.add_elements(new_elements) for el in self.box.get_descendants(): el.surface = self.screen thorpy.store(self.box) self.menu.rebuild(self.box) self.box.blit() self.box.update()
def main_menu(width, height, func_run, func_menu): def func_about(): about_text = thorpy.make_text( 'Game of the Hyenas \n ' 'by \n\n ' 'AnDreWerDnA' ' \n 700y \n Pk \n\n ' '- Python Code Jam 5 -', 25) normal_about, hover_about = 'assets/back-on.png',\ 'assets/back-off.png', back_button = thorpy.make_image_button(img_normal=normal_about, img_hover=hover_about, colorkey=(0, 0, 0)) back_button.user_func = func_menu about_background = thorpy.Background( image='map_objects/menu_bg.jpg', elements=[about_text, back_button]) thorpy.store(about_background) about_menu = thorpy.Menu(about_background) about_menu.play() application = thorpy.Application((width, height), "Code Jam", icon='pyGame') normal, hover = 'assets/play-on.png', 'assets/play-off.png' normal_quit, hover_quit = 'assets/quit-on.png', 'assets/quit-off.png' normal_about, hover_about = 'assets/about-on.png', 'assets/about-off.png' play_button = thorpy.make_image_button(img_normal=normal, img_hover=hover, colorkey=(0, 0, 0)) about_button = thorpy.make_image_button(img_normal=normal_about, img_hover=hover_about, colorkey=(0, 0, 0)) quit_button = thorpy.make_image_button(img_normal=normal_quit, img_hover=hover_quit, colorkey=(0, 0, 0)) background = thorpy.Background( image='map_objects/menu_bg.jpg', elements=[play_button, about_button, quit_button]) # Set functions for the buttons play_button.user_func = func_run quit_button.set_as_exiter() about_button.user_func = func_about thorpy.store(background) menu = thorpy.Menu(background) menu.play() application.quit()
def create_instructions(self): self.ins_buttons = [ thorpy.make_button(txt, func=self.ins_func[txt]) for txt in self.ins_func.keys() ] [button.set_font_size(30) for button in self.ins_buttons] [button.scale_to_title() for button in self.ins_buttons] self.ins_box = thorpy.Box(self.ins_buttons) self.ins_box.set_main_color((0, 0, 0, 0)) thorpy.store(self.ins_box, mode="h", gap=40) self.ins_box.set_topleft((170, 540)) self.ins_box.fit_children() return thorpy.Menu(self.ins_box)
def update_em(self, cell): new_img = cell.unit.get_current_img() self.em_unit_img_img.set_image(new_img) text = cell.unit.name self.em_unit_name.set_text(text) thorpy.store(self.em_unit, mode="h") if not cell.unit.name: unitname = "This unit has no name" else: unitname = cell.unit.name self.em_name.set_text(unitname) thorpy.store(self.em_name_rename, mode="h") self.em.store() self.em.fit_children()
def set_game_gui(game, compass_pos, compass, thermo_pos, thermo): game.e_temp = thorpy.make_text("99 C ") game.e_temp.stick_to("screen", "right", "right") game.e_temp.set_topleft( (thermo_pos[0], compass_pos[1] + compass.get_height() + 5)) # game.e_alt = thorpy.make_text("Alt.: 9000 m") game.e_x = thorpy.make_text("X: 9000 m") game.e_y = thorpy.make_text("Y: 9000 m") game.e_coords = thorpy.Element.make( elements=[game.e_alt, game.e_x, game.e_y]) game.e_coords.set_main_color((200, 200, 255, 100)) thorpy.store(game.e_coords) game.e_coords.fit_children(margins=(15, 2)) game.e_coords.set_topleft(Vector2(thermo_pos)+\ (0,thermo.get_size()[1]+10)) game.e_coords.stick_to("screen", "right", "right", align=False) game.e_coords.move((-5, 0)) # ## game.life_ship = get_lifebar("Ship") game.e_food = LifeBar("Food", color=(0, 255, 0)) game.e_life_ship = LifeBar("Ship seaworthy") game.e_life_a = LifeBar("Astronomer", color=parameters.A_COLOR) game.e_life_h = LifeBar("Hunter", color=parameters.H_COLOR) game.e_life_c = LifeBar("Captain", color=parameters.C_COLOR) # game.e_life = thorpy.Element.make(elements=[ game.e_food, game.e_life_ship, thorpy.Line.make(80, "h"), thorpy.make_text("Life"), game.e_life_a, game.e_life_h, game.e_life_c ]) game.e_life.set_main_color((200, 200, 255, 100)) thorpy.store(game.e_life, gap=10) game.e_life.fit_children() game.e_life.set_topleft( Vector2(thermo_pos) + (0, thermo.get_size()[1] + 10)) game.e_life.stick_to(game.e_coords, "bottom", "top") game.e_life.stick_to("screen", "right", "right", align=False) game.e_life.move((0, 10)) game.echars = { game.a: game.e_life_a, game.h: game.e_life_h, game.c: game.e_life_c } # game.e_clock = thorpy.make_text("Day 0", font_size=thorpy.style.FONT_SIZE + 2) game.e_clock.stick_to(game.e_life, "top", "bottom") game.e_clock.set_topleft((None, 3))
def get_journal(game): journal_title = thorpy.make_text("Journal", font_size=thorpy.style.FONT_SIZE + 3, font_color=TCOLOR) line = thorpy.Line.make(100, "h") elements = [] #entries for name, day, coord, h, t, text in game.journal.entries: e_name = thorpy.make_text(name, font_color=(0, 0, 255)) infos = thorpy.make_text(" ".join([ "| Day", str(day), ", coord=", coord, ", alt=", str(round(h)), ", temp=", str(round(t)) ]), font_color=(50, 50, 150)) ## e_title = thorpy.Element.make(elements=[e_name,infos]) ## thorpy.store(e_title, mode="h") e_title = thorpy.make_group([e_name, infos]) e_title.fit_children() elements.append(e_title) elements.append(thorpy.make_text(" " + thorpy.pack_text(400, text))) subbox = thorpy.Element.make(elements=elements, size=(500, 300)) thorpy.store(subbox, elements, x=3, y=0, align="left", gap=3) # def add_entry(): add_journal_entry(game) thorpy.functions.quit_menu_func() ok = thorpy.make_button("Ok", func=thorpy.functions.quit_menu_func) add = thorpy.make_button("Add entry", func=add_entry) ok_add = thorpy.make_group([ok, add]) # box = thorpy.Element.make(elements=[journal_title, subbox, ok_add]) box.set_main_color((200, 200, 200, 100)) thorpy.store(box) box.fit_children() box.set_size((None, int(1.3 * subbox.get_fus_rect().h))) # subbox.set_prison() subbox.refresh_lift() ## box.add_reaction(thorpy.ConstantReaction(pygame.KEYDOWN, ## thorpy.functions.quit_menu_func, ## {"key":pygame.K_SPACE})) box.center() m = thorpy.Menu([box]) m.play()
def update_e(self, cell): self.cell = cell if cell.name: name = cell.name + " (" + cell.material.name + ")" else: name = cell.material.name self.e_mat_name.set_text(name) new_img = cell.extract_all_layers_img_at_zoom(0) self.e_mat_img.set_image(new_img) thorpy.store(self.e_mat, mode="h") # altitude = round(cell.get_altitude()) alt_text = str(altitude) + "m" coord_text = str(cell.coord) + " " self.e_coordalt.set_text(coord_text + alt_text) self.e_coordalt.recenter()
def __init__(self, parent, screen, background, font, rows, columns): super().__init__(parent, screen, background, font) self.quit_button = thorpy.make_button('Return', func=OptionMenu.quit, params={'self': self}) self.varset = thorpy.VarSet() self.varset.add('rows', value=rows, text='Rows:', limits=(5, 20)) self.varset.add('columns', value=columns, text='Columns:', limits=(5, 20)) self.box = thorpy.ParamSetter([self.varset], elements=[self.quit_button], size=(screen.get_width(), screen.get_height())) thorpy.store(self.box) self.menu = thorpy.Menu(self.box) for element in self.menu.get_population(): element.surface = self.screen
def __init__(self, screen) -> None: self.game_id = None self.screen = screen self.startNewButton = thorpy.make_button("Start Game", func=self.add_players) self.joinButton = thorpy.make_button("Join Game", func=self.join_game) self.player1Edit = thorpy.Inserter.make("Player 1 name: ") self.player2Edit = thorpy.Inserter.make("Player 2 name: ") self.box = thorpy.Box.make(elements=[self.startNewButton, self.joinButton], size=(300, 300)) thorpy.store(self.box) self.box.set_center((screen_middle_h, screen_middle_v)) self.menu = thorpy.Menu(self.box) for el in self.menu.get_population(): el.surface = self.screen self.box.blit() self.box.update()
def update_em(self, cell): new_img = cell.unit.get_current_img() def descr(): thorpy.launch_blocking_alert("Unit infos", cell.unit.get_description(), outside_click_quit=True) self.me.draw() self.em.blit() pygame.display.flip() self.em_unit_img.user_func = descr self.em_unit_img_img.set_image(new_img) self.em_unit_name.set_text(cell.unit.name.capitalize()) baserace = cell.unit.race.baserace.capitalize() playername = cell.unit.get_all_players()[0].name self.em_unit_race.set_text(baserace + " (" + playername + ")") thorpy.store(self.em_unit, mode="h") # self.em_mat_img_img.set_image(self.me.cell_info.e_mat_img.get_image()) self.em.store() self.em.fit_children()
def mainmenu(): W, H = thorpy.functions.get_screen_size() title = graphics.get_title() ## box = thorpy.make_textbox("Credits", text, hline=100) ## box.set_main_color((200,200,200,100)) ## thorpy.launch_blocking(box) e_start = thorpy.make_button("Start game", thorpy.functions.quit_menu_func) e_options, vs = get_options() e_credits = thorpy.make_button("Credits", launch_credits) e_quit = thorpy.make_button("Quit", thorpy.functions.quit_func) space_img = image = thorpy.load_image("Calinou3.png") bck_pos = [0, 0] screen = thorpy.get_screen() def menureac(): if bck_pos[0] - W <= -space_img.get_width(): bck_pos[0] = 0 if bck_pos[1] - H <= -space_img.get_height(): bck_pos[1] = 0 bck_pos[0] -= 0.3 bck_pos[1] -= 0.2 ## bck_pos[1] += sgny * 0.2 screen.blit(space_img, bck_pos) e.blit() pygame.display.flip() e_start.add_reaction( thorpy.ConstantReaction(thorpy.THORPY_EVENT, menureac, {"id": thorpy.constants.EVENT_TIME})) e = thorpy.Ghost.make( elements=[title, e_start, e_options, e_credits, e_quit]) thorpy.store(e) e.center() # m = thorpy.Menu(e, fps=80) m.play() # parameters.SOUND = vs.get_value("sound") parameters.NSMOKE = vs.get_value("smokes") parameters.DEBRIS = vs.get_value("debris")
def update_e(self, unit): changed = False if unit: name = unit.name + " (" + str(unit.quantity) + ")" new_img = unit.get_current_img() self.e_img.visible = True changed = True elif self.unit is not None: name = "Unit infos (no unit)" new_img = self.blank_img self.e_img.visible = False changed = True # if changed: self.e_name.set_text(name) self.e_img.set_image(new_img) if self.e_img.visible: thorpy.store(self.e_group, mode="h") else: thorpy.store(self.e_group, [self.e_name], mode="h") self.unit = unit
def func_about(): about_text = thorpy.make_text( 'Game of the Hyenas \n ' 'by \n\n ' 'AnDreWerDnA' ' \n 700y \n Pk \n\n ' '- Python Code Jam 5 -', 25) normal_about, hover_about = 'assets/back-on.png',\ 'assets/back-off.png', back_button = thorpy.make_image_button(img_normal=normal_about, img_hover=hover_about, colorkey=(0, 0, 0)) back_button.user_func = func_menu about_background = thorpy.Background( image='map_objects/menu_bg.jpg', elements=[about_text, back_button]) thorpy.store(about_background) about_menu = thorpy.Menu(about_background) about_menu.play()
def update_e(self, unit): changed = False if unit: name = unit.name + " (" + str(unit.quantity) + ")" baserace = unit.race.baserace.capitalize() playername = unit.get_all_players()[0].name raceteam = baserace + " (" + playername + ")" new_img = unit.get_current_img() self.e_img.visible = True changed = True elif self.unit is not None: name = "Unit infos (no unit)" raceteam = "" new_img = self.blank_img self.e_img.visible = False changed = True # if changed: self.e_name.set_text(name) self.e_race.set_text(raceteam) thorpy.store(self.e_name_and_race, margin=2, gap=2) self.e_name_and_race.fit_children() self.e_img.set_image(new_img) if self.e_img.visible: thorpy.store(self.e_group, mode="h") else: thorpy.store(self.e_group, [self.e_name_and_race], mode="h") self.unit = unit
def update_em(self, cell): new_img = cell.extract_all_layers_img_at_zoom(0) self.em_mat_img_img.set_image(new_img) text = cell.material.name objs = set([]) for obj in cell.objects: objs.add(obj.name) #split to not take the id for name in objs: text += " (" + name + ")" self.em_mat_name.set_text(text) thorpy.store(self.em_mat, mode="h") self.em_coord.set_text("Coordinates: " + str(cell.coord)) self.em_altitude.set_text("Altitude: " + str(round(cell.get_altitude())) + "m") if not cell.name: cellname = "This location has no name" else: cellname = cell.name self.em_name.set_text(cellname) thorpy.store(self.em_name_rename, mode="h") self.em.store() self.em.fit_children()
def run_application(): W, H = 300, 300 application = thorpy.Application(size=(W, H), caption="Real life example") draggable = thorpy.Draggable("Drag me") sx = thorpy.SliderX(length=100, limvals=(0, W), text="X:", type_=int) sy = thorpy.SliderX(length=100, limvals=(0, H), text="Y:", type_=int) background = thorpy.Background(color=(200, 255, 255), elements=[draggable, sx, sy]) thorpy.store(background, [sx, sy]) reaction1 = thorpy.Reaction( reacts_to=thorpy.constants.THORPY_EVENT, reac_func=refresh_drag, event_args={"id": thorpy.constants.EVENT_SLIDE}, params={ "drag": draggable, "sx": sx, "sy": sy }, reac_name="my reaction to slide event") reaction2 = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=refresh_sliders, event_args={"id": thorpy.constants.EVENT_DRAG}, params={ "drag": draggable, "sx": sx, "sy": sy }, reac_name="my reaction to drag event") background.add_reaction(reaction1) background.add_reaction(reaction2) menu = thorpy.Menu(background) #create a menu for auto events handling menu.play() #launch the menu application.quit()
def update_em(self, cell): #at rmb new_img = cell.get_static_img_at_zoom(0) self.em_mat_img_img.set_image(new_img) # text = cell.material.name for obj in cell.objects: if obj.is_ground: text = obj.name.capitalize() break self.em_mat_name.set_text(text) # objs = set([]) for obj in cell.objects: if obj.is_ground: if "bridge" in obj.str_type: objs.add("Bridge") else: if not obj.name[0] == "*": objs.add(obj.name) #split to not take the id ## if obj.str_type == "fire": ## n = obj.game.burning[obj.cell.coord] ## objs.add("fire ("+str(n)+" turns)") ## else: ## objs.add(obj.name) #split to not take the id text = ", ".join([name for name in objs]) self.em_obj_name.set_text(text.capitalize()) # thorpy.store(self.em_mat, mode="h") self.em_coord.set_text("Coordinates: " + str(cell.coord)) ## self.em_altitude.set_text("Altitude: "+str(round(cell.get_altitude()))+"m") if not cell.name: cellname = "This location has no name" else: cellname = cell.name self.em_name.set_text(cellname) thorpy.store(self.em_name_rename, mode="h") self.em.store() self.em.fit_children()
def update_e(self, cell): #at hover self.cell = cell # text for material and ground text = cell.material.name for obj in cell.objects: if obj.is_ground: text = obj.name.capitalize() break self.e_mat_name.set_text(text) # text for other objects, including units objs = set() for obj in cell.objects: if obj.is_ground: if "bridge" in obj.str_type: objs.add("Bridge") else: if not obj.name[0] == "*": if obj.str_type == "flag": text = "flag" else: text = obj.name objs.add(text) #split to not take the id objs = list(objs) text = "" if len(objs) > 1: text = objs[0] + "(...)" elif len(objs) == 1: text = objs[0] self.e_obj_name.set_text(text.capitalize()) # ## new_img = self.me.extract_image(cell) new_img = cell.get_static_img_at_zoom(0) self.e_mat_img.set_image(new_img) if len(objs) > 0: thorpy.store(self.e_mat_obj) self.e_mat_obj.fit_children() else: self.e_mat_name.stick_to(self.e_mat_img, "right", "left") self.e_obj_name.center(element=self.e_mat_name) self.e_mat_name.move((3, 0)) thorpy.store(self.e_mat, mode="h") self.e_mat.fit_children() # ## altitude = round(cell.get_altitude()) ## alt_text = str(altitude) + "m" ## self.e_coordalt.set_text(coord_text+alt_text) coord_text = str(cell.coord) # + " " self.e_coordalt.set_text(coord_text) ## self.e_coordalt.fit_children() ## self.e_coordalt.recenter() chunk_text = str(cell.get_chunk()) self.e_chunk.set_text(chunk_text) thorpy.store(self.e)
def apploop(DISPLAY): global display_height, display_width #main app loo pygame.display.set_caption('Planetbox') pygame.display.set_icon(ico) display_width, display_height = DISPLAY.get_size() DISPLAY.fill(RICHBLUE) #display background Bg = Background('../imgs/bg.jpg', [0, 0]) DISPLAY.blit( pygame.transform.scale(Bg.image, (display_width, display_height)), Bg.rect) #display sky preview Skyprev = SkyPrev(DISPLAY) #update pygame.display.update() #thorpy elements #logo logo = pygame.image.load('../imgs/logo300.png') def radius_check(radius, type): if (type == "terrestrial"): if (radius < 300): return 0 else: return 1 elif (type == "ice" or type == "gas"): if (radius < 200): return 0 else: return 1 # checks if the density is ok def density_check(ptype, pmass, prad): # pmass: 10^22 kg, prad: km rad = prad * 100000 # radius in cm vol = (4 / 3) * math.pi * rad * rad * rad # cm3 mass = pmass * pow(10, 25) # g g = mass / vol # g/cm3 if ptype == "terrestrial": if (g < 3.6): # not sure about this one return 0 elif (g > 28): return 1 else: return 2 elif ptype == "gas" or ptype == "ice": if (g < 0.2): # not sure about this one return 0 elif (g > 17): return 1 else: return 2 #Reading inputs functions def read_inserter_func( event): #Reactions functions must take an event as first arg global prad, pmass, pname, pdist try: if (event.el == p_rad): prad = event.el.get_value() prad = int(prad) elif (event.el == p_dist): pdist = event.el.get_value() pdist = float(pdist) elif (event.el == p_mass): pmass = event.el.get_value() pmass = int(pmass) elif (event.el == p_name): pname = event.el.get_value() elif (event.el == p_kind): ptype = event.el.get_value() except: # handling errors AlertBox.AlertBox(1, "valueError") p_rad.set_value("") p_dist.set_value("") p_mass.set_value("") p_rad.unblit_and_reblit() p_dist.unblit_and_reblit() p_mass.unblit_and_reblit() def reset_simulation(): simulation.Planets = [] simulation.PlanetsCord = [] SkyPrev(DISPLAY) # pressing add planet btn reaction def readPlanet(): global prad, pmass, pname, ptype, simulation, pdist ptype = p_kind.get_selected().get_text() den_val = density_check(ptype, pmass, prad) rad_val = radius_check(prad, ptype) if den_val == 0 or den_val == 1: # wrong density AlertBox.AlertBox(den_val, "density") elif rad_val == 0: # radius too small AlertBox.AlertBox(rad_val, "radius") else: #print(ptype) # create a new planet # clean the inserters p_rad.set_value("") p_rad.unblit_and_reblit() p_dist.set_value("") p_dist.unblit_and_reblit() p_mass.set_value("") p_mass.unblit_and_reblit() p_name.set_value("") p_name.unblit_and_reblit() planet = Planet.Planet(prad, pmass, ptype, pdist, pname) simulation.AddPlanet(planet) # update preview SkyPrev(DISPLAY) def startExplorer(): #print("Starting explorer...") planetExp.pe_main() def startSimulation(): #simulation.CreateMoons() #print("Starting simulation...") print(simulation.PrintPlanets()) simulation_gui.create(simulation) # add button: adds planet to a list and updates prev addBtn = thorpy.make_button("Add planet", func=readPlanet) addBtn.set_size((120, 20)) addBtn.set_main_color(RICHBLUE) addBtn.set_font_color(WHITE) # new window: starts simulation startBtn = thorpy.make_button("Start simulation", func=startSimulation) startBtn.set_size((120, 20)) startBtn.set_main_color(RICHBLUE) startBtn.set_font_color(WHITE) # explore planets: lets u choose a planet and prints info about it and moons simulation prevBtn = thorpy.make_button("Explore the planets", func=startExplorer) prevBtn.set_size((120, 20)) prevBtn.set_main_color(RICHBLUE) prevBtn.set_font_color(WHITE) # reset simulation resBtn = thorpy.make_button("Reset simulation", func=reset_simulation) resBtn.set_size((120, 20)) resBtn.set_main_color(RICHBLUE) resBtn.set_font_color(WHITE) # radius input p_rad_txt = thorpy.OneLineText(text="Radius of the planet(km):") p_rad = thorpy.Inserter(name="", value="", size=(100, 20)) radReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=read_inserter_func, event_args={ "id": thorpy.constants.EVENT_INSERT, "el": p_rad }) p_rad.add_reaction(radReact) # distance to the sun input p_dist_txt = thorpy.OneLineText(text="Distance to the sun (AU):") p_dist = thorpy.Inserter(name="", value="", size=(100, 20)) distReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=read_inserter_func, event_args={ "id": thorpy.constants.EVENT_INSERT, "el": p_dist }) p_dist.add_reaction(distReact) # name input p_name_txt = thorpy.OneLineText(text="Name of the planet: ") p_name = thorpy.Inserter(name="", value="", size=(100, 20)) nameReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=read_inserter_func, event_args={ "id": thorpy.constants.EVENT_INSERT, "el": p_name }) p_name.add_reaction(nameReact) # mass input p_mass_txt = thorpy.OneLineText(text="Mass of the planet (10^22 kg):") p_mass = thorpy.Inserter(name="", value="", size=(100, 20)) massReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT, reac_func=read_inserter_func, event_args={ "id": thorpy.constants.EVENT_INSERT, "el": p_mass }) p_mass.add_reaction(massReact) # type of planet input radio_txt = thorpy.make_text("Type of the planet: ") radios = [ thorpy.Checker("gas", type_="radio"), thorpy.Checker("ice", type_="radio"), thorpy.Checker("terrestrial", type_="radio") ] p_kind = thorpy.RadioPool(radios, first_value=radios[1], always_value=True) # title above the preview prev_txt = thorpy.make_text("Preview of the planetary system: ", 24, WHITE) prev_txt.set_font("Ubuntu.ttf") prev_txt.set_topleft((420, 200)) # blit thingies entries = [ p_rad_txt, p_rad, p_mass_txt, p_mass, p_dist_txt, p_dist, p_name_txt, p_name ] txts = [radio_txt] buttons = [addBtn, prevBtn, startBtn, resBtn] elements = entries + txts + radios + buttons boxBtn = thorpy.Box.make(elements=elements) boxBtn.set_main_color(WHITE) boxBtn.set_size((260, 500)) thorpy.store(boxBtn, elements, align="center") menu = thorpy.Menu(elements=[prev_txt, boxBtn]) for element in menu.get_population(): element.surface = DISPLAY boxBtn.set_topleft((40, 50)) boxBtn.blit() boxBtn.update() prev_txt.blit() prev_txt.update() DISPLAY.blit(logo, (390, 20)) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.VIDEORESIZE: DISPLAY = pygame.display.set_mode( event.dict['size'], pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE) DISPLAY.blit( pygame.transform.scale(Bg.image, event.dict['size']), (0, 0)) display_width, display_height = event.dict['size'] apploop(DISPLAY) pygame.display.flip() menu.react(event)
def run(): import thorpy application = thorpy.Application((600, 600), "ThorPy test") ##thorpy.theme.set_theme("human") #### SIMPLE ELEMENTS #### ghost = thorpy.Ghost() ghost.finish() element = thorpy.Element("Element") element.finish() thorpy.makeup.add_basic_help( element, "Element instance:\nMost simple graphical element.") clickable = thorpy.Clickable("Clickable") clickable.finish() clickable.add_basic_help( "Clickable instance:\nCan be hovered and pressed.") draggable = thorpy.Draggable("Draggable") draggable.finish() thorpy.makeup.add_basic_help(draggable, "Draggable instance:\nYou can drag it.") #### SIMPLE Setters #### checker_check = thorpy.Checker("Checker") checker_check.finish() thorpy.makeup.add_basic_help( checker_check, "Checker instance:\nHere it is of type 'checkbox'.") checker_radio = thorpy.Checker("Radio", typ="radio") checker_radio.finish() thorpy.makeup.add_basic_help( checker_radio, "Checker instance:\nHere it is of type 'radio'.") browser = thorpy.Browser("../../", text="Browser") browser.finish() browser.set_prison() browserlauncher = thorpy.BrowserLauncher(browser, name_txt="Browser", file_txt="Nothing selected", launcher_txt="...") browserlauncher.finish() browserlauncher.scale_to_title() thorpy.makeup.add_basic_help( browserlauncher, "Browser instance:\nA way for user to find a file or" + "\na folder on the computer.") dropdownlist = thorpy.DropDownListLauncher( name_txt="DropDownListLauncher", file_txt="Nothing selected", titles=[str(i) for i in range(1, 9)]) dropdownlist.finish() dropdownlist.scale_to_title() thorpy.makeup.add_basic_help(dropdownlist, "DropDownList:\nDisplay a list of choices.") slider = thorpy.SliderX(120, (5, 12), "Slider: ", typ=float, initial_value=8.4) slider.finish() thorpy.makeup.add_basic_help( slider, "SliderX:\nA way for user to select a value." + "\nCan select any type of number (int, float, ..).") slider.set_center inserter = thorpy.Inserter(name="Inserter: ", value="Write here.") inserter.finish() thorpy.makeup.add_basic_help( inserter, "Inserter:\nA way for user to insert a value.") text_title = thorpy.make_text("Test Example", 25, (0, 0, 255)) central_box = thorpy.Box("", [ ghost, element, clickable, draggable, checker_check, checker_radio, dropdownlist, browserlauncher, slider, inserter ]) central_box.finish() central_box.center() central_box.add_lift() central_box.set_main_color((200, 200, 255, 120)) background = thorpy.Background(color=(200, 200, 200), elements=[text_title, central_box]) background.finish() thorpy.store(background) menu = thorpy.Menu(background) menu.play() application.quit()
def run(): import thorpy application = thorpy.Application((800, 600), "ThorPy Overview") #texts that summary the element's roles: text_element = "Element instance:\nMost simple graphical element." text_clickable = "Clickable instance:\nCan be hovered and pressed." text_draggable = "Draggable instance:\nYou can drag it." text_check = "Checker instance:\nHere it is of type 'checkbox'." text_radio = "Checker instance:\nHere it is of type 'radio'." text_browser = "Browser instance:\nFind a file or a folder on the computer." text_dropdownlist = "DropDownList:\nDisplay a list of choices." text_slider = "SliderX:\nA way for user to select a value.\n" +\ "Can be any type of number (int, float, bool, ..)" text_text = "Text:\nThis is just a raw element with a certain type of style." #actual declaration of the elements #this element has no effect : it contains nothing and it is a ghost ghost = thorpy.Ghost() ghost.finish() element = thorpy.Element("Element") element.finish() thorpy.makeup.add_basic_help(element, text_element) clickable = thorpy.Clickable("Clickable") clickable.finish() thorpy.makeup.add_basic_help(clickable, text_clickable) draggable = thorpy.Draggable("Draggable") draggable.finish() thorpy.makeup.add_basic_help(draggable, text_draggable) checker_check = thorpy.Checker("Checker") checker_check.finish() thorpy.makeup.add_basic_help(checker_check, text_check) checker_radio = thorpy.Checker("Radio", type_="radio") checker_radio.finish() thorpy.makeup.add_basic_help(checker_radio, text_radio) browser = thorpy.Browser("../../", text="Browser") browser.finish() browserlauncher = thorpy.BrowserLauncher(browser, name_txt="Browser", file_txt="Nothing selected", launcher_txt="...") browserlauncher.finish() browserlauncher.scale_to_title() thorpy.makeup.add_basic_help(browserlauncher, text_browser) dropdownlist = thorpy.DropDownListLauncher(text="DropDownListLauncher", file_txt="Nothing selected", titles=[str(i) for i in range(1, 9)]) dropdownlist.finish() dropdownlist.scale_to_title() thorpy.makeup.add_basic_help(dropdownlist, text_dropdownlist) slider = thorpy.SliderX(120, (5, 12), "Slider: ", type_=float, initial_value=8.4) slider.finish() thorpy.makeup.add_basic_help(slider, text_slider) inserter = thorpy.Inserter(name="Inserter: ", value="Write here.") inserter.finish() thorpy.makeup.add_basic_help(inserter, "Inserter:\nA way for user to insert a value.") title_element = thorpy.make_text("Overview example", 22, (255,255,0)) thorpy.makeup.add_basic_help(title_element, text_text) central_box = thorpy.Box("", [ghost, element, clickable, draggable, checker_check, checker_radio, dropdownlist, browserlauncher, slider, inserter]) central_box.finish() central_box.center() central_box.add_lift() central_box.set_main_color((220,220,220,180)) background = thorpy.Background(image=thorpy.style.EXAMPLE_IMG, elements=[title_element, central_box]) background.finish() thorpy.store(background) menu = thorpy.Menu(background) menu.play() application.quit()