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()
def reset_world(self, new_level): self._world = load_world(self._builder, new_level) self._world.add_player(self._player, self.character_x * BLOCK_SIZE, self.character_y * BLOCK_SIZE, mass=self.character_mass) self._builder.clear() self.level = new_level self._setup_collision_handlers()
def reset_world(self, new_level): self._world = load_world(self._builder, new_level) self._world.add_player(self._player, BLOCK_SIZE, BLOCK_SIZE) self._builder.clear() self._setup_collision_handlers() self._level_holder = new_level self._death_action = False self._player.change_health(self._player.get_max_health())
def reset_world(self, new_level="level1.txt"): self.level = new_level self._world = load_world(self._builder, new_level) health = self._player.get_health() self._player = Player(max_health=5) self._player.invincible = False self.down_pressed = False self._player.change_health(health - self._player.get_max_health()) self._world.add_player(self._player, BLOCK_SIZE, BLOCK_SIZE) self._setup_collision_handlers()
def reset_world(self, new_level: str): ''' reset the current world, and bind the buttons again :param new_level: :return: ''' self._world = load_world(self._builder, new_level) self._world.add_player(self._player, BLOCK_SIZE, BLOCK_SIZE) self._builder.clear() self.bind() self._setup_collision_handlers()
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()
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)
def reset_world(self, new_level): self._world = load_world(self._builder, new_level) self._world.add_player(self._player, self._x, self._y, self._mass) self._builder.clear() self._setup_collision_handlers()