class MainMenu:
    """
    Main menu
    """
    def __init__(self, screen):
        self.screen = screen
        # Loading the background world for the main menu
        self.background_world = World(
            load_file=os.path.join("Worlds", "MarioBros"))
        self.play_button = TextButton((10, 10), "Play game", FONT_BIG,
                                      pygame.Color("white"),
                                      pygame.Color("red"))
        self.level_button = TextButton((10, 60), "Level Creator", FONT_BIG,
                                       pygame.Color("white"),
                                       pygame.Color("red"))
        self.about_button = TextButton((10, 110), "About", FONT_BIG,
                                       pygame.Color("white"),
                                       pygame.Color("red"))
        self.settings_button = TextButton((10, 160), "Settings", FONT_BIG,
                                          pygame.Color("white"),
                                          pygame.Color("red"))
        self.clock = pygame.time.Clock()
        self.time_since_creation = 0

    def reset(self):
        self.background_world = World(
            load_file=os.path.join("Worlds", "MarioBros"))

    def render(self):
        self.background_world.render(self.screen)
        self.play_button.render(self.screen)
        self.level_button.render(self.screen)
        self.about_button.render(self.screen)
        self.settings_button.render(self.screen)

    def loop(self):
        self.time_since_creation = 0
        while True:
            self.clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()

            mouse_buttons = pygame.mouse.get_pressed()
            pos = pygame.mouse.get_pos()
            self.play_button.update_selected(pos)
            self.level_button.update_selected(pos)
            self.about_button.update_selected(pos)
            self.settings_button.update_selected(pos)
            if mouse_buttons[0] and self.time_since_creation > 0.1:
                if self.play_button.selected:
                    return "level"
                elif self.level_button.selected:
                    return "creator"
                elif self.about_button.selected:
                    return "about"
                elif self.settings_button.selected:
                    return "settings"

            self.render()
            get_fps = self.clock.get_fps()
            if get_fps != 0:
                # this small line allows a user to play the game in the main menu!
                self.background_world.update(1 / get_fps)
                self.time_since_creation += 1 / get_fps

            pygame.display.update()
class SettingsMenu:
    """
    Menu to adjust settings.
    """
    SETTINGS = {"Music": "on", "Sound": "on"}

    def __init__(self, screen):
        self.screen = screen
        self.settings_button = TextButton((10, 10), "Settings", FONT_BIG,
                                          pygame.Color("White"))
        self.options = dict()
        self.position = [10, 70]
        self.load_options()
        self.main_button = TextButton(self.position, "Main menu", FONT_BIG,
                                      pygame.Color("white"),
                                      pygame.Color("red"))
        self.clock = pygame.time.Clock()

        # A variable registering how long the user has been in the menu. Not allowing anything to happen before
        # this variable gets to a certain size, makes sure it is not possible to accidently click and go two menus
        # further
        self.time_after_creation = 0

    def load_options(self):
        """
        Loads all options from the settings file.
        """
        with open(os.path.join("Menus", "settings"), "r") as file:
            for line in file.read().splitlines():
                option_name, different_options = line.split(":")
                different_options, selected_option = different_options.split(
                    " | ")
                different_options = different_options.split(" ")
                selected_option = selected_option.replace(" ", "")
                self.options[option_name] = [
                    TextButton(self.position[:], option_name + ": ",
                               FONT_MEDIUM, pygame.Color("white")),
                ]
                SettingsMenu.SETTINGS[option_name] = selected_option
                current_pos = self.position[:]
                for option in different_options:
                    current_pos[
                        0] += 10 + self.options[option_name][-1].size[0]
                    self.options[option_name].append(
                        TextButton(current_pos[:], option, FONT_MEDIUM,
                                   pygame.Color("white"), pygame.Color("red")))
                    if option == selected_option:
                        self.options[option_name][-1].selected = True

                self.position[1] += self.options[option_name][0].size[1] + 10

    def save_options(self):
        with open(os.path.join("Menus", "settings"), "w") as f:
            for option_name in self.options:
                string_option = option_name + ":"
                selected_option = None
                for option in self.options[option_name][1:]:
                    string_option += option.message + " "
                    if option.selected:
                        selected_option = option.message
                string_option += "| " + selected_option + "\n"
                f.write(string_option)

    def render(self):
        self.settings_button.render(self.screen)
        self.main_button.render(self.screen)
        for option_name in self.options:
            for button in self.options[option_name]:
                button.render(self.screen)

    def update_options(self):
        pos = pygame.mouse.get_pos()
        for option in self.options:
            for button in self.options[option][1:]:
                if button.is_selected(pos) and not button.selected:
                    for button2 in self.options[option][1:]:
                        button2.selected = False
                    button.selected = True
                    SettingsMenu.SETTINGS[option] = button.message
                self.save_options()

    def loop(self):
        self.time_after_creation = 0
        while True:
            self.clock.tick(FPS)
            self.screen.fill((0, 0, 0))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()
            mouse_buttons = pygame.mouse.get_pressed()
            pos = pygame.mouse.get_pos()
            self.main_button.update_selected(pos)
            if mouse_buttons[0] and self.time_after_creation > 0.1:
                self.update_options()

                if self.main_button.selected:
                    return "main"

            if SettingsMenu.SETTINGS["Music"] == "off":
                mixer.music.pause()
            elif SettingsMenu.SETTINGS["Music"] == "on":
                mixer.music.unpause()

            get_fps = self.clock.get_fps()
            if get_fps != 0:
                self.time_after_creation += 1 / get_fps

            self.render()

            pygame.display.update()
