コード例 #1
0
    def load_images(self):
        """
        Loads all images in our loaded theme.

        """
        for element_key in self.ui_element_image_paths:
            image_paths_dict = self.ui_element_image_paths[element_key]
            if element_key not in self.ui_element_image_surfaces:
                self.ui_element_image_surfaces[element_key] = {}
            for path_key in image_paths_dict:
                image_path_data = image_paths_dict[path_key]
                if image_path_data['changed']:
                    path = image_path_data['path']
                    image = None
                    if path in self.loaded_image_files:
                        image = self.loaded_image_files[path]
                    else:
                        try:
                            image = pygame.image.load(create_resource_path(path)).convert_alpha()
                            self.loaded_image_files[path] = image
                        except pygame.error:
                            warnings.warn('Unable to load image at path: ' +
                                          str(create_resource_path(path)))

                    if image is not None:
                        if 'sub_surface_rect' in image_path_data:
                            surface = image.subsurface(image_path_data['sub_surface_rect'])
                        else:
                            surface = image
                        self.ui_element_image_surfaces[element_key][path_key] = surface
コード例 #2
0
    def _load_single_font_style(self, font_path: str, font_id: str,
                                font_size: int, font_style: Dict[str, bool]):
        """
        Load a single font file with a given style.

        :param font_path: Path to the font file.
        :param font_id: id for the font in the loaded fonts dictionary.
        :param font_size: pygame font size.
        :param font_style: style dictionary (italic, bold, both or neither)

        """
        try:
            if isinstance(font_path, bytes):
                file_loc = io.BytesIO(base64.standard_b64decode(font_path))
            else:
                file_loc = create_resource_path(font_path)
            new_font = pygame.font.Font(file_loc, font_size)
            new_font.set_bold(font_style['bold'])
            new_font.set_italic(font_style['italic'])
            self.loaded_fonts[font_id] = new_font
        except (FileNotFoundError, OSError):
            warnings.warn("Failed to load font at path: " + font_path)
コード例 #3
0
    def load_theme(self, file_path: Union[str, PathLike, io.StringIO,
                                          PackageResource]):
        """
        Loads a theme file, and currently, all associated data like fonts and images required
        by the theme.

        :param file_path: The path to the theme we want to load.
        """
        if isinstance(file_path, PackageResource):
            if USE_IMPORT_LIB_RESOURCE:
                used_file_path = io.StringIO(
                    read_text(file_path.package, file_path.resource))
                self._theme_file_path = file_path
                with path(file_path.package,
                          file_path.resource) as package_file_path:
                    self._theme_file_last_modified = os.stat(
                        package_file_path).st_mtime
            elif USE_FILE_PATH:
                used_file_path = file_path.to_path()

        elif not isinstance(file_path, io.StringIO):
            self._theme_file_path = create_resource_path(file_path)
            try:
                self._theme_file_last_modified = os.stat(
                    self._theme_file_path).st_mtime
            except (pygame.error, FileNotFoundError, OSError):
                self._theme_file_last_modified = 0
            used_file_path = self._theme_file_path
        else:
            used_file_path = file_path

        with self._opened_w_error(used_file_path, 'r') as (theme_file, error):
            if error:
                warnings.warn("Failed to open theme file at path:" +
                              str(file_path))
                load_success = False
            else:
                try:
                    theme_dict = json.load(theme_file,
                                           object_pairs_hook=OrderedDict)
                except json.decoder.JSONDecodeError:
                    warnings.warn(
                        "Failed to load current theme file, check syntax",
                        UserWarning)
                    load_success = False
                else:
                    load_success = True

                if load_success:

                    for element_name in theme_dict.keys():
                        if element_name == 'defaults':
                            self._load_colour_defaults_from_theme(theme_dict)
                        else:
                            self._load_prototype(element_name, theme_dict)
                            for data_type in theme_dict[element_name]:
                                if data_type == 'font':
                                    self._load_element_font_data_from_theme(
                                        data_type, element_name, theme_dict)

                                if data_type == 'colours':
                                    self._load_element_colour_data_from_theme(
                                        data_type, element_name, theme_dict)

                                elif data_type == 'images':
                                    self._load_element_image_data_from_theme(
                                        data_type, element_name, theme_dict)

                                elif data_type == 'misc':
                                    self._load_element_misc_data_from_theme(
                                        data_type, element_name, theme_dict)

        if load_success:
            self._load_fonts(
            )  # save to trigger load with the same data as it won't do anything
            self._load_images()
            self._preload_shadow_edges()
