def construct_gen_explore(self): #self.gen_explore_bg_surface = pygame.Surface(self.opt.resolution) #self.gen_explore_bg_surface.fill(self.ui_manager.get_theme().get_colour('dark_bg')) self.pnl_gen_explore = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.gen_explore_label = UILabel( pygame.Rect(5, 5, 350, 35), 'Explore generators', self.ui_manager, container=self.pnl_gen_explore, ) self.gen_list = UISelectionList( relative_rect=pygame.Rect(5, 75, 350, self.opt.H - 95), item_list=self.gen_state.get_current_gen_list(), manager=self.ui_manager, container=self.pnl_gen_explore, allow_multi_select=False, object_id='#gen_list', ) self.btn_gen_explore_save_as_png = None self.gen_explore_map_image = None self.gen_explore_image = None self.file_dialog_gen_explore_save_as_png = None self.pnl_gen_explore.hide()
def _create_panel(self, manager: UIManager, 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, manager, element_id='match_pick_panel') UILabel(pygame.Rect((0, 10), (self.width, 20)), "Pick a card to show", manager, container=self.panel) y_offset = 10 + 10 self._weapon_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_weapon_button('') y_offset += 25 + 50 self._character_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_character_button('') y_offset += 25 + 50 self._room_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_room_button('') self.panel.hide()
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 _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, "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 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): 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 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, manager: UIManager): self.manager = manager self._checkboxes = dict() panel_rect = pygame.Rect((0,0), (LogBookPanel.PANEL_WIDTH, 1000)) self.panel = UIPanel(panel_rect, 0, manager, element_id='log_book_panel') UILabel(pygame.Rect((0,0), (LogBookPanel.PANEL_WIDTH, 20)), "Player Logbook", manager, container=self.panel, object_id="categoryLabel") height = self._create_section("Chacater", Character, 20) height = self._create_section("Room", Room, height) self._create_section("Weapon", Weapon, height) self.panel.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, "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, layout_info, container, background_color=None, padding=None, depth=0, corner_radius=None): super().__init__(layout_info, container, padding) self.element = UIPanel(relative_rect=self.bounds, manager=self.manager, starting_layer_height=depth) self.background_color = background_color if corner_radius is not None: self.element.shape = 'rounded_rectangle' self.element.shape_corner_radius = corner_radius self.rebuild()
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 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 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, "Start New Game", self.gui, self.ui_elements['panel']) l_side_rect = Rect(0, 0, 150, get_param('element_height')) l_side_rect.centerx = 125 l_side_rect.y = scene_label_rect.bottom + get_param('element_padding') self.ui_elements['l_dd_label'] = UILabel(l_side_rect, "Map Type", self.gui, self.ui_elements['panel']) dd_rect = Rect(0, 0, 250, get_param('element_height')) dd_rect.y = l_side_rect.y dd_rect.centerx = 325 self.ui_elements['l_dd_game_map'] = UIDropDownMenu( ['Existing', 'Random'], 'Existing', dd_rect, self.gui, self.ui_elements['panel']) btn_rect = Rect(0, 0, 200, get_param('element_height')) btn_rect.centerx = (panel_rect.w) // 2 - 100 btn_rect.y = dd_rect.bottom + get_param('element_padding') self.ui_elements['btn_back'] = UIButton(btn_rect, "Back", self.gui, self.ui_elements['panel']) btn_rect = Rect(0, 0, 200, get_param('element_height')) btn_rect.centerx = (panel_rect.w // 2) + 100 btn_rect.y = dd_rect.bottom + get_param('element_padding') self.ui_elements['btn_next'] = UIButton(btn_rect, "Next", self.gui, self.ui_elements['panel'])
def construct_tiles_explore(self): self.pnl_tiles_explore = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.pnl_tiles_explore.hide()
def recreate_ui(self): self.ui_manager.set_window_resolution(self.options.resolution) self.ui_manager.clear_and_reset() self.background_surface = pygame.Surface(self.options.resolution) self.background_surface.fill(self.ui_manager.get_theme().get_colour( None, None, 'dark_bg')) self.test_button = UIButton( pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.90)), (100, 40)), '', self.ui_manager, tool_tip_text="<font face=fira_code color=normal_text size=2>" "<b><u>Test Tool Tip</u></b>" "<br><br>" "A little <i>test</i> of the " "<font color=#FFFFFF><b>tool tip</b></font>" " functionality." "<br><br>" "Unleash the Kraken!" "</font>", object_id='#hover_me_button') self.test_button_2 = UIButton(pygame.Rect( (int(self.options.resolution[0] / 3), int(self.options.resolution[1] * 0.90)), (100, 40)), 'EVERYTHING', self.ui_manager, object_id='#everything_button') self.test_button_3 = UIButton(pygame.Rect( (int(self.options.resolution[0] / 6), int(self.options.resolution[1] * 0.90)), (100, 40)), 'Scaling?', self.ui_manager, object_id='#scaling_button') self.test_slider = UIHorizontalSlider(pygame.Rect( (int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.70)), (240, 25)), 25.0, (0.0, 100.0), self.ui_manager, object_id='#cool_slider') self.test_text_entry = UITextEntryLine(pygame.Rect( (int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.50)), (200, -1)), self.ui_manager, object_id='#main_text_entry') current_resolution_string = (str(self.options.resolution[0]) + 'x' + str(self.options.resolution[1])) self.test_drop_down = UIDropDownMenu( ['640x480', '800x600', '1024x768'], current_resolution_string, pygame.Rect((int(self.options.resolution[0] / 2), int(self.options.resolution[1] * 0.3)), (200, 25)), self.ui_manager) self.panel = UIPanel(pygame.Rect(50, 50, 200, 300), starting_layer_height=4, manager=self.ui_manager) UIButton(pygame.Rect(10, 10, 174, 30), 'Panel Button', manager=self.ui_manager, container=self.panel) UISelectionList(pygame.Rect(10, 50, 174, 200), item_list=[ 'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', 'Item 9', 'Item 10', 'Item 11', 'Item 12', 'Item 13', 'Item 14', 'Item 15', 'Item 16', 'Item 17', 'Item 18', 'Item 19', 'Item 20' ], manager=self.ui_manager, container=self.panel, allow_multi_select=True)
class LogBookPanel: CHECKED_IMG = pygame.image.load('assets/checked.png') UNCHECKED_IMG = pygame.image.load('assets/unchecked.png') PANEL_WIDTH = 200 panel: UIPanel _checkboxes: Dict[UIImage, bool] def __init__(self, manager: UIManager): self.manager = manager self._checkboxes = dict() panel_rect = pygame.Rect((0,0), (LogBookPanel.PANEL_WIDTH, 1000)) self.panel = UIPanel(panel_rect, 0, manager, element_id='log_book_panel') UILabel(pygame.Rect((0,0), (LogBookPanel.PANEL_WIDTH, 20)), "Player Logbook", manager, container=self.panel, object_id="categoryLabel") height = self._create_section("Chacater", Character, 20) height = self._create_section("Room", Room, height) self._create_section("Weapon", Weapon, height) self.panel.hide() def show(self): self.panel.show() def process_events(self, event): if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() # check if click is inside panel first? for cb in self._checkboxes.keys(): if cb.get_abs_rect().collidepoint(pos): self._toggle_img(cb) break def _create_section(self, title: str, items: Enum, y_offset) -> int: item_height = 35 label_rect = pygame.Rect((0, y_offset), (LogBookPanel.PANEL_WIDTH, item_height)) label_text = "<strong>" + title + "</strong>" title_label = UITextBox(label_text, label_rect, self.manager, container=self.panel) y_offset += item_height item_label_width = LogBookPanel.PANEL_WIDTH - 30 for item in items: img_rect = pygame.Rect((5, y_offset + 6), (24, 24)) checkbox_img = UIImage(img_rect, LogBookPanel.UNCHECKED_IMG, self.manager, container=self.panel) label_rect = pygame.Rect((30, y_offset), (item_label_width, item_height)) item_button = UITextBox(item.pretty(), label_rect, self.manager, container=self.panel) self._checkboxes[checkbox_img] = False y_offset += item_height return y_offset def _toggle_img(self, checkbox: UIImage): if self._checkboxes.get(checkbox): checkbox.set_image(LogBookPanel.UNCHECKED_IMG) self._checkboxes[checkbox] = False else: checkbox.set_image(LogBookPanel.CHECKED_IMG) self._checkboxes[checkbox] = True
def __init__(self, rect: pygame.Rect, game): self.rect = rect self.game = game self.after_click = False self.ui_manager = pygame_gui.UIManager((rect.width, rect.height)) self.ui_manager.preload_fonts([{ 'name': 'fira_code', 'point_size': 10, 'style': 'bold' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'regular' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'bold' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'bold_italic' }]) self.info_panel = UIPanel(pygame.Rect( (DESK_WIDTH, 0), (SCREEN_WIDTH - DESK_WIDTH, SCREEN_HEIGHT)), starting_layer_height=4, manager=self.ui_manager) step = 40 self.info_label = UILabel(pygame.Rect( (0, 0), (self.info_panel.rect.width, step)), "Game Statistics", manager=self.ui_manager, container=self.info_panel) self.current_player_label = TextValueLabel( pygame.Rect((0, step), (self.info_panel.rect.width, step)), "Current Player", f"{self.game.current_player.name if self.game.current_player else ''}", self.ui_manager, self.info_panel) self.last_player_label = TextValueLabel( pygame.Rect((0, 3 * step), (self.info_panel.rect.width, step)), "Last player", f"", self.ui_manager, self.info_panel) self.best_player_label = TextValueLabel( pygame.Rect((0, 5 * step), (self.info_panel.rect.width, step)), "Best player", f"", self.ui_manager, self.info_panel) self.best_card_label = TextValueLabel( pygame.Rect((0, 7 * step), (self.info_panel.rect.width, step)), "Best card", f"", self.ui_manager, self.info_panel) btn_panel_width = SCREEN_WIDTH - 200 btn_panel_height = SCREEN_HEIGHT - DESK_HEIGHT self.history_panel = UIPanel(pygame.Rect( (0, DESK_HEIGHT), (btn_panel_width, btn_panel_height)), starting_layer_height=0, manager=self.ui_manager) btn_y = btn_panel_height // 2 - 20 self.main_menu_btn = UIButton(pygame.Rect((50, btn_y), (100, 40)), 'MAIN MENU', self.ui_manager, container=self.history_panel) self.pause_game_btn = UIButton(pygame.Rect( (btn_panel_width // 2 - 50, btn_y), (100, 40)), 'PAUSE', self.ui_manager, container=self.history_panel) self.quit_game_btn = UIButton(pygame.Rect( (btn_panel_width - 150, btn_y), (100, 40)), 'QUIT', self.ui_manager, container=self.history_panel) self.auto_play_btn = UIButton( pygame.Rect(((SCREEN_WIDTH - DESK_WIDTH) // 2 - 50, SCREEN_HEIGHT - btn_y - 40), (100, 40)), 'AUTO ON' if self.game.auto_mode else 'AUTO OFF', self.ui_manager, container=self.info_panel)
def __init__(self): pygame.init() self.size = [INTRO_SCREEN_WIDTH, INTRO_SCREEN_HEIGHT] self.screen = pygame.display.set_mode(self.size) self.con_win = None self.game_level = 4 self.game = None self.start_game = False self.game_nb_players = 4 # TODO The max number of players should be in function of the size of the desk self.game_nb_online_players = 1 self.nb_p_c = 1 # self.block_list self.player_name = "John" # self.player_name = "IA Show" self.player_type = "offline" self.host_address = "localhost" self.survival_mode = True self.all_sprites_list = pygame.sprite.Group() self.block_list = pygame.sprite.Group() self.clicked = False self.ui_manager = pygame_gui.UIManager( (INTRO_SCREEN_WIDTH, INTRO_SCREEN_HEIGHT)) self.ui_manager.preload_fonts([{ 'name': 'fira_code', 'point_size': 10, 'style': 'bold' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'regular' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'bold' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'bold_italic' }]) step = 40 s = [150, 35] self.name_label = UILabel(pygame.rect.Rect((0, step), s), "Name", manager=self.ui_manager) self.name_value = UITextEntryLine( pygame.Rect((self.name_label.relative_rect.width, self.name_label.relative_rect.y), (200, -1)), self.ui_manager) self.name_value.set_text(self.player_name) # The level self.game_level_label = UILabel(pygame.rect.Rect((0, step * 2), s), "Level", manager=self.ui_manager) self.game_level_drop = UIDropDownMenu( [str(i) for i in range(1, 5, 1)], f"{self.game_level}", pygame.rect.Rect(((s[0] - step) // 2, step * 3), (50, 35)), self.ui_manager) self.nb_players_label = UILabel(pygame.rect.Rect( (s[0], self.game_level_label.relative_rect.y), s), "NB players", manager=self.ui_manager) self.nb_players_drop = UIDropDownMenu([str(i) for i in range(2, 7, 1)], f"{self.game_nb_players}", pygame.Rect( (int(1.3 * s[0]), step * 3), (50, 35)), self.ui_manager) self.nb_p_c_label = UILabel(pygame.rect.Rect( (2 * s[0], self.game_level_label.relative_rect.y), s), "NB playing cards", manager=self.ui_manager) self.nb_p_c_players_drop = UIDropDownMenu( [str(i) for i in range(1, 5, 1)], f"{self.nb_p_c}", pygame.Rect((int(2.3 * s[0]), step * 3), (50, 35)), self.ui_manager) self.survival_label = UILabel(pygame.rect.Rect( (3 * s[0], self.game_level_label.relative_rect.y), s), "Survival mode", manager=self.ui_manager) self.survival_drop = UIDropDownMenu( ["yes", "no"], f"{'yes' if self.survival_mode else 'no'}", pygame.Rect((int(3.3 * s[0]), step * 3), (60, 35)), self.ui_manager) self.start_game_btn = UIButton( pygame.Rect((INTRO_SCREEN_WIDTH - 200, INTRO_SCREEN_HEIGHT - 50), (100, 40)), 'START', self.ui_manager) self.resume_game_btn = UIButton( pygame.Rect( (INTRO_SCREEN_WIDTH // 2 - 50, INTRO_SCREEN_HEIGHT - 50), (100, 40)), 'RESUME', self.ui_manager) self.quit_game_btn = UIButton( pygame.Rect((100, INTRO_SCREEN_HEIGHT - 50), (100, 40)), 'QUIT', self.ui_manager) UILabel(pygame.rect.Rect((10, int(step * 6.5 - s[1])), s), "On-line Settings", manager=self.ui_manager) self.online_panel = UIPanel(pygame.rect.Rect( (10, int(step * 6.5)), (INTRO_SCREEN_WIDTH - 20, step * 5)), starting_layer_height=4, manager=self.ui_manager) self.nb_online_players_label = UILabel(pygame.rect.Rect((s[0], 0), s), "Online players", manager=self.ui_manager, container=self.online_panel) self.nb_online_players_drop = UIDropDownMenu( [str(i) for i in range(1, 6, 1)], f"{self.game_nb_online_players}", pygame.Rect((int(1.3 * (s[0])), step), (55, 35)), self.ui_manager, container=self.online_panel) self.player_type_label = UILabel(pygame.rect.Rect((0, 0), s), "Player type", manager=self.ui_manager, container=self.online_panel) self.player_type_drop = UIDropDownMenu(["server", "client", "offline"], f"{self.player_type}", pygame.Rect( (int(0.17 * s[0]), step), (100, 35)), self.ui_manager, container=self.online_panel) self.host_address_label = UILabel(pygame.rect.Rect((s[0], 0), s), "Host address", manager=self.ui_manager, container=self.online_panel) self.host_address_entry = UITextEntryLine(pygame.Rect((s[0], step), (150, 35)), self.ui_manager, container=self.online_panel) self.host_address_entry.set_text(self.host_address) if self.player_type != "client": # self.host_address_label.is_enabled = False # self.host_address_entry.is_enabled = False self.game_level_drop.show() self.game_level_label.show() self.nb_players_drop.show() self.nb_players_label.show() self.nb_p_c_players_drop.show() self.nb_p_c_label.show() self.survival_drop.show() self.survival_label.show() self.host_address_entry.hide() self.host_address_label.hide() else: # self.host_address_label.is_enabled = True # self.host_address_entry.is_enabled = True self.game_level_drop.hide() self.game_level_label.hide() self.nb_players_drop.hide() self.nb_players_label.hide() self.nb_p_c_players_drop.hide() self.nb_p_c_label.hide() self.survival_drop.hide() self.survival_label.hide() self.host_address_label.show() self.host_address_entry.show() if self.player_type != "server": # self.nb_online_players_drop.is_enabled = False self.nb_online_players_drop.hide() self.nb_online_players_label.hide() else: # self.nb_online_players_drop.is_enabled = True self.nb_online_players_drop.show() self.nb_online_players_label.show()
class GuessPanel: ### ### STATIC ### def card_from_text(dropdown: UIDropDownMenu, items: Enum, cardType: CardType) -> Card: text = dropdown.selected_option for item in items: if text == item.pretty(): return Card(item, cardType) raise Exception("enum value not found " + text + " " + str(items)) manager: UIManager panel: UIPanel player: HumanPlayer on_end_turn: Callable _weapon_menu: UIDropDownMenu _character_menu: UIDropDownMenu _room_menu: UIDropDownMenu _guess_button: UIButton _room_menu_rect: pygame.Rect guess: Solution is_guess: bool def __init__(self, manager: UIManager, screen_width: int, screen_height: int, player: HumanPlayer, \ on_end_turn: Callable): self.manager = manager self.player = player self.on_end_turn = on_end_turn self._create_panel(manager, screen_width, screen_height) 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 make_label(self, text: str, y_offset): rect = pygame.Rect((0, y_offset), (self.width, 20)) UILabel(rect, text, self.manager, container=self.panel) def make_drop_down(self, enum: Enum, y_offset: int) -> UIDropDownMenu: items = list(map(lambda i: i.pretty(), enum)) rect = pygame.Rect((int((self.width - 200) / 2), y_offset), (200, 25)) if enum == Room: self._room_menu_rect = rect return UIDropDownMenu(items, items[0], rect, self.manager, container=self.panel) def show_guess(self): self.is_guess = True self._guess_button.set_text('Guess!') rect = self._room_menu_rect.copy() items = list(map(lambda i: i.pretty(), Room)) self._room_menu.kill() self._room_menu = UIDropDownMenu(items, self.player.room.pretty(), rect, self.manager, container=self.panel) #self._room_menu.rebuild() self._room_menu.disable() self._show() def show_accuse(self): self.is_guess = False self._guess_button.set_text('Make Accusation!') self._room_menu.enable() self._show() def _show(self): self.guess = Solution(None, None, None) self._update_weapon() self._update_character() self._update_room() self.panel.show() def process_events(self, event): if not self.panel.visible: return self.panel.process_event(event) if event.type != pygame.USEREVENT: return if event.user_type == pygame_gui.UI_DROP_DOWN_MENU_CHANGED: if event.ui_element == self._weapon_menu: self._update_weapon() elif event.ui_element == self._character_menu: self._update_character() elif event.ui_element == self._room_menu: self._update_room() if event.user_type == pygame_gui.UI_BUTTON_PRESSED and event.ui_element == self._guess_button: print("guessing " + str(self.guess)) self.panel.hide() if self.is_guess: self.player.make_guess(self.guess) else: self.player.accuse(self.guess) self.on_end_turn() def _update_weapon(self): self.guess.weapon = GuessPanel.card_from_text(self._weapon_menu, Weapon, CardType.WEAPON) def _update_character(self): self.guess.character = GuessPanel.card_from_text(self._character_menu, Character, CardType.CHARACTER) def _update_room(self): self.guess.room = GuessPanel.card_from_text(self._room_menu, Room, CardType.ROOM)
class StartTurnPanel: player: HumanPlayer panel: UIPanel player_roll: PlayerRoll guess_panel: GuessPanel manager: UIManager _roll_button: UIButton _guess_button: UIButton _accuse_button: UIButton _guess_rect: pygame.Rect def __init__(self, manager: UIManager, screen_width: int, screen_height: int, player_roll: PlayerRoll, \ guess_panel: GuessPanel, player: HumanPlayer): self.manager = manager self._create_panel(screen_width, screen_height) self.player_roll = player_roll self.guess_panel = guess_panel self.player = player 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 show(self): self._guess_button.kill() self._make_guess_button() if self.player.room is None: self._guess_button.disable() else: self._guess_button.enable() self.panel.show() def _make_guess_button(self): self._guess_button = UIButton(self._guess_rect.copy(), 'Guess', self.manager, container=self.panel) def hide(self): self.panel.hide() def process_events(self, event): if not self.panel.visible: return self.panel.process_event(event) if event.type != pygame.USEREVENT: return if event.user_type == pygame_gui.UI_BUTTON_PRESSED: self._button_press(event) def _button_press(self, event): if event.ui_element == self._roll_button: self.hide() self.player_roll.roll() elif event.ui_element == self._guess_button: self.hide() self.guess_panel.show_guess() elif event.ui_element == self._accuse_button: self.hide() self.guess_panel.show_accuse()
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, "Generate Random Map", self.gui, self.ui_elements['panel']) # map 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, "Map Size", self.gui, self.ui_elements['panel']) self.ui_elements['dd_map_size'] = UIDropDownMenu( ['64', '128', '256'], '64', dd_rect, self.gui, self.ui_elements['panel']) # Seed label_rect.y += get_param('element_height') + get_param( 'element_padding') ip_rect = Rect(0, 0, 180, get_param('element_height')) ip_rect.centerx = (panel_rect.w // 2) + 30 ip_rect.y = label_rect.y s_btn_rect = Rect(0, 0, 60, get_param('element_height')) s_btn_rect.x = ip_rect.right + get_param('element_padding') s_btn_rect.y = ip_rect.y self.ui_elements['seed_label'] = UILabel(label_rect, 'Map Seed', self.gui, self.ui_elements['panel']) self.ui_elements['input_seed'] = UITextEntryLine( ip_rect, self.gui, self.ui_elements['panel']) self.ui_elements['btn_rnd_seed'] = UIButton(s_btn_rect, 'Random', self.gui, self.ui_elements['panel']) # I want to add two sliders, 1 for mountains and 1 for water, these would be used to control the upper # and lower limits of the height mapping. h_sl_ops = (0, 100) h_sl_sel = 50 label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect = Rect(0, 0, 200, get_param('element_height')) h_sl_rect.centerx = (panel_rect.w // 2) + get_param('element_padding') + 30 h_sl_rect.y = label_rect.y self.ui_elements['hsl_grass'] = UILabel(label_rect, 'Grass', self.gui, self.ui_elements['panel']) self.ui_elements['hs_grass'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_water'] = UILabel(label_rect, 'Water', self.gui, self.ui_elements['panel']) self.ui_elements['hs_water'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_mountain'] = UILabel(label_rect, 'Mountain', self.gui, self.ui_elements['panel']) self.ui_elements['hs_mountain'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.centerx = panel_rect.w // 2 button_rect.y = label_rect.bottom + get_param('element_padding') self.ui_elements['btn_preview'] = UIButton(button_rect, "Generate Preview", self.gui, self.ui_elements['panel']) button_rect.w = 200 button_rect.y += get_param('element_height') + 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_next'] = UIButton(button_rect, "Next", self.gui, self.ui_elements['panel']) pv_rect = Rect(0, 0, 300, 300) pv_rect.centerx = panel_rect.w // 2 pv_rect.y = button_rect.bottom + get_param('element_padding') self.ui_elements['pv_image'] = UIImage(pv_rect, self.pv_img, self.gui, self.ui_elements['panel'])
class MatchPickPanel: panel: UIPanel player: HumanPlayer pick: Card on_end_turn: Callable manager: UIManager _weapon_button: UIButton _character_button: UIButton _room_button: UIButton _solution: Solution _weapon_button_rect: pygame.Rect _character_button_rect: pygame.Rect _room_button_rect: pygame.Rect def __init__(self, manager: UIManager, screen_width: int, screen_height: int, player: HumanPlayer, \ on_end_turn: Callable): self.manager = manager self.player = player self.on_end_turn = on_end_turn self._create_panel(manager, screen_width, screen_height) def _create_panel(self, manager: UIManager, 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, manager, element_id='match_pick_panel') UILabel(pygame.Rect((0, 10), (self.width, 20)), "Pick a card to show", manager, container=self.panel) y_offset = 10 + 10 self._weapon_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_weapon_button('') y_offset += 25 + 50 self._character_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_character_button('') y_offset += 25 + 50 self._room_button_rect = pygame.Rect((50, y_offset + 25), (200, 50)) self._make_room_button('') self.panel.hide() def _make_weapon_button(self, text): self._weapon_button = UIButton(self._weapon_button_rect.copy(), text, self.manager, container=self.panel) def _make_character_button(self, text): self._character_button = UIButton(self._character_button_rect.copy(), text, self.manager, container=self.panel) def _make_room_button(self, text): self._room_button = UIButton(self._room_button_rect.copy(), text, self.manager, container=self.panel) def show(self, turn_data: PickMatchTurn): solution = turn_data.match self._solution = solution self._weapon_button.kill() self._character_button.kill() self._room_button.kill() self._make_weapon_button(str(turn_data.guess.weapon)) self._make_character_button(str(turn_data.guess.character)) self._make_room_button(str(turn_data.guess.room)) if solution.weapon is None: self._weapon_button.disable() else: self._weapon_button.enable() if solution.character is None: self._character_button.disable() else: self._character_button.enable() if solution.room is None: self._room_button.disable() else: self._room_button.enable() self.panel.show() def process_events(self, event): if not self.panel.visible: return self.panel.process_event(event) if event.type != pygame.USEREVENT: return if event.user_type == pygame_gui.UI_BUTTON_PRESSED: done = False if event.ui_element == self._weapon_button: self.player.set_card_to_show(self._solution.weapon) done = True elif event.ui_element == self._character_button: self.player.set_card_to_show(self._solution.character) done = True elif event.ui_element == self._room_button: self.player.set_card_to_show(self._solution.room) done = True if done: self.panel.hide() self.on_end_turn()
class Game: def __init__(self, session): self.manager = UIManager((800, 600), 'themes/game.json') image = pygame.image.load('assets/background.png') self.background = UIImage(relative_rect=pygame.Rect(0, 0, 800, 600), manager=self.manager, image_surface=image) self.session = session self.allPlayers = pygame.sprite.Group() self.player = Perso(self, self.session) self.allPlayers.add(self.player) self.pressed = {} self.allEnemy = pygame.sprite.Group() self.addScore = 100 self.score = 0 self.level = Niveau(self, self.score) self.panelTop = UIPanel(relative_rect=pygame.Rect(0, 0, 800, 20), starting_layer_height=2, manager=self.manager, object_id="#panel-top") self.labelHealthTitle = UILabel(relative_rect=pygame.Rect( 0, 0, 50, 20), text="Vie :", manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelHealth = UILabel(relative_rect=pygame.Rect(30, 0, 50, 20), text=str(self.player.health), manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelAsset = UILabel(relative_rect=pygame.Rect( 800 - 100, 0, 50, 20), text=str(self.level.assetIsActive[2]), manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelScore = UILabel(relative_rect=pygame.Rect( 0, 600 - 50, 200, 50), text=str(self.score), manager=self.manager, object_id="#label-score") self.playerImage = UIImage(relative_rect=pygame.Rect( (self.player.rect.x, self.player.rect.y), (50, 50)), manager=self.manager, image_surface=self.player.image) self.panel = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelGameOver = UILabel(relative_rect=pygame.Rect( 400 - 100, 300 - 50, 200, 50), text="Game Over", manager=self.manager, object_id="#label-game-over", container=self.panel) self.buttonMenu = UIButton(relative_rect=pygame.Rect( 400 - 100, 300 + 50, 200, 50), text='Quitter', manager=self.manager, container=self.panel) self.buttonParty = UIButton(relative_rect=pygame.Rect( (400 - 100, 300 + 50 + 80), (200, 50)), text='Recommencer', manager=self.manager, container=self.panel) self.panelPause = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelPause = UILabel(relative_rect=pygame.Rect( 400 - 100, 300 - 50, 200, 50), text="Pause", manager=self.manager, object_id="#label-game-over", container=self.panelPause) self.buttonSave = UIButton(relative_rect=pygame.Rect( 400 - 100, 300 + 50, 200, 50), text='Sauvegarder et quitter', manager=self.manager, container=self.panelPause) self.buttonQuit = UIButton(relative_rect=pygame.Rect( (400 - 100, 300 + 50 + 80), (200, 50)), text='Quitter', manager=self.manager, container=self.panelPause) self.panelAsset = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelEquipment = UILabel(relative_rect=pygame.Rect( 400 - 100, 50, 200, 50), text="Pause", manager=self.manager, object_id="#label-game-over", container=self.panelAsset) self.button1 = UIButton(relative_rect=pygame.Rect( 400 - 100, 50 + 50, 200, 50), text="Double l'attaque 10C", manager=self.manager, container=self.panelAsset) self.button2 = UIButton(relative_rect=pygame.Rect( (400 - 100, 120 + 50), (200, 50)), text='3 Missiles 100C', manager=self.manager, container=self.panelAsset) self.button3 = UIButton(relative_rect=pygame.Rect( (400 - 100, 190 + 50), (200, 50)), text='Double Score 20C', manager=self.manager, container=self.panelAsset) self.button4 = UIButton(relative_rect=pygame.Rect( (400 - 100, 260 + 50), (200, 50)), text="Temps d'apparition 50C", manager=self.manager, container=self.panelAsset) self.labelCredit = UILabel(relative_rect=pygame.Rect( 400 - 100, 330 + 50, 200, 50), text=str(self.player.credit), manager=self.manager, object_id="#label-game-over", container=self.panelAsset) self.pause = True def menuGameOver(self): self.panel.set_dimensions((800, 600)) def setLabelHealth(self): self.labelHealth.set_text(str(self.player.health)) def setLabelScore(self): self.labelScore.set_text(str(self.score)) def setLabelAsset(self): self.labelAsset.set_text(str(self.level.assetIsActive[2])) def setLabelCredit(self): self.player.credit += 1 self.labelCredit.set_text(str(self.player.credit)) def getManager(self): return self.manager def updatePosPlayer(self): self.playerImage.set_relative_position( (self.player.rect.x, self.player.rect.y)) def getPause(self): return self.pause def getEvent(self, event): if not self.player.isDead(): if event.type == pygame.USEREVENT: if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.buttonQuit: pygame.quit() elif event.ui_element == self.buttonSave: self.session.setCredit(self.player.credit) self.session.setScore(self.score) pygame.quit() elif event.ui_element == self.button1: if self.player.credit >= 10 and self.level.assetIsActive[ 0] == False: self.player.credit -= 10 self.player.getSurprise("damage") self.panelAsset.set_dimensions((0, 0)) self.pause = True elif event.ui_element == self.button2: if self.player.credit >= 100 and self.level.assetIsActive[ 0] == False: self.player.credit -= 100 self.player.getSurprise("attack") self.panelAsset.set_dimensions((0, 0)) self.pause = True elif event.ui_element == self.button3: if self.player.credit >= 20 and self.level.assetIsActive[ 0] == False: self.player.credit -= 20 self.player.getSurprise("score") self.panelAsset.set_dimensions((0, 0)) self.pause = True elif event.ui_element == self.button4: if self.player.credit >= 50 and self.level.assetIsActive[ 0] == False: self.player.credit -= 50 self.player.getSurprise("reload") self.panelAsset.set_dimensions((0, 0)) self.pause = True if event.type == KEYDOWN: if event.key == pygame.K_ESCAPE: if self.pause: self.panelPause.set_dimensions((800, 600)) self.pause = False else: self.panelPause.set_dimensions((0, 0)) self.pause = True return self.pause if event.key == pygame.K_RETURN: if self.pause: self.panelAsset.set_dimensions((800, 600)) self.pause = False else: self.panelAsset.set_dimensions((0, 0)) self.pause = True return self.pause if event.key == pygame.K_SPACE: self.player.attack(self.manager) self.pressed[event.key] = True elif event.type == KEYUP: self.pressed[event.key] = False return None else: if event.type == pygame.USEREVENT: if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.buttonParty: return "restart" elif event.ui_element == self.buttonMenu: pygame.quit() def updatePosEnemy(self, enemy, x, y): enemy.ennemyImage.set_relative_position((x, y)) def spawnEnemy(self, health, x, asset): enemy = Enemy(health, self.manager, x, self, asset) self.updatePosEnemy(enemy, enemy.rect.x, enemy.rect.y) self.allEnemy.add(enemy) def checkColision(self, sprite, group): return pygame.sprite.spritecollide(sprite, group, False, pygame.sprite.collide_mask) def run(self): self.level.generate() self.level.setScore(self.score)
class UrizenGuiApp: def __init__(self): pygame.init() pygame.display.set_caption('Urizen 0.2.5') self.opt = GUIOptions() self.gen_state = GeneratorsState() if self.opt.fullscreen: self.window_surface = pygame.display.set_mode( self.opt.resolution, pygame.FULLSCREEN) else: self.window_surface = pygame.display.set_mode( self.opt.resolution, pygame.RESIZABLE) self.background_surface = None self.ui_manager = UIManager( self.opt.resolution, PackageResource('urizen.data.themes', 'gui_theme.json')) self.ui_manager.preload_fonts([{ 'name': 'fira_code', 'point_size': 10, 'style': 'bold' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'regular' }, { 'name': 'fira_code', 'point_size': 10, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'italic' }, { 'name': 'fira_code', 'point_size': 14, 'style': 'bold' }]) self.panel = None self.message_window = None self.active_panel = None self.recreate_ui() self.clock = pygame.time.Clock() self.time_delta_stack = deque([]) self.button_response_timer = pygame.time.Clock() self.running = True 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 construct_gen_explore(self): #self.gen_explore_bg_surface = pygame.Surface(self.opt.resolution) #self.gen_explore_bg_surface.fill(self.ui_manager.get_theme().get_colour('dark_bg')) self.pnl_gen_explore = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.gen_explore_label = UILabel( pygame.Rect(5, 5, 350, 35), 'Explore generators', self.ui_manager, container=self.pnl_gen_explore, ) self.gen_list = UISelectionList( relative_rect=pygame.Rect(5, 75, 350, self.opt.H - 95), item_list=self.gen_state.get_current_gen_list(), manager=self.ui_manager, container=self.pnl_gen_explore, allow_multi_select=False, object_id='#gen_list', ) self.btn_gen_explore_save_as_png = None self.gen_explore_map_image = None self.gen_explore_image = None self.file_dialog_gen_explore_save_as_png = None self.pnl_gen_explore.hide() def construct_gen_search(self): self.pnl_gen_search = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.pnl_gen_search.hide() def construct_tiles_explore(self): self.pnl_tiles_explore = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.pnl_tiles_explore.hide() def construct_tiles_search(self): self.pnl_tiles_search = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.pnl_tiles_search.hide() def process_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.ui_manager.process_events(event) if event.type == pygame.VIDEORESIZE: self.opt.W = event.w self.opt.H = event.h self.opt.resolution = (event.w, event.h) self.recreate_ui() if event.type == pygame.USEREVENT: if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.btn_gen_explore: self.pnl_gen_explore.show() self.pnl_gen_search.hide() self.pnl_tiles_explore.hide() self.pnl_tiles_search.hide() self.active_panel = self.pnl_gen_explore elif event.ui_element == self.btn_gen_search: self.pnl_gen_explore.hide() self.pnl_gen_search.show() self.pnl_tiles_explore.hide() self.pnl_tiles_search.hide() self.active_panel = self.btn_gen_search elif event.ui_element == self.btn_tiles_explore: self.pnl_gen_explore.hide() self.pnl_gen_search.hide() self.pnl_tiles_explore.show() self.pnl_tiles_search.hide() self.active_panel = self.btn_tiles_explore elif event.ui_element == self.btn_tiles_search: self.pnl_gen_explore.hide() self.pnl_gen_search.hide() self.pnl_tiles_explore.hide() self.pnl_tiles_search.show() self.active_panel = self.pnl_tiles_search elif event.ui_element == self.btn_gen_explore_save_as_png: self.file_dialog_gen_explore_save_as_png = UIFileDialog( pygame.Rect(self.opt.W // 4, self.opt.H // 4, self.opt.W // 2, self.opt.H // 2), self.ui_manager, window_title='Save as PNG', initial_file_path='map.png', object_id='#file_dialog_gen_explore_save_as_png') if event.user_type == pygame_gui.UI_FILE_DIALOG_PATH_PICKED: if event.ui_element == self.file_dialog_gen_explore_save_as_png: self.gen_explore_image.save(event.text) if event.user_type == pygame_gui.UI_WINDOW_CLOSE: if event.ui_element == self.file_dialog_gen_explore_save_as_png: self.file_dialog_gen_explore_save_as_png = None if event.user_type == pygame_gui.UI_SELECTION_LIST_DOUBLE_CLICKED_SELECTION: if event.ui_element == self.gen_list: if self.gen_state.is_generator(event.text): M = self.gen_state.get_generator(event.text)() surface_w = self.opt.W - 435 surface_h = self.opt.H - 95 self.gen_explore_image = construct_bounded_map_image( M, surface_w, surface_h) image_bytes = self.gen_explore_image.tobytes() im_w, im_h = self.gen_explore_image.size shift_x = (surface_w - im_w) // 2 shift_y = (surface_h - im_h) // 2 if not self.gen_explore_map_image: self.gen_explore_map_image = UIImage( relative_rect=pygame.Rect( 360 + shift_x, 75 + shift_y, im_w, im_h), image_surface=pygame.image.fromstring( image_bytes, self.gen_explore_image.size, self.gen_explore_image.mode), manager=self.ui_manager, container=self.pnl_gen_explore, object_id='#gen_explore_map_image', ) else: self.gen_explore_map_image.set_relative_position( pygame.Rect(360 + shift_x, 75 + shift_y, im_w, im_h)) self.gen_explore_map_image.image = pygame.image.fromstring( image_bytes, self.gen_explore_image.size, self.gen_explore_image.mode) if not self.btn_gen_explore_save_as_png: self.btn_gen_explore_save_as_png = UIButton( pygame.Rect(self.opt.W - 265, 5, 190, 50), 'Save as PNG', manager=self.ui_manager, container=self.pnl_gen_explore, object_id='#btn_gen_explore_save_as_png', starting_height=10, ) else: self.gen_state.change_state(event.text) self.gen_list.set_item_list( self.gen_state.get_current_gen_list()) def run(self): while self.running: time_delta = self.clock.tick() / 1000.0 self.time_delta_stack.append(time_delta) if len(self.time_delta_stack) > 2000: self.time_delta_stack.popleft() # check for input self.process_events() # respond to input self.ui_manager.update(time_delta) # draw graphics self.window_surface.blit(self.background_surface, (0, 0)) self.ui_manager.draw_ui(self.window_surface) pygame.display.update()
def construct_tiles_search(self): self.pnl_tiles_search = UIPanel(self.main_area, starting_layer_height=0, manager=self.ui_manager) self.pnl_tiles_search.hide()
def __init__(self, session): self.manager = UIManager((800, 600), 'themes/game.json') image = pygame.image.load('assets/background.png') self.background = UIImage(relative_rect=pygame.Rect(0, 0, 800, 600), manager=self.manager, image_surface=image) self.session = session self.allPlayers = pygame.sprite.Group() self.player = Perso(self, self.session) self.allPlayers.add(self.player) self.pressed = {} self.allEnemy = pygame.sprite.Group() self.addScore = 100 self.score = 0 self.level = Niveau(self, self.score) self.panelTop = UIPanel(relative_rect=pygame.Rect(0, 0, 800, 20), starting_layer_height=2, manager=self.manager, object_id="#panel-top") self.labelHealthTitle = UILabel(relative_rect=pygame.Rect( 0, 0, 50, 20), text="Vie :", manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelHealth = UILabel(relative_rect=pygame.Rect(30, 0, 50, 20), text=str(self.player.health), manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelAsset = UILabel(relative_rect=pygame.Rect( 800 - 100, 0, 50, 20), text=str(self.level.assetIsActive[2]), manager=self.manager, object_id="#label-score", container=self.panelTop) self.labelScore = UILabel(relative_rect=pygame.Rect( 0, 600 - 50, 200, 50), text=str(self.score), manager=self.manager, object_id="#label-score") self.playerImage = UIImage(relative_rect=pygame.Rect( (self.player.rect.x, self.player.rect.y), (50, 50)), manager=self.manager, image_surface=self.player.image) self.panel = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelGameOver = UILabel(relative_rect=pygame.Rect( 400 - 100, 300 - 50, 200, 50), text="Game Over", manager=self.manager, object_id="#label-game-over", container=self.panel) self.buttonMenu = UIButton(relative_rect=pygame.Rect( 400 - 100, 300 + 50, 200, 50), text='Quitter', manager=self.manager, container=self.panel) self.buttonParty = UIButton(relative_rect=pygame.Rect( (400 - 100, 300 + 50 + 80), (200, 50)), text='Recommencer', manager=self.manager, container=self.panel) self.panelPause = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelPause = UILabel(relative_rect=pygame.Rect( 400 - 100, 300 - 50, 200, 50), text="Pause", manager=self.manager, object_id="#label-game-over", container=self.panelPause) self.buttonSave = UIButton(relative_rect=pygame.Rect( 400 - 100, 300 + 50, 200, 50), text='Sauvegarder et quitter', manager=self.manager, container=self.panelPause) self.buttonQuit = UIButton(relative_rect=pygame.Rect( (400 - 100, 300 + 50 + 80), (200, 50)), text='Quitter', manager=self.manager, container=self.panelPause) self.panelAsset = UIPanel(relative_rect=pygame.Rect((0, 0), (0, 0)), starting_layer_height=3, manager=self.manager, object_id="#panel-game-over") self.labelEquipment = UILabel(relative_rect=pygame.Rect( 400 - 100, 50, 200, 50), text="Pause", manager=self.manager, object_id="#label-game-over", container=self.panelAsset) self.button1 = UIButton(relative_rect=pygame.Rect( 400 - 100, 50 + 50, 200, 50), text="Double l'attaque 10C", manager=self.manager, container=self.panelAsset) self.button2 = UIButton(relative_rect=pygame.Rect( (400 - 100, 120 + 50), (200, 50)), text='3 Missiles 100C', manager=self.manager, container=self.panelAsset) self.button3 = UIButton(relative_rect=pygame.Rect( (400 - 100, 190 + 50), (200, 50)), text='Double Score 20C', manager=self.manager, container=self.panelAsset) self.button4 = UIButton(relative_rect=pygame.Rect( (400 - 100, 260 + 50), (200, 50)), text="Temps d'apparition 50C", manager=self.manager, container=self.panelAsset) self.labelCredit = UILabel(relative_rect=pygame.Rect( 400 - 100, 330 + 50, 200, 50), text=str(self.player.credit), manager=self.manager, object_id="#label-game-over", container=self.panelAsset) self.pause = True