class OptionsUIApp: def __init__(self): pygame.init() pygame.display.set_caption("Options UI") self.options = Options() if self.options.fullscreen: self.window_surface = pygame.display.set_mode( self.options.resolution, pygame.FULLSCREEN) else: self.window_surface = pygame.display.set_mode( self.options.resolution) self.background_surface = None self.ui_manager = UIManager(self.options.resolution, 'data/themes/theme_2.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.test_button = None self.test_button_2 = None self.test_button_3 = None self.test_slider = None self.test_text_entry = None self.test_drop_down = None self.panel = None self.message_window = None self.recreate_ui() self.clock = pygame.time.Clock() self.button_response_timer = pygame.time.Clock() self.running = True self.debug_mode = False 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) def create_message_window(self): self.button_response_timer.tick() self.message_window = UIMessageWindow( rect=pygame.Rect( (random.randint(0, self.options.resolution[0] - 300), random.randint(0, self.options.resolution[1] - 200)), (300, 250)), window_title='Test Message Window', html_message='<font color=normal_text>' 'This is a <a href="test">test</a> message to see if ' 'this box <a href=actually_link>actually</a> works.' '' 'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br><br> ' 'In hac a habitasse to platea dictumst.<br>' ' <font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec ' 'porttitor.<br> Morbi' ' accumsan, lectus at' ' tincidunt to dictum, neque <font color=#879AF6>erat tristique' ' blob</font>,' ' sed a tempus for <b>nunc</b> dolor in nibh.<br>' ' Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, sit amet ' 'on pharetra a ante' ' sollicitudin.</font>' '<br><br>' 'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br><br> ' 'In hac a habitasse to platea dictumst.<br>' ' <font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec ' 'porttitor.<br> Morbi' ' accumsan, lectus at' ' tincidunt to dictum, neque <font color=#879AF6>erat tristique ' 'erat</font>,' ' sed a tempus for <b>nunc</b> dolor in nibh.<br>' ' Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, sit amet ' 'on pharetra a ante' ' sollicitudin.</font>' '<br><br>' 'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br><br> ' 'In hac a habitasse to platea dictumst.<br>' ' <font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec ' 'porttitor.<br> Morbi' ' accumsan, lectus at' ' tincidunt to dictum, neque <font color=#879AF6>erat tristique ' 'erat</font>,' ' sed a tempus for <b>nunc</b> dolor in nibh.<br>' ' Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, ' 'sit amet on pharetra a ante' ' sollicitudin.</font>' '</font>', manager=self.ui_manager) time_taken = self.button_response_timer.tick() / 1000.0 # currently taking about 0.35 seconds down from 0.55 to create # an elaborately themed message window. # still feels a little slow but it's better than it was. print("Time taken to create message window: " + str(time_taken)) def check_resolution_changed(self): resolution_string = self.test_drop_down.selected_option.split('x') resolution_width = int(resolution_string[0]) resolution_height = int(resolution_string[1]) if (resolution_width != self.options.resolution[0] or resolution_height != self.options.resolution[1]): self.options.resolution = (resolution_width, resolution_height) self.window_surface = pygame.display.set_mode( self.options.resolution) self.recreate_ui() 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.KEYDOWN and event.key == pygame.K_d: self.debug_mode = False if self.debug_mode else True self.ui_manager.set_visual_debug_mode(self.debug_mode) if event.type == pygame.USEREVENT: if (event.user_type == pygame_gui.UI_TEXT_ENTRY_FINISHED and event.ui_object_id == '#main_text_entry'): print(event.text) if event.user_type == pygame_gui.UI_TEXT_BOX_LINK_CLICKED: if event.link_target == 'test': print("clicked test link") elif event.link_target == 'actually_link': print("clicked actually link") if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.test_button: self.test_button.set_text( random.choice( ['', 'Hover me!', 'Click this.', 'A Button'])) self.create_message_window() if event.ui_element == self.test_button_3: ScalingWindow(pygame.Rect((50, 50), (224, 224)), self.ui_manager) if event.ui_element == self.test_button_2: EverythingWindow(pygame.Rect((10, 10), (640, 480)), self.ui_manager) if (event.user_type == pygame_gui.UI_DROP_DOWN_MENU_CHANGED and event.ui_element == self.test_drop_down): self.check_resolution_changed() def run(self): while self.running: time_delta = self.clock.tick(60) / 1000.0 # 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()
class App(): def __init__(self): pygame.init() pygame.display.set_caption("MineSweaper") self.iconImage = pygame.image.load('data/images/mine.png') pygame.display.set_icon(self.iconImage) self.options = Options() if self.options.fullscreen: self.window_surface = pygame.display.set_mode( self.options.resolution, pygame.FULLSCREEN) else: self.window_surface = pygame.display.set_mode( self.options.resolution) self.background_surface = None self.ui_manager = UIManager(self.options.resolution, 'data/theme/mineSweaper_theme.json') self.clock = pygame.time.Clock() self.running = True ''' Create game Window ''' self.mainMenu = None self.recreate_ui() def process_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.ui_manager.process_events(event) 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('dark_bg')) self.mainMenu = MainMenu( pygame.Rect((25, 25), (self.options.resolution[0] - 250, self.options.resolution[1] - 50)), self.ui_manager) def run(self): while self.running: time_delta = self.clock.tick() / 1000.0 # 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()
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()
class Game: def __init__(self): # Start up Pygame. pygame.init() # Title the window our game runs in. pygame.display.set_caption(config.game_name) self.options = Options() # Define the dimensions of our game's window. self.window_surface = pygame.display.set_mode(self.options.resolution) self.window_surface.blit(pygame.image.load("media/images/background.png"), (0, 0)) self.start_up = True self.enter_mines = False self.enter_busstop = False self.enter_uptown = False self.background_surface = None self.ui_manager = UIManager(self.options.resolution, PackageResource(package='media.themes', resource='theme.json')) self.text_block = None self.text_entry = None self.message_window = None self.recreate_ui() self.clock = pygame.time.Clock() self.time_delta_stack = deque([]) self.running = True 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) if self.start_up == True: self.image = UIImage(pygame.Rect((0, 0), self.options.resolution), title_screen_image, self.ui_manager) elif self.enter_mines == True: self.image = UIImage(pygame.Rect((0, 0), self.options.resolution), mines_image, self.ui_manager) elif self.enter_busstop == True: self.image = UIImage(pygame.Rect((0, 0), self.options.resolution), busstop_image, self.ui_manager) elif self.enter_uptown == True: self.image = UIImage(pygame.Rect((0, 0), self.options.resolution), uptown_image, self.ui_manager) else: self.background_surface.fill(self.ui_manager.get_theme().get_colour('dark_bg')) self.background_surface.blit(pygame.image.load("media/images/background.png"), (0, 0)) global response_history response = response_history response_history = response self.text_entry = UITextEntryLine(pygame.Rect((50, 550), (700, 50)), self.ui_manager, object_id = "#text_entry") self.text_block = UITextBox(response, pygame.Rect((50, 25), (700, 500)), self.ui_manager, object_id = "#text_block") 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.KEYDOWN: if event.key== pygame.K_UP: if self.start_up == True: self.start_up = False self.recreate_ui() if self.enter_mines == True: self.enter_mines = False self.recreate_ui() if self.enter_busstop == True: self.enter_busstop = False self.recreate_ui() if self.enter_uptown == True: self.enter_uptown = False self.recreate_ui() if event.type == pygame.USEREVENT: if (event.user_type == pygame_gui.UI_TEXT_ENTRY_FINISHED and event.ui_object_id == '#text_entry'): if event.text != "": # Turn the user's input into a string. message = event.text # Begin the response message. Start with repeating the user's input. response = "<b><i>> {message}</i></b>".format(message = message) # Create a player object using the player's save data. player_data = player.Player() # Check to see if the player is in a scripted sequence. if player_data.scene is not None: # If they are... if player_data.scene == config.scene_id_newgame: # If the player has started the game for the first time. command_response = scenes.Introduction(message) response += ("<br>" * 4) + command_response else: # If they aren't... command_response = utilities.parse_message(message) response += ("<br>" * 4) + command_response # End the response with some decoration. ^_^ response += ("<br>" * 4) + ("-" * 20) + ("<br>" * 4) # Add this response to our response history, and then send the entire history to be rendered. global response_history response += response_history response_history = response # Render the response. self.text_entry.set_text("") self.text_block.kill() self.text_block = UITextBox(response, pygame.Rect((50, 25), (700, 500)), self.ui_manager, object_id = "#text_block") if command_response == "You enter the Mines.": self.enter_mines = True self.recreate_ui() if command_response == "You enter a Bus Stop.": self.enter_busstop = True self.recreate_ui() if command_response == "You enter Uptown.": self.enter_uptown = True self.recreate_ui() def run(self): while self.running: time_delta = self.clock.tick() / 1000 # Check for inputs from the player. self.process_events() # Respond to inputs. self.ui_manager.update(time_delta) # Draw the graphics. self.window_surface.blit(self.background_surface, (0, 0)) self.ui_manager.draw_ui(self.window_surface) pygame.display.update()