コード例 #4
0
    def load_theme(self, file_path: Union[str, PathLike, io.StringIO]):
        """
        Loads a theme file, and currently, all associated data like fonts and images required
        by the theme.

        :param file_path: The path to the theme we want to load.

        """

        if not isinstance(file_path, io.StringIO):
            self._theme_file_path = create_resource_path(file_path)
            try:
                self._theme_file_last_modified = stat(
                    self._theme_file_path).st_mtime
            except FileNotFoundError:
                self._theme_file_last_modified = 0
            used_file_path = self._theme_file_path
        else:
            used_file_path = file_path

        with self._opened_w_error(used_file_path, 'r') as (theme_file, error):
            if error:
                warnings.warn("Failed to open theme file at path:" +
                              str(file_path))
                load_success = False
            else:
                try:
                    load_success = True
                    theme_dict = json.load(theme_file,
                                           object_pairs_hook=OrderedDict)
                except json.decoder.JSONDecodeError:
                    warnings.warn(
                        "Failed to load current theme file, check syntax",
                        UserWarning)
                    load_success = False

                if load_success:

                    for element_name in theme_dict.keys():
                        if element_name == 'defaults':
                            self._load_colour_defaults_from_theme(theme_dict)
                        else:
                            self._load_prototype(element_name, theme_dict)
                            for data_type in theme_dict[element_name]:
                                if data_type == 'font':
                                    self._load_element_font_data_from_theme(
                                        data_type, element_name, theme_dict)

                                if data_type == 'colours':
                                    self._load_element_colour_data_from_theme(
                                        data_type, element_name, theme_dict)

                                elif data_type == 'images':
                                    self._load_element_image_data_from_theme(
                                        data_type, element_name, theme_dict)

                                elif data_type == 'misc':
                                    self._load_element_misc_data_from_theme(
                                        data_type, element_name, theme_dict)

        # TODO: these should be triggered at an appropriate time in our project when
        #  lots of files are being loaded
        if load_success:
            self._load_fonts(
            )  # save to trigger load with the same data as it won't do anything
            self._load_images()
            self._preload_shadow_edges()
