def _create_panel(self, manager: UIManager, screen_width: int, screen_height: int): self.width = 300 self.height = 355 rect = create_modal_rect(screen_width, screen_height, self.width, self.height) self.panel = UIPanel(rect, 0, manager, element_id='guess_panel') y_offset = 20 self.make_label("Make a guess", y_offset) y_offset += 20 + 20 self._character_menu = self.make_drop_down(Character, y_offset) y_offset += 25 + 10 self.make_label("in the", y_offset) y_offset += 20 + 10 self._room_menu = self.make_drop_down(Room, y_offset) y_offset += 25 + 10 self.make_label("with the", y_offset) y_offset += 20 + 10 self._weapon_menu = self.make_drop_down(Weapon, y_offset) y_offset += 20 + 50 button_rect = pygame.Rect((50, y_offset), (200, 30)) self._guess_button = UIButton(button_rect, '', manager, container=self.panel) self.panel.hide()
def __init__(self): self.manager = UIManager((800, 600), 'themes/theme.json') self.title = UILabel(relative_rect=pygame.Rect(350, 50, 100, 50), text='Connexion', manager=self.manager) self.labelPseudo = UILabel(relative_rect=pygame.Rect( 400 - 210, 142, 100, 50), text='Pseudo', manager=self.manager) self.pseudo = UITextEntryLine(relative_rect=pygame.Rect( 400 - 100, 150, 200, 50), manager=self.manager) self.labelPassword = UILabel(relative_rect=pygame.Rect( 400 - 210, 222, 100, 50), text='Mot de passe', manager=self.manager) self.password = UITextEntryLine(relative_rect=pygame.Rect( 400 - 100, 230, 200, 50), manager=self.manager) self.buttonLogin = UIButton(relative_rect=pygame.Rect( 400 - 100, 320, 200, 50), text='Se connecter', manager=self.manager) self.buttonSignup = UIButton(relative_rect=pygame.Rect( 400 - 100, 400, 200, 50), text='Pas de compte ?', manager=self.manager)
def __init__(self): self.manager = UIManager((800, 600), 'themes/home.json') self.title = UILabel(relative_rect=pygame.Rect(400 - 100, 20, 200, 50), text='Instruction', manager=self.manager, object_id='#title') self.text = UITextBox( relative_rect=pygame.Rect(100, 100, 800 - 200, 600 - 100), html_text= "<body><b>Touche</b><br>Espace -> Tirer<br>Entrer -> Magasin<br>Echap -> Menu de sauvegarde<br>Flèche droite et gauche -> Se déplacer<br><br><b>Système de Niveau</b><br>Niveau 1 -> 0 à 5000 points<br>Niveau 2 -> 5000 à 10000 points<br>Niveau 3 -> 10000 à 15000 points<br>Niveau 4 -> plus de 15000 points<br><br><b>Système du jeu</b><br>Le Joueur commence avec 3 vies, lorsque un monstre touche le joueur celui-ci perd une vie et le monstre meurt.<br>Un monstre rapporte 100 points et 1 credit lorsque le joueur le tue et retire 50 points lorsque le monstre touche le sol<br>Le Joueur a un pourcentage de chance d'obtenir un bonus lorsqu'il tue un monstre<br><br><b>Les Bonus</b><br>Double attaque -> Double les dégats du missile du joueur<br>3 missiles -> Tire 3 missiles au lieu de 1<br>Double score -> Double le score du joueur<br>Temps d'apparition -> Le temps d'apparition est augmenté de 2s</body>", manager=self.manager) self.buttonReturn = UIButton(relative_rect=pygame.Rect( 20, 20, 200, 50), text='Retour', manager=self.manager) self.buttonLogout = UIButton(relative_rect=pygame.Rect( 800 - 220, 20, 200, 50), text='Déconnexion', manager=self.manager, object_id='#button-logout')
def __init__(self): pygame.init() pygame.display.set_caption('Image Load App') self.window_surface = pygame.display.set_mode((800, 600)) self.ui_manager = pygame_gui.UIManager( (800, 600), 'data/themes/image_load_app_theme.json') self.background = pygame.Surface((800, 600)) self.background.fill( self.ui_manager.ui_theme.get_colour(None, None, 'dark_bg')) self.load_button = UIButton(relative_rect=pygame.Rect( -180, -60, 150, 30), text='Load Image', manager=self.ui_manager, anchors={ 'left': 'right', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom' }) self.file_dialog = None # scale images, if necessary so that their largest dimension does not exceed these values self.max_image_display_dimensions = (400, 400) self.display_loaded_image = None self.clock = pygame.time.Clock() self.is_running = True
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Map Editor Menu", self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 100, 400, get_param('element_height')) button_rect.centerx = panel_rect.w // 2 self.ui_elements['btn_new'] = UIButton(button_rect, "New Map", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_load'] = UIButton(button_rect, "Load Map", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel'])
def _create_panel(self, screen_width: int, screen_height: int): self.width = 300 self.height = 275 rect = create_modal_rect(screen_width, screen_height, self.width, self.height) self.panel = UIPanel(rect, 0, self.manager, element_id='start_turn') UILabel(pygame.Rect((0, 10), (self.width, 20)), "Your Turn", self.manager, container=self.panel, object_id="windowTitle") y_offset = 10 + 10 roll_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._roll_button = UIButton(roll_button_rect, 'Roll', self.manager, container=self.panel) y_offset += 25 + 50 self._guess_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_guess_button() y_offset += 25 + 50 rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._accuse_button = UIButton(rect, 'Accuse', self.manager, container=self.panel) self.hide()
def build_gui(self): super().build_gui() panel_rect = Rect(0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Select Existing Game Map", self.gui, self.ui_elements['panel']) # buttons select_rect = Rect(0, 0, 400, get_param('element_height') * 3) select_rect.y = scene_label_rect.bottom + get_param('element_padding') select_rect.centerx = panel_rect.w // 2 self.ui_elements['map_select'] = UISelectionList(select_rect, [f"Map {n}" for n in range(20)], self.gui, container=self.ui_elements['panel']) preview_rect = Rect(0, 0, 256, get_param('element_height') * 8) preview_rect.y = select_rect.bottom + get_param('element_padding') preview_rect.centerx = panel_rect.w // 2 self.ui_elements['preview'] = UILabel(preview_rect, "PREVIEW AREA", self.gui, self.ui_elements['panel']) button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.y = preview_rect.bottom + get_param('element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel']) button_rect.centerx = (panel_rect.w // 2) + 100 self.ui_elements['btn_next'] = UIButton(button_rect, "Next", self.gui, self.ui_elements['panel']) self.ui_elements['btn_next'].disable()
def __init__(self): pygame.init() pygame.display.set_caption('Colour Picking App') self.window_surface = pygame.display.set_mode((800, 600)) self.ui_manager = pygame_gui.UIManager( (800, 600), 'data/themes/colour_picker_app_theme.json') self.background = pygame.Surface((800, 600)) self.background.fill( self.ui_manager.ui_theme.get_colour(None, None, 'dark_bg')) self.pick_colour_button = UIButton(relative_rect=pygame.Rect( -180, -60, 150, 30), text='Pick Colour', manager=self.ui_manager, anchors={ 'left': 'right', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom' }) self.colour_picker = None self.current_colour = pygame.Color(0, 0, 0) self.picked_colour_surface = pygame.Surface((400, 400)) self.picked_colour_surface.fill(self.current_colour) self.clock = pygame.time.Clock() self.is_running = True
def __init__(self): self.manager = UIManager((800, 600), 'themes/signup.json') self.title = UILabel(relative_rect=pygame.Rect(400 - 100, 50, 200, 50), text='Inscription', manager=self.manager) self.labelPseudo = UILabel(relative_rect=pygame.Rect(400 - 210, 142, 100, 50), text='Pseudo', manager=self.manager) self.pseudo = UITextEntryLine(relative_rect=pygame.Rect(400 - 100, 150, 200, 50), manager=self.manager) self.labelPassword = UILabel(relative_rect=pygame.Rect(400 - 210, 222, 100, 50), text='Mot de passe', manager=self.manager) self.password = UITextEntryLine(relative_rect=pygame.Rect(400 - 100, 230, 200, 50), manager=self.manager) self.buttonSignup = UIButton(relative_rect=pygame.Rect(400 - 100, 320, 200, 50), text="S'inscrire", manager=self.manager) self.buttonLogin = UIButton(relative_rect=pygame.Rect(400 - 100, 400, 200, 50), text="Déja un compte ?", manager=self.manager)
class Button(UIElement): def __init__(self, text, layout_info, container, bg_colors: BackgroundColors = None, padding=None, shape='rectangle', corner_radius_ratio=None): self._corner_radius_ratio = corner_radius_ratio self.bg_colors = bg_colors super().__init__(layout_info, container, padding) self.element = UIButton(relative_rect=self.bounds, manager=self.manager, text=text) self.element.shape = shape self._set_colors(bg_colors) self.layout() @property def text(self): return self.element.text def set_text(self, text): self.element.text = text self.element.rebuild() def _set_colors(self, bg_colors): if bg_colors is None: return if bg_colors.Enabled is not None: self.element.colours['normal_bg'] = bg_colors.Enabled else: self.bg_colors.Enabled = self.element.colours['normal_bg'] if bg_colors.Disabled is not None: self.element.colours['disabled_bg'] = bg_colors.Disabled else: self.bg_colors.Enabled = self.element.colours['disabled_bg'] def on_enable(self, enabled: bool): super().on_enable(enabled) if self.bg_colors is None or self.element is None: return c = self.bg_colors self.element.colours[ 'normal_bg'] = c.Enabled if enabled else c.Disabled self.rebuild() def layout(self, only_if_changed=True): super().layout(only_if_changed) if self.element is not None and self._corner_radius_ratio is not None: min_dimension = min(self.width, self.height) corner_radius = int(self._corner_radius_ratio * min_dimension) self.element.shape_corner_radius = corner_radius self.rebuild()
def __init__(self): self.manager = UIManager((800, 600), 'themes/loading.json') self.title = UILabel(relative_rect=pygame.Rect(400 - 100, 20, 200, 50), text='Charger une partie', manager=self.manager, object_id='#title') self.scrollingContainer = UIScrollingContainer( relative_rect=pygame.Rect(100, 100, 700, 400), manager=self.manager) self.verticalScrollBar = UIVerticalScrollBar( relative_rect=pygame.Rect(680, 0, 20, 400), manager=self.manager, visible_percentage=0.8, container=self.scrollingContainer) self.background = UIPanel(relative_rect=pygame.Rect(0, 0, 500, 80), starting_layer_height=0, manager=self.manager, container=self.scrollingContainer) self.name = UILabel(relative_rect=pygame.Rect(10, 10, 200, 20), text='Charger une partie', manager=self.manager, object_id='#text-button', container=self.background) self.date = UILabel(relative_rect=pygame.Rect(300, 10, 200, 20), text='date de la partie', manager=self.manager, object_id='#text-button', container=self.background) self.currentScore = UILabel(relative_rect=pygame.Rect(10, 40, 200, 20), text='45544', manager=self.manager, object_id='#text-button', container=self.background) self.maxScore = UILabel(relative_rect=pygame.Rect(300, 40, 200, 20), text='1255454', manager=self.manager, object_id='#text-button', container=self.background) self.buttonCreating = UIButton(relative_rect=pygame.Rect( 0, 0, 500, 80), text='', manager=self.manager, container=self.scrollingContainer) self.buttonLogout = UIButton(relative_rect=pygame.Rect( 800 - 220, 20, 200, 50), text='Déconnexion', manager=self.manager, object_id='#button-logout')
def _setup_ui(self) -> None: """ Set up the UI components for the game. This helper is called inside the __init__ and creates several of the private instance attributes. It is called again any time the puzzle to be solved is changed. """ self._variable_map = [] self._variable_name = [] n_variables = len(self._puzzle.variables) for vname in self._puzzle.variables: rect = pygame.Rect( (UI_WIDTH // 2, UI_ITEM_HEIGHT * len(self._variable_name)), (UI_WIDTH // 2, UI_ITEM_HEIGHT)) uidrop = UIDropDownMenu([str(x) for x in range(10)], relative_rect=rect, starting_option=str( self._puzzle.variables[vname]), manager=self._manager) self._variable_map.append(uidrop) rect = pygame.Rect((0, UI_ITEM_HEIGHT * len(self._variable_name)), (UI_WIDTH // 2, UI_ITEM_HEIGHT)) label = UILabel(relative_rect=rect, text=vname, manager=self._manager) self._variable_name.append(label) rect = pygame.Rect((0, UI_ITEM_HEIGHT * n_variables), (UI_WIDTH, UI_ITEM_HEIGHT)) # target label UILabel(relative_rect=rect, text=f"Target: {self._puzzle.target}", manager=self._manager) rect = pygame.Rect((0, UI_ITEM_HEIGHT * (n_variables + 1)), (UI_WIDTH, UI_ITEM_HEIGHT)) tree_evaluation = self._tree.eval(self._puzzle.variables) self._result_label = UILabel(relative_rect=rect, text=f"Current:" f" {tree_evaluation}", manager=self._manager) rect = pygame.Rect((0, UI_HEIGHT // 2), (UI_WIDTH // 2, UI_ITEM_HEIGHT)) self._hint_button = UIButton(relative_rect=rect, text='HINT', manager=self._manager) rect = pygame.Rect((0, UI_HEIGHT // 2 + UI_ITEM_HEIGHT), (UI_WIDTH // 2, UI_ITEM_HEIGHT)) self._new_button = UIButton(relative_rect=rect, text='NEW', manager=self._manager)
def __init__(self, rect: pygame.Rect, manager: IUIManagerInterface, action_long_desc: str, *, window_title: str = 'Confirm', action_short_name: str = 'OK', blocking: bool = True, object_id: Union[ObjectID, str] = ObjectID('#confirmation_dialog', None), visible: int = 1): super().__init__(rect, manager, window_display_title=window_title, object_id=object_id, resizable=True, visible=visible) minimum_dimensions = (260, 200) if rect.width < minimum_dimensions[0] or rect.height < minimum_dimensions[1]: warn_string = ("Initial size: " + str(rect.size) + " is less than minimum dimensions: " + str(minimum_dimensions)) warnings.warn(warn_string, UserWarning) self.set_minimum_dimensions(minimum_dimensions) self.confirm_button = UIButton(relative_rect=pygame.Rect(-220, -40, 100, 30), text=action_short_name, manager=self.ui_manager, container=self, object_id='#confirm_button', anchors={'left': 'right', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom'}) self.cancel_button = UIButton(relative_rect=pygame.Rect(-110, -40, 100, 30), text='Cancel', manager=self.ui_manager, container=self, object_id='#cancel_button', anchors={'left': 'right', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom'}) text_width = self.get_container().get_size()[0] - 10 text_height = self.get_container().get_size()[1] - 50 self.confirmation_text = UITextBox(html_text=action_long_desc, relative_rect=pygame.Rect(5, 5, text_width, text_height), manager=self.ui_manager, container=self, anchors={'left': 'left', 'right': 'right', 'top': 'top', 'bottom': 'bottom'}) self.set_blocking(blocking)
def main(): # Cria uma janela grafica usando as configuracoes do # computador screen = pygame.display.set_mode(SCREEN_SIZE) # Cria um UIManager do Pygame GUI pra controlar procesando # os eventos do jogo manager = pygame_gui.UIManager(SCREEN_SIZE) # Cira os butoes do jogo flip_button = UIButton(relative_rect=pygame.Rect((0, 320), (100, 80)), text='Flip it!', manager=manager) speed_button = UIButton(relative_rect=pygame.Rect((100, 320), (100, 80)), text='Speed up!', manager=manager) slow_button = UIButton(relative_rect=pygame.Rect((200, 320), (100, 80)), text='Slow down!', manager=manager) # Cria a formiga formiga = Formiga() clock = pygame.time.Clock() running = True while running: time_delta = clock.tick(FPS) / 1000.0 # Reagir aos eventos do jogo for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.USEREVENT: if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == flip_button: formiga.reverse() elif event.ui_element == speed_button: formiga.speed_up() elif event.ui_element == slow_button: formiga.slow_down() manager.process_events(event) # Atualiza os objetos do jogo manager.update(time_delta) formiga.update() # Atualiza a janela do jogo screen.fill(WHITE) formiga.draw(screen) manager.draw_ui(screen) pygame.display.flip()
def _gen_save_button(self): button_layout_rect = pygame.Rect(int(0.05 * self._window_w), int(8 * self._window_h / 10), menu_button_w // 2, menu_button_h // 2) button = UIButton(relative_rect=button_layout_rect, text="Save", manager=self._ui_manager) button.select() return button
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Settings", self.gui, self.ui_elements['panel']) # screen size label_rect = Rect(0, 0, 150, get_param('element_height')) label_rect.y += scene_label_rect.bottom + get_param('element_padding') dd_rect = Rect(0, 0, 250, get_param('element_height')) dd_rect.y = label_rect.y label_rect.centerx = 125 dd_rect.centerx = 325 self.ui_elements['d_size_label'] = UILabel(label_rect, "Display Size", self.gui, self.ui_elements['panel']) self.ui_elements['dd_d_size'] = UIDropDownMenu( get_param('display_sizes'), get_param('current_display_size'), dd_rect, self.gui, self.ui_elements['panel']) # full screen label_rect.y += get_param('element_height') + get_param( 'element_padding') dd_rect.y = label_rect.y self.ui_elements['fs_label'] = UILabel(label_rect, "Full Screen", self.gui, self.ui_elements['panel']) self.ui_elements['dd_fs'] = UIDropDownMenu( ["On", "Off"], get_param('display_full_screen_value'), dd_rect, self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.y = label_rect.bottom + get_param('element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel']) # the apply button always starts off disabled button_rect.centerx = (panel_rect.w // 2) + 100 self.ui_elements['btn_apply'] = UIButton(button_rect, "Apply", self.gui, self.ui_elements['panel']) self.ui_elements['btn_apply'].disable() # re-assign values for our check parameters, this is to control the apply buttons state self._check_screen_size = get_param('current_display_size') self._check_full_screen = get_param('display_full_screen_value')
def __init__(self, args: dict, int_sec, rect, ui_manager): self.game_args = args self.int_sec = int_sec super().__init__(rect, ui_manager, window_display_title='Waiting for connections', object_id='#connection_window', resizable=True) self.enable_close_button = False self.close_window_button = None size = [150, 35] self.nb_online = self.game_args["nb_online"] self.socket = socket.socket() HOST = socket.gethostbyname(socket.gethostname()) print(HOST, PORT) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind((HOST, PORT)) self.socket.listen(self.nb_online) self.socket_list = [] self.socket_list.append((self.int_sec.player_name, self.socket)) self.thread = ListeningThread(self) self.thread.setDaemon(True) self.thread.start() UILabel(pygame.Rect((0, 0), size), f"Connected players", self.ui_manager, container=self) self.new_game_btn = UIButton( pygame.Rect((self.relative_rect.width - 160, self.relative_rect.height - 140), (100, 40)), 'GO', self.ui_manager, container=self, )
def __init__(self, rect: pygame.Rect, html_message: str, manager: IUIManagerInterface, *, window_title: str = 'Message', object_id: str = '#message_window'): super().__init__(rect, manager, window_display_title=window_title, object_id=object_id, resizable=True) minimum_dimensions = (250, 160) if rect.width < minimum_dimensions[ 0] or rect.height < minimum_dimensions[1]: warn_string = ("Initial size: " + str(rect.size) + " is less than minimum dimensions: " + str(minimum_dimensions)) warnings.warn(warn_string, UserWarning) self.set_minimum_dimensions(minimum_dimensions) self.dismiss_button = None self.text_block = None button_size = (70, 20) button_spacing = 10 button_vertical_space = (button_spacing * 2) + button_size[1] dismiss_button_rect = pygame.Rect((0, 0), button_size) dismiss_button_rect.bottomright = (-button_spacing, -button_spacing) self.dismiss_button = UIButton( relative_rect=dismiss_button_rect, text="Dismiss", manager=manager, container=self, tool_tip_text="Click to get rid of this message.", object_id='#dismiss_button', anchors={ "left": "right", "top": "bottom", "right": "right", "bottom": "bottom" }) text_block_rect = pygame.Rect( 0, 0, self.get_container().get_size()[0], self.get_container().get_size()[1] - button_vertical_space) self.text_block = UITextBox(html_message, text_block_rect, manager=manager, container=self, anchors={ "left": "left", "top": "top", "right": "right", "bottom": "bottom" })
def __init__(self, manager): # load bg module names bgdir = os.path.dirname(os.path.abspath(__file__)) + "/backgrounds/" self.BGLIST = [] for f in os.listdir(bgdir): if "__" in f or ".pyc" in f: continue self.BGLIST.append(f.split(".")[0]) self.BGLIST = sorted(self.BGLIST) # create window super().__init__( pygame.Rect( (100, 100), (self.BSIZE[0] + 32, self.BSIZE[1] * len(self.BGLIST)) ), manager=manager, window_display_title="SnakeBG", object_id="#bgmenu", resizable=False, ) # add buttons for i in range(len(self.BGLIST)): UIButton( relative_rect=pygame.Rect((0, self.BSIZE[1] * i), self.BSIZE), text=self.BGLIST[i], manager=manager, container=self, parent_element=self, object_id=self.BGLIST[i], )
def __init__(self, manager, pos, path, elements, loadfunc): """ manager - UIManager to manage this panel pos - position indices to start drawing this panel at path - the directory this panel represents elements - list of elements in this directory """ super().__init__( pygame.Rect( (pos[0] * BUTTON_DIMS[0], pos[1] * BUTTON_DIMS[1]), (BUTTON_DIMS[0] + 5, BUTTON_DIMS[1] * len(elements.keys()) + 5), ), starting_layer_height=PANEL_LAYER, manager=manager, ) self.pos = pos self.path = path self.elements = elements self.loadfunc = loadfunc # sorted list of element keys to generate the panel from self.element_keys = sorted(list(elements.keys())) # generate buttons for i in range(len(self.element_keys)): UIButton( pygame.Rect((0, i * BUTTON_DIMS[1]), BUTTON_DIMS), text=self.element_keys[i], manager=manager, container=self, object_id="menu-" + self.path.replace(".", "-"), )
def __init__(self, text, layout_info, container, bg_colors: BackgroundColors = None, padding=None, shape='rectangle', corner_radius_ratio=None): self._corner_radius_ratio = corner_radius_ratio self.bg_colors = bg_colors super().__init__(layout_info, container, padding) self.element = UIButton(relative_rect=self.bounds, manager=self.manager, text=text) self.element.shape = shape self._set_colors(bg_colors) self.layout()
def _setup_ui(self) -> None: """ Set up the UI components for the game. This helper is called inside the __init__ and creates several of the private instance attributes. It is called again any time the puzzle to be solved is changed. """ self._buttons = [] for i in range(N): row = [] for j in range(N): rect = pygame.Rect( (UI_ITEM_WIDTH * j + 2, UI_ITEM_HEIGHT * i + 2), (UI_ITEM_WIDTH - 2, UI_ITEM_HEIGHT - 2)) tip = str(self._puzzle.get_possible(i, j)) if self._grid[i][j] == self._solution_grid[i][j]: tip = None label = UIButton(relative_rect=rect, text=self._grid[i][j], tool_tip_text=tip, manager=self._manager) if self._grid[i][j] != EMPTY_CELL: label.disable() row.append(label) self._buttons.append(row) rect = pygame.Rect((0, UI_ITEM_HEIGHT * N), ((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT)) self._hint_button = UIButton(relative_rect=rect, text='CHECK', manager=self._manager) rect = pygame.Rect(((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT * N), ((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT)) self._hint_toggle = UIButton(relative_rect=rect, text='HINTS ON', manager=self._manager) self._hints_on = True rect = pygame.Rect((0, UI_ITEM_HEIGHT * (N + 1)), (UI_ITEM_WIDTH * N, UI_ITEM_HEIGHT)) self._new_button = UIButton(relative_rect=rect, text='NEW', manager=self._manager) # solved label rect = pygame.Rect( (UI_ITEM_WIDTH * (N / 2) - UI_ITEM_WIDTH, UI_ITEM_HEIGHT * (N / 2) - UI_ITEM_HEIGHT / 2), (UI_ITEM_WIDTH * 2, UI_ITEM_HEIGHT)) self._solved_label = UILabel(rect, "SOLVED!", manager=self._manager, visible=0)
def _gen_use_trained_model_button(self): x_center, y_center, w, h = int(0.8 * self._window_w), int( 8 * self._window_h / 10), menu_button_w, menu_button_h // 2 centered_rect = (x_center - w // 2, y_center - h // 2, w, h) button_layout_rect = pygame.Rect(*centered_rect) button = UIButton(relative_rect=button_layout_rect, text="Use Trained Model ?", manager=self._ui_manager) return button
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Main Menu", self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 0, 400, get_param('element_height')) button_rect.y = scene_label_rect.bottom + get_param('element_padding') button_rect.centerx = panel_rect.w // 2 self.ui_elements['btn_new'] = UIButton(button_rect, "New Game", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_load'] = UIButton(button_rect, "Load Game", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_editor'] = UIButton(button_rect, "Map Editor", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_settings'] = UIButton(button_rect, "Settings", self.gui, self.ui_elements['panel']) button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_quit'] = UIButton(button_rect, "Quit Game", self.gui, self.ui_elements['panel'])
def recreate_ui(self): self.ui_manager.set_window_resolution(self.opt.resolution) self.ui_manager.clear_and_reset() self.background_surface = pygame.Surface(self.opt.resolution) self.background_surface.fill( self.ui_manager.get_theme().get_colour('dark_bg')) self.btn_gen_explore = UIButton( pygame.Rect(5, 5, 48, 48), '', manager=self.ui_manager, container=None, object_id='#btn_gen_explore', ) self.btn_gen_search = UIButton( pygame.Rect(5, 58, 48, 48), '', manager=self.ui_manager, container=None, object_id='#btn_gen_search', tool_tip_text='Not yet implemented', ) self.btn_tiles_explore = UIButton( pygame.Rect(5, 111, 48, 48), '', manager=self.ui_manager, container=None, object_id='#btn_tiles_explore', tool_tip_text='Not yet implemented', ) self.btn_tiles_search = UIButton( pygame.Rect(5, 164, 48, 48), '', manager=self.ui_manager, container=None, object_id='#btn_tiles_search', tool_tip_text='Not yet implemented', ) self.main_area = pygame.Rect(5 + 48 + 5, 5, self.opt.W - (5 + 48 + 5 + 5), self.opt.H - (5 + 5)) self.pnl_empty = UIPanel(self.main_area, starting_layer_height=1, manager=self.ui_manager) self.construct_gen_explore() self.construct_gen_search() self.btn_gen_search.disable() self.construct_tiles_explore() self.btn_tiles_explore.disable() self.construct_tiles_search() self.btn_tiles_search.disable() if self.active_panel: self.active_panel.show()
def __init__( self, size: tuple[int, int], manager: IUIManagerInterface, udp: UDP_P2P, username: str, ) -> None: # region Docstring """ Creates a `Chat` object ### Arguments `size {(int, int)}`: `summary`: the size of the chat window `manager {UIManager}`: `summary`: the UIManager that manages this element. `udp {UDP_P2P}`: `summary`: the udp object `username {str}`: `summary`: the username of this host """ # endregion super().__init__(relative_rect=Rect((Game.SIZE[0], 0), size), manager=manager) # region Element creation self.textbox = UITextBox( html_text="", relative_rect=Rect((0, 0), (size[0], size[1] - 25)), manager=manager, container=self, ) self.entryline = UITextEntryLine( relative_rect=Rect((0, size[1] - 28), (size[0] - 50, 20)), manager=manager, container=self, ) self.entryline.set_text_length_limit(100) self.enterbtn = UIButton( text="Enter", relative_rect=Rect((size[0] - 50, size[1] - 28), (50, 30)), manager=manager, container=self, ) # endregion self.record = "" self.size = size self.udp = udp self.username = username
def _gen_buttons_action(self): buttons = {} for i, (label, callback) in enumerate(buttons_dict.items()): button_layout_rect = pygame.Rect( self._window_w // 2 - menu_button_w // 2, buttons_y[i], menu_button_w, menu_button_h) button = UIButton(relative_rect=button_layout_rect, text=label, manager=self._ui_manager) buttons[button] = callback return buttons
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Load Saved Game", self.gui, self.ui_elements['panel']) # buttons select_rect = Rect(0, 0, 400, get_param('element_height') * 3) select_rect.y = scene_label_rect.bottom + get_param('element_padding') select_rect.centerx = panel_rect.w // 2 self.ui_elements['game_select'] = UISelectionList( select_rect, [f"Game {n}" for n in range(5)], self.gui, container=self.ui_elements['panel']) button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.y = select_rect.bottom + get_param('element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_load'] = UIButton(button_rect, "Load Save", self.gui, self.ui_elements['panel']) self.ui_elements['btn_load'].disable() button_rect.centerx = (panel_rect.w // 2 + 100) self.ui_elements['btn_delete'] = UIButton(button_rect, "Delete Save", self.gui, self.ui_elements['panel']) self.ui_elements['btn_delete'].disable() button_rect.w = 400 button_rect.centerx = panel_rect.w // 2 button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel'])
def __init__(self, rect, ui_manager): super().__init__(rect, ui_manager, window_display_title='Scale', object_id='#main_menu', resizable=True) self.options = Options() ui_window = self button_layout_rect = pygame.Rect(30, 20, 100, 40) self.disable_toggle = UIButton(relative_rect=button_layout_rect, text='Disable', manager=ui_manager, object_id='#disable_button', container=ui_window) button_layout_rect1 = pygame.Rect(30, 80, 100, 40) self.hide_toggle = UIButton(relative_rect=button_layout_rect1, text='Hide', manager=self.ui_manager, object_id='#hide_button', container=ui_window)
def aboutPage(): # clear the screen Main.manager.clear_and_reset() Main.screen.fill(Main.manager.get_theme().get_colour(Main.background)) # title logo_font = pygame.font.SysFont("lucida sans", 24, bold=True) word = logo_font.render('About', 0, pygame.Color('white')) Main.screen.blit(word, (int(SIZE[0] / 2 - 25), h)) # about-text alignment font = pygame.font.SysFont("lucida sans", 16) # we're using i to pu lines and button in order i = 1 for line in lines: i += 1 word = font.render(line, 0, pygame.Color('white')) Main.screen.blit(word, (int(SIZE[0] / 4), h + 25 * i)) pygame.display.update() i += 7 # buttons back_btn = UIButton(relative_rect=pygame.Rect(bx, h + 20 * i, w, h), object_id='btn_style', text='Back', manager=Main.manager, anchors={ 'left': 'left', 'right': 'right', 'top': 'top', 'bottom': 'bottom' }) while True: # get events (button clicks) for event in pygame.event.get(): # about if event.type == pygame.USEREVENT and event.user_type == pygame_gui.UI_BUTTON_PRESSED and \ event.ui_element == back_btn: Main.StartPage() # quit if event.type == pygame.QUIT: exit() Main.manager.process_events(event) # refresh every 10 ms Main.manager.update(10) Main.surface.blit(Main.screen, (0, 0)) Main.manager.draw_ui(Main.surface) pygame.display.update()