Ejemplo n.º 1
0
def main():
    pygame.init()
    os.environ['SDL_VIDEO_CENTERED'] = '1'
    pygame.key.set_repeat()
    x_screen_size = 1024
    y_screen_size = 600
    pygame.display.set_caption('Turret Warfare')
    screen = pygame.display.set_mode((x_screen_size, y_screen_size))

    screen_data = ScreenData([x_screen_size, 128], [x_screen_size, 184], [x_screen_size, y_screen_size])

    ui_manager = UIManager(screen.get_size(), "data/ui_theme.json")
    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': 14, 'style': 'bold'}])

    app_state_manager = AppStateManager()
    MainMenu(ui_manager, app_state_manager)
    SelectLevelMenu(ui_manager, app_state_manager)
    GameState(ui_manager, screen, screen_data, app_state_manager)
    EditorState(ui_manager, screen, screen_data, app_state_manager)
    QuitState(app_state_manager)
    app_state_manager.set_initial_state('main_menu')

    clock = pygame.time.Clock()
    running = True

    while running:
        frame_time = clock.tick(60)
        time_delta = min(frame_time/1000.0, 0.1)

        running = app_state_manager.run(screen, time_delta)

        pygame.display.flip()  # flip all our drawn stuff onto the screen

    pygame.quit()  # exited game loop so quit pygame
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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()