コード例 #5
0
    def Main(cls):
        game_running = False
        engine_running = True
        speed_x = 5
        speed_y = 5
        player_pos_x = (MiPiGUI.g_screen_x * MiPiSettings.playerx_to_editor)
        player_pos_y = (MiPiGUI.g_screen_y * MiPiSettings.playery_to_editor)
        npc_pos_x = (MiPiGUI.g_screen_x * MiPiSettings.npcx_to_editor)
        npc_pos_y = (MiPiGUI.g_screen_y * MiPiSettings.npcy_to_editor)
        MiPiSettings.enemy_x = 800 / 2
        MiPiSettings.enemy_y = 600 / 2
        e_moverandx = 0
        e_moverandy = 0
        editableContent = False
        edMouseX = 0
        edMouseY = 0
        inputx = 0
        inputy = 0
        m_forwards = 'moving forwards'
        m_backwards = 'moving backwards'
        m_up = 'moving up'
        m_down = 'moving down'
        currentposition = ''
        Render.Shapes.CreateTriangle()
        MiPiPhysics.Player.BoxCollider2D(enginescreen, Render.GREEN, 30, 30,
                                         60, 60)

        while engine_running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    engine_running = False

                enginescreen.fill(Render.LIGHTCOLOR)
                MiPiGUI.GUI.EditorScreen()
                MiPiGUI.GUI.MiPiBorders(enginescreen, Render.DARKSHADE, 45,
                                        176, 385, 292, 15)
                enginescreen.blit(MiPiGUI.editorlabel, MiPiGUI.editorborder)
                enginescreen.blit(MiPiGUI.engineborder_north,
                                  MiPiGUI.set_engineborder_north)
                enginescreen.blit(MiPiGUI.engineborder_south,
                                  MiPiGUI.set_engineborder_south)

                # edMouseX, edMouseY = pygame.mouse.get_pos()
                # MiPiGUI Labels

                MiPi.EditSprite()

                # if event.type == pygame.MOUSEMOTION:
                # print("X: %d, Y: %d" % (edMouseX, edMouseY))

                if event.type == pygame.USEREVENT:
                    if event.user_type == pygame_gui.UI_BUTTON_START_PRESS:
                        if event.ui_element == MiPiGUI.play_button:
                            if not MiPiSettings.editor_has_content:
                                print(current_date, editor_error)
                                logging.warning(current_date, editor_error)
                            else:
                                print(
                                    current_date,
                                    "Gameplay test window has been launched!")
                                engine_running = False
                                game_running = True

                if event.type == pygame.USEREVENT:
                    if event.user_type == pygame_gui.UI_DROP_DOWN_MENU_CHANGED:
                        if event.text == "Run":
                            print(current_date,
                                  "Gamescreen window has been launched!")
                            engine_running = False
                            game_running = True
                        if event.text == "Close":
                            print(current_date, "Application has been closed")
                            engine_running = False
                            game_running = False
                        if event.text == "Import Sprite":
                            MiPiGUI.spritesubmenu.show()

                        # ------------------------ Import Sprite --------------------------------------------------#
                        if event.text == "Player":
                            if platform.system() == MiPiSettings.Windows:
                                MiPiGUI.playermenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a player sprite",
                                    initial_file_path="C:/",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.playersprite = True
                                MiPiSettings.editablePlayerSprite = True
                                MiPiSettings.editableNPCSprite = False
                                MiPi.EraseUI()
                                MiPiGUI.spritesubmenu.hide()
                            elif platform.system() == MiPiSettings.Mac:
                                MiPiGUI.playermenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a player sprite",
                                    initial_file_path="~/Desktop",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.playersprite = True
                                MiPiSettings.editablePlayerSprite = True
                                MiPiSettings.editableNPCSprite = False
                                MiPiGUI.spritesubmenu.hide()
                            elif platform.system() == MiPiSettings.Linux:
                                MiPiGUI.playermenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a player sprite",
                                    initial_file_path="/bin",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.playersprite = True
                                MiPiSettings.editablePlayerSprite = True
                                MiPiSettings.editableNPCSprite = False
                                MiPiGUI.spritesubmenu.hide()
                            else:
                                print(current_date,
                                      MiPiSettings.platform_error)
                                logging.warning(current_date,
                                                MiPiSettings.platform_error)

                        if event.text == "NPC":
                            MiPiGUI.npcmenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                rect=pygame.Rect((200, 200), (260, 300)),
                                manager=MiPiGUI.mainframe,
                                window_title="Select an npc sprite",
                                initial_file_path="C:/",
                                allow_existing_files_only=True,
                                visible=True)

                            MiPi.UnderDev(MiPiSettings.underdevelopment)
                            MiPiSettings.npcsprite = True
                            MiPiSettings.editableNPCSprite = True
                            MiPiSettings.editablePlayerSprite = False
                            MiPi.EraseUI()
                            MiPiGUI.spritesubmenu.hide()
                        if event.text == "Object":
                            MiPi.UnderDev(MiPiSettings.underdevelopment)
                            MiPiGUI.spritesubmenu.hide()
                        # ------------------------ Import Sprite --------------------------------------------------#

                        # -------------- Import Tilemap -----------------------------------------------------------#

                        if event.text == "Import Tilemap":
                            print(MiPiSettings.underdevelopment)
                            if platform.system() == MiPiSettings.Windows:
                                MiPiGUI.tilemapmenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a tilemap",
                                    initial_file_path="C:/",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.tilemap_chosen = True
                                MiPiSettings.tilemap_ready = True
                            elif platform.system() == MiPiSettings.Mac:
                                MiPiGUI.tilemapmenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a tilemap",
                                    initial_file_path="~/Desktop",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.tilemap_chosen = True
                                MiPiSettings.tilemap_ready = True
                            elif platform.system() == MiPiSettings.Linux:
                                MiPiGUI.tilemapmenu = pygame_gui.windows.ui_file_dialog.UIFileDialog(
                                    rect=pygame.Rect((200, 200), (260, 300)),
                                    manager=MiPiGUI.mainframe,
                                    window_title="Select a tilemap",
                                    initial_file_path="/bin",
                                    allow_existing_files_only=True,
                                    visible=True)
                                MiPiSettings.tilemap_chosen = True
                                MiPiSettings.tilemap_ready = True
                            else:
                                print(current_date,
                                      MiPiSettings.platform_error)
                                logging.warning(current_date,
                                                MiPiSettings.platform_error)

                        # -------------- Import Tilemap -----------------------------------------------------------#

                if event.type == pygame.USEREVENT:
                    if event.user_type == pygame_gui.UI_FILE_DIALOG_PATH_PICKED:

                        # ----------- NPC Sprite Import --------------------------------------------------------------#
                        if MiPiSettings.npcsprite:
                            try:
                                if not MiPiSettings.playersprite_available:
                                    print(
                                        "Import  a player sprite first: <- Replace this with a message box!"
                                    )
                                else:
                                    MiPiSettings.npc_path = create_resource_path(
                                        event.text)
                                    MiPiSettings.npc_img = pygame.image.load(
                                        MiPiSettings.npc_path).convert_alpha()
                                    npc_rect = MiPiSettings.npc_img.get_rect()
                                    aspect_ratio = npc_rect.width / npc_rect.height
                                    scalability = False

                                    if npc_rect.width > MiPiGUI.max_image_display_dimensions[
                                            0]:
                                        npc_rect.width = MiPiGUI.max_image_display_dimensions[
                                            0]
                                        npc_rect.height = int(npc_rect.width /
                                                              aspect_ratio)
                                        scalability = True

                                    if npc_rect.height > MiPiGUI.max_image_display_dimensions[
                                            1]:
                                        npc_rect.height = MiPiGUI.max_image_display_dimensions[
                                            1]
                                        npc_rect.width = int(npc_rect.height *
                                                             aspect_ratio)
                                        scalability = True

                                    if scalability:
                                        MiPiSettings.npc_img = pygame.transform.smoothscale(
                                            MiPiSettings.npc_img,
                                            npc_rect.size)

                                    # Boundary values are subject to change based on image size and scaling!
                                    # Make these values editable in a gui by the user: x range: 80-400
                                    # y range: 215-425  Best default: 80,425

                                    npcinputx = 80
                                    npcinputy = 215
                                    npcx = npcinputx
                                    npcy = npcinputy

                                    # Get current slider values
                                    getnpcspritex = MiPiGUI.npcxlocbar.get_current_value(
                                    )
                                    getnpcspritey = MiPiGUI.npcylocbar.get_current_value(
                                    )
                                    MiPiGUI.npctextx = str(getnpcspritex)
                                    MiPiGUI.npctexty = str(getnpcspritey)
                                    MiPiGUI.npcxlocbar.update(delta_time)
                                    MiPiGUI.npcylocbar.update(delta_time)

                                    npcx = getnpcspritex
                                    npcy = getnpcspritey

                                    npc_rect.x = npcx
                                    npc_rect.y = npcy

                                    if npc_rect.x < 80 or npc_rect.x > 400:
                                        print(
                                            "Value out of range! Setting to default"
                                        )
                                        npc_rect.x = 80

                                    if npc_rect.y < 215 or npc_rect.y > 425:
                                        print(
                                            "Value out of range! Setting to default"
                                        )
                                        npc_rect.y = 425

                                    npc_rect.center = (npc_rect.x, npc_rect.y)

                                    # UI manager mainframe has now been created after pygame.init()
                                    # Thank you MyreMylar(pygame_gui) for your help in spotting this :)
                                    # Currently see line 14 of MiPiGUI file for pygame.init()
                                    MiPiSettings.import_npc_sprite = UIImage(
                                        relative_rect=npc_rect,
                                        image_surface=MiPiSettings.npc_img,
                                        manager=MiPiGUI.mainframe)

                                    MiPiSettings.npcx_to_editor = (npc_rect.x /
                                                                   400)
                                    MiPiSettings.npcy_to_editor = (npc_rect.y /
                                                                   425)
                                    npc_pos_x = (MiPiGUI.g_screen_x *
                                                 MiPiSettings.npcx_to_editor)
                                    npc_pos_y = (MiPiGUI.g_screen_y *
                                                 MiPiSettings.npcy_to_editor)
                                    npc_pos_x = npc_pos_x - MiPiSettings.offset_x
                                    npc_pos_y = npc_pos_y - MiPiSettings.offset_y

                                    MiPiSettings.npcsprite = False
                                    MiPiSettings.editor_has_content = True

                            except pygame.error:
                                pass
                        # --------------------------------------------------------------------------------------------#

                        if MiPiSettings.tilemap_chosen:
                            try:

                                MiPiSettings.tile_path = create_resource_path(
                                    event.text)
                                MiPiSettings.tilemap_img = pygame.image.load(
                                    MiPiSettings.tile_path).convert_alpha()
                                tilemap_rect = MiPiSettings.tilemap_img.get_rect(
                                )
                                scalability = False

                                if tilemap_rect.width >= MiPiGUI.max_image_display_dimensions[
                                        0]:
                                    tilemap_rect.width = MiPiGUI.edwidth
                                    tilemap_rect.height = MiPiGUI.edheight
                                    scalability = True

                                if tilemap_rect.height > MiPiGUI.max_image_display_dimensions[
                                        1]:
                                    tilemap_rect.height = MiPiGUI.tilemap_height
                                    tilemap_rect.width = MiPiGUI.tilemap_width
                                    scalability = True

                                if tilemap_rect.width <= MiPiGUI.max_image_display_dimensions[
                                        0]:
                                    tilemap_rect.width = MiPiGUI.tilemap_width
                                    tilemap_rect.height = MiPiGUI.tilemap_height
                                    scalability = True

                                if tilemap_rect.height < MiPiGUI.max_image_display_dimensions[
                                        1]:
                                    tilemap_rect.height = MiPiGUI.tilemap_height
                                    tilemap_rect.width = MiPiGUI.tilemap_width
                                    scalability = True

                                if scalability:
                                    MiPiSettings.tilemap_img = pygame.transform.smoothscale(
                                        MiPiSettings.tilemap_img,
                                        tilemap_rect.size)

                                tilemap_rect.x = 238
                                tilemap_rect.y = 321
                                tilemap_rect.center = (tilemap_rect.x,
                                                       tilemap_rect.y)

                                MiPiSettings.import_tilemap = UIImage(
                                    relative_rect=tilemap_rect,
                                    image_surface=MiPiSettings.tilemap_img,
                                    manager=MiPiGUI.mainframe)

                                MiPiSettings.tilemap_chosen = False
                                MiPiSettings.tilemap_available = True
                            except pygame.error:
                                pass

                        # ----------- Player Sprite Import -----------------------------------------------------------#
                        if MiPiSettings.playersprite:
                            try:
                                if not MiPiSettings.tilemap_available:
                                    print(
                                        "Please import a tilemap first! <- Replace this with a message box!"
                                    )
                                if MiPiSettings.playersprite_available:
                                    print(
                                        "Player sprite available. <- Replace this with a message box!"
                                    )
                                else:
                                    MiPiSettings.sprite_path = create_resource_path(
                                        event.text)
                                    MiPiSettings.player_img = pygame.image.load(
                                        MiPiSettings.sprite_path
                                    ).convert_alpha()
                                    sprite_rect = MiPiSettings.player_img.get_rect(
                                    )
                                    aspect_ratio = sprite_rect.width / sprite_rect.height
                                    scalability = False

                                    if sprite_rect.width > MiPiGUI.max_image_display_dimensions[
                                            0]:
                                        sprite_rect.width = MiPiGUI.max_image_display_dimensions[
                                            0]
                                        sprite_rect.height = int(
                                            sprite_rect.width / aspect_ratio)
                                        scalability = True

                                    if sprite_rect.height > MiPiGUI.max_image_display_dimensions[
                                            1]:
                                        sprite_rect.height = MiPiGUI.max_image_display_dimensions[
                                            1]
                                        sprite_rect.width = int(
                                            sprite_rect.height * aspect_ratio)
                                        scalability = True

                                    if scalability:
                                        MiPiSettings.player_img = pygame.transform.smoothscale(
                                            MiPiSettings.player_img,
                                            sprite_rect.size)

                                    # Removes any duplicate copies of player sprites in the editor
                                    if MiPiSettings.player_removeduplicates >= 1:
                                        MiPiSettings.player_img.fill(
                                            Render.TRANSPARENCY)
                                        MiPiSettings.player_removeduplicates = 0

                                    # Boundary values are subject to change based on image size and scaling!
                                    # Make these values editable in a gui by the user: x range: 80-400
                                    # y range: 215-425  Best default: 80,425

                                    inputx = 80
                                    inputy = 215
                                    x = inputx
                                    y = inputy

                                    # Get current slider values
                                    getspritex = MiPiGUI.playerxlocbar.get_current_value(
                                    )
                                    getspritey = MiPiGUI.playerylocbar.get_current_value(
                                    )
                                    MiPiGUI.textx = str(getspritex)
                                    MiPiGUI.texty = str(getspritey)
                                    MiPiGUI.playerxlocbar.update(delta_time)
                                    MiPiGUI.playerylocbar.update(delta_time)

                                    x = getspritex
                                    y = getspritey

                                    sprite_rect.x = x
                                    sprite_rect.y = y

                                    if sprite_rect.x < 80 or sprite_rect.x > 400:
                                        print(
                                            "Value out of range! Setting to default"
                                        )
                                        sprite_rect.x = 80

                                    if sprite_rect.y < 215 or sprite_rect.y > 425:
                                        print(
                                            "Value out of range! Setting to default"
                                        )
                                        sprite_rect.y = 425

                                    sprite_rect.center = (sprite_rect.x,
                                                          sprite_rect.y)

                                    # UI manager mainframe has now been created after pygame.init()
                                    # Thank you MyreMylar(pygame_gui) for your help in spotting this :)
                                    # Currently see line 14 of MiPiGUI file for pygame.init()
                                    MiPiSettings.import_player_sprite = UIImage(
                                        relative_rect=sprite_rect,
                                        image_surface=MiPiSettings.player_img,
                                        manager=MiPiGUI.mainframe)

                                    MiPiSettings.playerx_to_editor = (
                                        sprite_rect.x / 400)
                                    MiPiSettings.playery_to_editor = (
                                        sprite_rect.y / 425)
                                    player_pos_x = (
                                        MiPiGUI.g_screen_x *
                                        MiPiSettings.playerx_to_editor)
                                    player_pos_y = (
                                        MiPiGUI.g_screen_y *
                                        MiPiSettings.playery_to_editor)
                                    player_pos_x = player_pos_x - MiPiSettings.offset_x
                                    player_pos_y = player_pos_y - MiPiSettings.offset_y

                                    MiPiSettings.playersprite_available = True
                                    MiPiSettings.playersprite = False
                                    MiPiSettings.editor_has_content = True

                            except pygame.error:
                                pass
                        # --------------------------------------------------------------------------------------------#

                    # ------------------- UPDATE PLAYER BUTTON CONTROLS ---------------------------------------------------#
                    if event.user_type == pygame_gui.UI_BUTTON_START_PRESS:
                        if event.ui_element == MiPiGUI.updateplayer_button:
                            print(inprogress)
                            MiPiSettings.import_player_sprite.hide()

                    if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
                        if event.ui_element == MiPiGUI.updateplayer_button:
                            change_rect = MiPiSettings.player_img.get_rect()
                            movex = MiPiGUI.playerxlocbar.get_current_value()
                            movey = MiPiGUI.playerylocbar.get_current_value()
                            change_rect.x = movex
                            change_rect.y = movey
                            change_rect.center = (change_rect.x, change_rect.y)
                            MiPiSettings.import_player_sprite = UIImage(
                                relative_rect=change_rect,
                                image_surface=MiPiSettings.player_img,
                                manager=MiPiGUI.mainframe)

                            MiPiSettings.playerx_to_editor = (change_rect.x /
                                                              400)
                            MiPiSettings.playery_to_editor = (change_rect.y /
                                                              425)
                            player_pos_x = (MiPiGUI.g_screen_x *
                                            MiPiSettings.playerx_to_editor)
                            player_pos_y = (MiPiGUI.g_screen_y *
                                            MiPiSettings.playery_to_editor)
                            player_pos_x = player_pos_x - MiPiSettings.offset_x
                            player_pos_y = player_pos_y - MiPiSettings.offset_y

                            getspritex = MiPiGUI.playerxlocbar.get_current_value(
                            )
                            getspritey = MiPiGUI.playerylocbar.get_current_value(
                            )
                            MiPiGUI.textx = str(getspritex)
                            MiPiGUI.texty = str(getspritey)
                            MiPiGUI.playerxlocbar.update(delta_time)
                            MiPiGUI.playerylocbar.update(delta_time)

                    # ----------------------------------------------------------------------------------------------------#

                    # ------------------- UPDATE NPC BUTTON CONTROLS -----------------------------------------------------#

                    if event.user_type == pygame_gui.UI_BUTTON_START_PRESS:
                        if event.ui_element == MiPiGUI.updatenpc_button:
                            print(inprogress)
                            MiPiSettings.import_npc_sprite.hide()

                    if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
                        if event.ui_element == MiPiGUI.updatenpc_button:
                            change_rect_npc = MiPiSettings.npc_img.get_rect()
                            movenpcx = MiPiGUI.npcxlocbar.get_current_value()
                            movenpcy = MiPiGUI.npcylocbar.get_current_value()
                            change_rect_npc.x = movenpcx
                            change_rect_npc.y = movenpcy
                            change_rect_npc.center = (change_rect_npc.x,
                                                      change_rect_npc.y)
                            MiPiSettings.import_npc_sprite = UIImage(
                                relative_rect=change_rect_npc,
                                image_surface=MiPiSettings.npc_img,
                                manager=MiPiGUI.mainframe)

                            MiPiSettings.npcx_to_editor = (change_rect_npc.x /
                                                           400)
                            MiPiSettings.npcy_to_editor = (change_rect_npc.y /
                                                           425)
                            npc_pos_x = (MiPiGUI.g_screen_x *
                                         MiPiSettings.npcx_to_editor)
                            npc_pos_y = (MiPiGUI.g_screen_y *
                                         MiPiSettings.npcy_to_editor)
                            npc_pos_x = npc_pos_x - MiPiSettings.offset_x
                            npc_pos_y = npc_pos_y - MiPiSettings.offset_y

                            getnpcspritex = MiPiGUI.npcxlocbar.get_current_value(
                            )
                            getnpcspritey = MiPiGUI.npcylocbar.get_current_value(
                            )
                            MiPiGUI.npctextx = str(getnpcspritex)
                            MiPiGUI.npctexty = str(getnpcspritey)
                            MiPiGUI.npcxlocbar.update(delta_time)
                            MiPiGUI.npcylocbar.update(delta_time)

                # ----------------------------------------------------------------------------------------------------#

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_ESCAPE:
                        engine_running = False

                # Updates: Tried de-indenting MiPiGUI updates, but this caused instability in all
                # looped events including gui elements. Also tried setting updates in a method, but
                # this caused pygame.event to throw an error. So instead, I indented pygame display
                # updates and everything seems to still work correctly. Will keep an eye on updates.
                # I did notice that de-indenting update methods will put them outside the main engine's
                # while loop. For now, just gonna keep them in the while loop unless I can find a
                # workaround.

                MiPiGUI.mainframe.process_events(event)
                MiPiGUI.mainframe.draw_ui(enginescreen)
                MiPiGUI.mainframe.update(delta_time)
                pygame.display.flip()
                pygame.display.update()

        while game_running:
            for game in pygame.event.get():
                if game.type == pygame.QUIT:
                    pygame.display.set_caption(engine_title)
                    MiPi.Main()
                    game_running = False

                # The screen gets rendered a color to be tested regardless if a tilemap is ready to be tested.
                gamescreen.fill(Render.BLUE)
                # Collider box2D
                MiPi.DrawBoxCol(box_x, box_y)
                # Remove this later
                MiPi.ShowPlayerPos(player_pos_x, player_pos_y)

                # If the user has placed a tilemap and is ready to test with a tilemap, then blit it to the test screen
                if MiPiSettings.tilemap_ready:
                    gamescreen.blit(
                        pygame.transform.scale(
                            MiPiSettings.tilemap_img,
                            (MiPiGUI.g_screen_x, MiPiGUI.g_screen_y)), (0, 0))

                # Player movement boundaries
                if player_pos_x > MiPiGUI.g_screen_x - 1:
                    player_pos_x = 0
                elif player_pos_x < 0:
                    player_pos_x = MiPiGUI.g_screen_x

                if player_pos_y > MiPiGUI.g_screen_y - 1:
                    player_pos_y = 0
                elif player_pos_y < 0:
                    player_pos_y = MiPiGUI.g_screen_y

                # Enemy movement boundaries
                if MiPiSettings.enemy_x > MiPiGUI.g_screen_x - 1:
                    MiPiSettings.enemy_x = 0
                elif MiPiSettings.enemy_x < 0:
                    MiPiSettings.enemy_x = MiPiGUI.g_screen_x

                if MiPiSettings.enemy_y > MiPiGUI.g_screen_y - 1:
                    MiPiSettings.enemy_y = 0
                elif MiPiSettings.enemy_y < 0:
                    MiPiSettings.enemy_y = MiPiGUI.g_screen_y

                e_moverandx = random.randint(1, 3)
                e_moverandy = random.randint(4, 6)

                if 1 <= e_moverandx <= 2:
                    MiPiSettings.enemy_x -= MiPiSettings.enemy_speed
                elif e_moverandx == 3:
                    MiPiSettings.enemy_x += MiPiSettings.enemy_speed

                if 4 <= e_moverandy <= 5:
                    MiPiSettings.enemy_y -= MiPiSettings.enemy_speed
                elif e_moverandy == 6:
                    MiPiSettings.enemy_y += MiPiSettings.enemy_speed

                pygame.key.set_repeat(
                    1, 10
                )  # Not a friendly way of movement while key is held down, but works!
                # Movement loop
                if game.type == pygame.KEYDOWN:
                    if game.key == pygame.K_LEFT:
                        player_pos_x -= speed_x
                        print("(<) Left key pressed", currentposition)
                        currentposition = m_backwards
                    if game.key == pygame.K_RIGHT:
                        player_pos_x += speed_x
                        print("(>) Right key pressed", currentposition)
                        currentposition = m_forwards
                    if game.key == pygame.K_UP:
                        player_pos_y -= speed_y
                        print("(^) Up key pressed", currentposition)
                        currentposition = m_up
                    if game.key == pygame.K_DOWN:
                        player_pos_y += speed_y
                        print("(v)Down key pressed", currentposition)
                        currentposition = m_down
                    if game.key == pygame.K_a:
                        player_pos_x -= speed_x
                        print("(a) Left key pressed", currentposition)
                        currentposition = m_backwards
                    if game.key == pygame.K_d:
                        player_pos_x += speed_x
                        print("(d) Right key pressed", currentposition)
                        currentposition = m_forwards
                    if game.key == pygame.K_w:
                        player_pos_y -= speed_y
                        print("(w) Up key pressed", currentposition)
                        currentposition = m_up
                    if game.key == pygame.K_s:
                        player_pos_y += speed_y
                        print("(s) Down key pressed", currentposition)
                        currentposition = m_down
                    # Collisions ------------------------------------
                    if player_pos_x in range(
                            170, 200) and player_pos_y in range(170, 200):
                        print('Player is in collision RANGE!')
                        if currentposition == m_forwards and game.key == pygame.K_RIGHT or game.key == pygame.K_d:
                            player_pos_x = 170
                            speed_x = 0
                        elif game.key == pygame.K_a:
                            speed_x = 5
                            player_pos_x -= speed_x
                        elif game.key == pygame.K_LEFT:
                            speed_x = 5
                            player_pos_x -= speed_x

                elif game.type == pygame.KEYUP:
                    if game.key == pygame.K_LEFT:
                        player_pos_x -= 0
                        print("Left key released")
                    if game.key == pygame.K_RIGHT:
                        player_pos_x += 0
                        print("Right key released")
                    if game.key == pygame.K_UP:
                        player_pos_y -= 0
                        print("Up key released")
                    if game.key == pygame.K_DOWN:
                        player_pos_y += 0
                        print("Down key released")
                    if game.key == pygame.K_ESCAPE:
                        pygame.display.set_caption(engine_title)
                        MiPi.Main()
                        game_running = False

                MiPi.DrawBoxCol(box_x, box_y)
                MiPi.CheckForCollision(box_x, box_y, player_pos_x)

                pygame.display.set_caption(game_title)
                # MiPi.RenderTest(player_pos_x, player_pos_y)
                MiPi.LoadPlayer(player_pos_x, player_pos_y)
                MiPi.LoadNPC(npc_pos_x, npc_pos_y)
                # MiPi.LoadEnemy(MiPiSettings.enemy_x, MiPiSettings.enemy_y)

                # The below is commented out, because UI is NOT being drawn to the game screen.
                # If you decide to add pygame_gui UI elements, then please use these!
                # mainframe.process_events(game)
                # mainframe.update(delta_time)

                sysclock.tick(mainFPS)
                pygame.display.flip()
                pygame.display.update()

        return MiPiSettings.player_img, MiPiSettings.sprite_path, \
               MiPiSettings.npc_img, MiPiSettings.npc_path, player_pos_x