class GameOverMenu:
    """
    Menu for when the player dies or wins the level.
    """
    def __init__(self, screen):
        self.screen = screen
        self.report_button = TextButton((10, 10), "You won!", FONT_BIG,
                                        pygame.Color("White"))
        self.play_button = TextButton((10, 70), "Play again", FONT_BIG,
                                      pygame.Color("white"),
                                      pygame.Color("red"))
        self.main_button = TextButton((10, 130), "Main menu", FONT_BIG,
                                      pygame.Color("white"),
                                      pygame.Color("red"))
        self.clock = pygame.time.Clock()

        # A variable registering how long the user has been in the menu. Not allowing anything to happen before
        # this variable gets to a certain size, makes sure it is not possible to accidently click and go two menus
        # further
        self.time_after_creation = 0

    def render(self, world):
        world.render(self.screen)
        self.report_button.render(self.screen)
        self.play_button.render(self.screen)
        self.main_button.render(self.screen)

    def mouse_update(self):
        mouse_buttons = pygame.mouse.get_pressed()
        pos = pygame.mouse.get_pos()

        self.play_button.update_selected(pos)
        self.main_button.update_selected(pos)
        if mouse_buttons[0] and self.time_after_creation > 0.1:
            if self.play_button.selected:
                return "level"
            elif self.main_button.selected:
                return "main"

        return None

    def loop(self, world):
        self.time_after_creation = 0
        if not world.won:
            self.report_button.set_text("You lost")
        while True:
            self.clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()

            selected_menu = self.mouse_update()
            if selected_menu is not None:
                return selected_menu

            get_fps = self.clock.get_fps()
            if get_fps != 0:
                self.time_after_creation += 1 / get_fps

            self.render(world)
            pygame.display.update()
Exemple #4
0
class AboutMenu:
    """
    Menu wiht some information about the game, mainly on how to create a new world.
    """
    def __init__(self, screen):
        self.screen = screen
        self.phrases = [
            "This game was made by Jasper Dekoninck.",
            "You can use the code for whatever you want, no permission is required.",
            "Most sprites are copied from http://www.mariouniverse.com/sprites-ds-nsmb/.",
            "",
            "A small guide for the creation of new levels is probably required.",
            "If you click on 'Level Creator' in main menu, you will be able to create new worlds yourself.",
            "Just follow the steps, when you're doing the actual creating, you can do the following things:",
            "1. Select a game object at the right side by pressing your left mouse button on it.",
            "     Once selected, you can place the game object in your world by again pressing the left mouse button.",
            "     You can create multiple instances of the same object by keeping the mouse pressed.",
            "2. You can delete game objects by clicking on them with your right mouse button.",
            "3. You can move the screen up / down / left / right by using the arrow keys.",
            "4. You can play your world in creation by pressing the escape key.",
            "     If you press the key again, you stop playing.",
            "You can save your world by pressing enter."
        ]
        self.buttons = []
        y_pos = 10
        for phrase in self.phrases:
            self.buttons.append(TextButton((10, y_pos), phrase, FONT_STANDARD, pygame.Color("white")))
            y_pos += self.buttons[-1].size[1] + 2

        pos_main = (10, y_pos + self.buttons[-1].size[1] + 10)
        self.main_button = TextButton(pos_main, "Main menu", FONT_BIG, pygame.Color("white"), pygame.Color("red"))

        # A variable registering how long the user has been in the menu. Not allowing anything to happen before
        # this variable gets to a certain size, makes sure it is not possible to accidently click and go two menus
        # further
        self.time_after_creation = 0
        self.clock = pygame.time.Clock()

    def render(self):
        for button in self.buttons:
            button.render(self.screen)
        self.main_button.render(self.screen)

    def loop(self):
        self.time_after_creation = 0
        while True:
            self.clock.tick(FPS)
            self.screen.fill((0, 0, 0))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()

            mouse_buttons = pygame.mouse.get_pressed()
            pos = pygame.mouse.get_pos()
            self.main_button.update_selected(pos)
            if mouse_buttons[0] and self.time_after_creation > 0.1:
                if self.main_button.selected:
                    return "main"

            self.render()
            get_fps = self.clock.get_fps()
            if get_fps != 0:
                self.time_after_creation += 1 / get_fps

            pygame.display.update()