Beispiel #1
0
    def __init__(self, master):
        """Construct a new game of a MarioApp game.

        Parameters:
            master (tk.Tk): tkinter root widget
        """



        #level readed
        self._master = master
        self._master.title('Mario')
        self.down_pressed = False

        self._master.update()
        file_path = filedialog.askopenfilename()   # Openning  a view to let the user choose the configuration file
        filename, file_extension = os.path.splitext(file_path)  # Getting the extension of the selected file
        if file_extension != ".txt" :     # Show an error message box when the extension is Wrong. The extension must be .txt
            messagebox.showerror("Error", "File extension should be .txt")
            exit()

        self.levels = []

        self.load_configuration(file_path) # to load configurations from the configuration file

        world_builder = WorldBuilder(BLOCK_SIZE, gravity=(0, self.gravity), fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder
        self._world = load_world(world_builder, self.level)

        # Create a menu bar on the top of the game and association a function to each button on the menu with command parameter
        menubar = tk.Menu(self._master)
        menubar.add_command(label = "Load Level", command = self.showDialogInput)
        menubar.add_command(label = "Reset Level", command = self.resetLevel)
        menubar.add_command(label = "High scores", command = self.displayHighScores)
        menubar.add_command(label = "Exit", command = self.exitGame)
        self._master.config(menu = menubar)

        self._player = Player(name = self.character, max_health=self.max_health)
        self._player.invincible = False
        self._world.add_player(self._player, self.starting_x, self.starting_y, mass = self.mass)

        self._setup_collision_handlers()

        self._renderer = MarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES, MOB_IMAGES)

        size = tuple(map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(master, size, self._renderer)
        self._view.pack()
        self.bind()

        # Wait for window to update before continuing
        master.update_idletasks()

        self.statusBar = StatusBar(self._master)

        self.step()
        self._master.mainloop()
Beispiel #2
0
    def __init__(self, master: tk.Tk):
        """Construct a new game of a MarioApp game.

        Parameters:
            master (tk.Tk): tkinter root widget
        """
        self._master = master
        # default configuration setting
        self._level = "level1.txt"
        self._gravity = (0, 300)
        self._max_health = 5
        self._mass = 100
        self._x = BLOCK_SIZE
        self._y = BLOCK_SIZE
        self._max_velocity = 500
        self._config = {}

        self._master.update_idletasks()
        self.load_config()

        world_builder = WorldBuilder(BLOCK_SIZE,
                                     self._gravity,
                                     fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder

        self._player = Player(max_health=self._max_health)
        self._player.set_jumping(True)
        self._player.set_shoot(False)

        self.reset_world(self._level)

        self._renderer = MarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES,
                                           MOB_IMAGES)

        size = tuple(
            map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(master, size, self._renderer)
        self._view.pack()

        self.bind()

        self.menu_bar()

        # create the character status bar
        self.status_bar = Status(master)
        self.status_bar.pack()

        # Wait for window to update before continuing
        master.update_idletasks()
        self.step()
Beispiel #3
0
    def _start_game(self):
        path = tk.simpledialog.askstring("Mario", "Configuration file:")
        self._game = loader.load_level(path)
        if self._game == None:
            tk.messagebox.showerror("Error", "Configuration file wrong.")
            self._master.destroy()

        # World builder
        world_builder = WorldBuilder(
            BLOCK_SIZE,
            gravity=(0, int(self._game["==World=="]["gravity"])),
            fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder

        self._current_level = self._game["==World=="]['start']
        self._world = load_world(self._builder, self._current_level)

        # Set tunnel/goal
        Flagpole._next_level = self._game[("==" + self._current_level +
                                           "==")]["goal"]
        Tunnel._next_level = self._game[("==" + self._current_level +
                                         "==")].get("tunnel", None)

        self._player = Player(
            max_health=int(self._game["==Player=="]["health"]))
        print(self._player.get_shape())
        self._world.add_player(self._player,
                               int(self._game["==Player=="]["x"]),
                               int(self._game["==Player=="]["y"]),
                               int(self._game["==Player=="]["mass"]))

        self._max_speed = int(self._game["==Player=="]["max_velocity"])

        self._setup_collision_handlers()

        self._renderer = AnimatedMarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES,
                                                   MOB_IMAGES)  # Into Animated

        size = tuple(
            map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(self._master, size, self._renderer)
        self._view.pack()

        self._status_view = StatusView(self._master,
                                       int(self._game["==Player=="]['health']),
                                       0)

        self.bind()
Beispiel #4
0
    def __init__(self, master: tk.Tk) :
        """Construct a new game of a MarioApp game.

        Parameters:
            master (tk.Tk): tkinter root widget
        """
        self._master = master
        config_name = askopenfilename(filetypes=( ("Text file", "*.txt"),("HTML files", "*.html;*.htm")))
        self.load_config(config_name)
        self._gravity
        self._level_name
        self._healthdeterminant
        world_builder = WorldBuilder(BLOCK_SIZE, (0,300), fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder

        self._player = Player(max_health= self._health)
        self._powerup = PowerUp()
        self.reset_world(self._level_name)

        #self._renderer = MarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES, MOB_IMAGES)
        self._sprite_renderer = SpriteSheetView(BLOCK_IMAGES, ITEM_IMAGES, MOB_IMAGES)
        size = tuple(map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(master, size, self._sprite_renderer)
        self._view.pack()

        self.bind()

        # Wait for window to update before continuing
        master.update_idletasks()

        self._status_view = StatusView(master, self._health)
        self._status_view.pack()
        self._status_view.update_score(self._player.get_score())
        self._status_view.update_health(self._player.get_health())

        self._setup_collision_handlers()
        master.update_idletasks()
        self._start_time = 0
        self.step()
        self.menu = MenuBar(master, [("File", {"Reset" : self._reset,
                                               "Load Level" : self._import_new_level,
                                               "Exit" : self._close,
                                               "High Score": self._status_view.high})])
        self._collide = False
Beispiel #5
0
    def reset_world(self, new_level=None, full=False):
        if new_level == None:
            new_level = self._current_level
        else:
            self._current_level = new_level
            self._scores = HighScores()
        world_builder = WorldBuilder(BLOCK_SIZE,
                                     gravity=(0, 500),
                                     fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder
        self._world = load_world(self._builder, new_level)

        Flagpole._next_level = self._game[("==" + self._current_level +
                                           "==")]["goal"]
        Tunnel._next_level = self._game[("==" + self._current_level +
                                         "==")].get("tunnel", None)

        if full:  # full reset player stats
            self._player = Player(
                max_health=int(self._game["==Player=="]["health"]))
        self._world.add_player(self._player,
                               int(self._game["==Player=="]["x"]),
                               int(self._game["==Player=="]["y"]),
                               int(self._game["==Player=="]["mass"]))

        self._setup_collision_handlers()

        self._view.pack_forget()
        size = tuple(
            map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(self._master, size, self._renderer)
        self._view.pack()

        self._status_view.unpack()
        self._status_view = StatusView(self._master,
                                       int(self._game["==Player=="]['health']),
                                       self._player._score)
Beispiel #6
0
    def __init__(self, master: tk.Tk):
        """Construct a new game of a MarioApp game.

        Parameters:
            master (tk.Tk): tkinter root widget
        """
        self._master = master
        self._master.title("Mario bird")

        world_builder = WorldBuilder(BLOCK_SIZE, gravity=(0, 300), fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder

        self._player = Player(max_health=5)
        self.reset_world('level1.txt')
        self._level_holder = 'level1.txt'

        self._renderer = MarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES, MOB_IMAGES)

        size = tuple(map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(master, size, self._renderer)
        self._view.pack()
        self.bind()
        self.menubar()
        #Stats controller for changing the status bar.
        #used in the function 'step'
        self._health = self._player.get_health()
        self._score = self._player.get_score()
        self._status_bar = tk.Frame(self._master)
        self.status_bar()
        # Wait for window to update before continuing
        master.update_idletasks()
        self.step()
        self._death_action = False
Beispiel #7
0
    def __init__(self, master: tk.Tk):
        """Construct a new game of a MarioApp game.

        Parameters:
            master (tk.Tk): tkinter root widget
        """
        """
        -------------------------------------
        task 2.3 
        增加函数 load_config(在MarioApp)最底部
        涉及到的变量参数修改已在注释中标出
        -------------------------------------
        """
        self._master = master
        # 设置了一些参数默认值
        self.gravity = 300  # world_builder = WorldBuilder(BLOCK_SIZE, gravity=(0,self.gravity), fallback=create_unknown)
        self.level = 'level1.txt'  # 直接改变level值
        self.character_name = 'mario'  # self._player = Player(max_health=5,name=self.character_name)
        self.character_x = 1  # 在add_player中改变参数
        self.character_y = 1  # 在add_player中改变参数
        self.character_mass = 100  # 在add_player中改变参数
        self.max_velocity = 200  # 暂时不知参数位置
        # 调用load config函数
        self._load_config()
        #----------------------------------------------------------task3

        fm = tk.Frame(self._master)
        fm.pack(padx=10, expand=1)
        load = tk.Button(fm, text='Load Level', command=self.load_f)
        load.pack(side=RIGHT, fill=Y)
        rest = tk.Button(fm, text='Rest Level', command=self.rest_f)
        rest.pack(side=RIGHT, fill=Y)
        Exit = tk.Button(fm, text='Exits the game', command=self._master.quit)
        Exit.pack(side=RIGHT, fill=Y)

        world_builder = WorldBuilder(BLOCK_SIZE,
                                     gravity=(0, self.gravity),
                                     fallback=create_unknown)
        world_builder.register_builders(BLOCKS.keys(), create_block)
        world_builder.register_builders(ITEMS.keys(), create_item)
        world_builder.register_builders(MOBS.keys(), create_mob)
        self._builder = world_builder

        self._player = Player(max_health=5, name=self.character_name)
        self.reset_world(self.level)

        self._renderer = MarioViewRenderer(BLOCK_IMAGES, ITEM_IMAGES,
                                           MOB_IMAGES)

        size = tuple(
            map(min, zip(MAX_WINDOW_SIZE, self._world.get_pixel_size())))
        self._view = GameView(master, size, self._renderer)
        self._view.pack()

        self.bind()

        #``````````````````````````````````````````````````
        #task1.3
        Health = tk.Frame(self._master)
        Health.pack(padx=300, pady=50)
        self.score_num = tk.Variable = 0
        self.Score = tk.Button(Health, text='Score:' + str(self.score_num))
        self.Score.pack(side=RIGHT, fill=Y)
        label = tk.Label(bg="black", width=self._player._max_health)
        label.place(relx=0, rely=0.9)
        self.label1 = tk.Label(bg="blue", width=self._player._health)
        self.label1.place(relx=0, rely=0.9)
        #``````````````````````````````````````````````````

        # Wait for window to update before continuing
        master.update_idletasks()
        self.step()
        self._master.mainloop()