コード例 #6
0
    def run(self):
        while self.is_running:
            time_delta = self.clock.tick(60) / 1000.0
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.is_running = False

                if (event.type == pygame.USEREVENT
                        and event.user_type == pygame_gui.UI_BUTTON_PRESSED
                        and event.ui_element == self.load_button):
                    self.file_dialog = UIFileDialog(
                        pygame.Rect(160, 50, 440, 500),
                        self.ui_manager,
                        window_title='Load Image...',
                        initial_file_path='data/images/',
                        allow_existing_files_only=True)
                    self.load_button.disable()

                if (event.type == pygame.USEREVENT and event.user_type
                        == pygame_gui.UI_FILE_DIALOG_PATH_PICKED):
                    if self.display_loaded_image is not None:
                        self.display_loaded_image.kill()

                    try:
                        image_path = create_resource_path(event.text)
                        loaded_image = pygame.image.load(
                            image_path).convert_alpha()
                        image_rect = loaded_image.get_rect()
                        aspect_ratio = image_rect.width / image_rect.height
                        need_to_scale = False
                        if image_rect.width > self.max_image_display_dimensions[
                                0]:
                            image_rect.width = self.max_image_display_dimensions[
                                0]
                            image_rect.height = int(image_rect.width /
                                                    aspect_ratio)
                            need_to_scale = True

                        if image_rect.height > self.max_image_display_dimensions[
                                1]:
                            image_rect.height = self.max_image_display_dimensions[
                                1]
                            image_rect.width = int(image_rect.height *
                                                   aspect_ratio)
                            need_to_scale = True

                        if need_to_scale:
                            loaded_image = pygame.transform.smoothscale(
                                loaded_image, image_rect.size)

                        image_rect.center = (400, 300)

                        self.display_loaded_image = UIImage(
                            relative_rect=image_rect,
                            image_surface=loaded_image,
                            manager=self.ui_manager)

                    except pygame.error:
                        pass

                if (event.type == pygame.USEREVENT
                        and event.user_type == pygame_gui.UI_WINDOW_CLOSE
                        and event.ui_element == self.file_dialog):
                    self.load_button.enable()
                    self.file_dialog = None

                self.ui_manager.process_events(event)

            self.ui_manager.update(time_delta)

            self.window_surface.blit(self.background, (0, 0))
            self.ui_manager.draw_ui(self.window_surface)

            pygame.display.update()