def __init__(self, master, game=None, grid_view=None): """Constructor Parameters: master (tk.Tk|tk.Frame): The parent widget. game (model.AbstractGame): The game to play. Defaults to a game_regular.RegularGame. grid_view (view.GridView): The view to use for the game. Optional. Raises: ValueError: If grid_view is supplied, but game is not. """ self._master = master # Game if game is None: game = RegularGame(types=3) self._game = game # Grid View if grid_view is None: if game is None: raise ValueError("A grid view cannot be given without a game.") grid_view = view.GridView(master, self._game.grid.size()) self._grid_view = grid_view self._grid_view.pack() self._grid_view.draw(self._game.grid, self._game.find_connections()) # Events self.bind_events()
def main(): # Set up model component grid = model.Board() # Set up view component game_view = view.GameView(600, 600) grid_view = view.GridView(game_view, len(grid.tiles)) grid.add_listener(grid_view) # Handle control component responsibility here commands = keypress.Command(game_view) grid.place_tile(value=2) # Game continues until there is no empty # space for a tile while grid.has_empty(): grid.place_tile() cmd = commands.next() if cmd == keypress.LEFT: grid.left() elif cmd == keypress.RIGHT: grid.right() elif cmd == keypress.UP: grid.up() elif cmd == keypress.DOWN: grid.down() elif cmd == keypress.CLOSE: # Ended game by closing window print(f"Your score: {grid.score()}") sys.exit(0) else: assert cmd == keypress.UNMAPPED game_view.lose(grid.score())
def main(): # Model component grid = model.Grid() # View component game_view = view.GameView(600, 600) grid_view = view.GridView(game_view, len(grid.tiles)) grid.add_listener(grid_view) # Control component responsibility: commands = keypress.Command(game_view) grid.place_tile() # Continue the game until it is not empty. while grid.find_empty(): grid.place_tile() cmd = commands.next() if cmd == keypress.LEFT: grid.left() elif cmd == keypress.RIGHT: grid.right() elif cmd == keypress.UP: grid.up() elif cmd == keypress.DOWN: grid.down() else: assert cmd == keypress.UNMAPPED game_view.lose(grid.score())
def high_scores_display(self): """ Displays high score / leaderboard in a Toplevel """ top = tk.Toplevel(self._parent_frame) top.title('Leaderboard') # Leaderboard Frames self._best_player_frame = tk.Frame(top) self._best_player_frame.pack(expand=True, pady=5, padx=10) # Best player label self._best_player = self._high_scores.get_sorted_data()[0] self._best_player_label = tk.Label( self._best_player_frame, text='Best Player: {} with {} points!'.format( self._best_player.get('name'), self._best_player.get('score'))) self._best_player_label.pack() # Best player static visual representation grid_list = self._best_player.get('grid') game = game_regular.RegularGame.deserialize(grid_list) grid_view = view.GridView(self._best_player_frame, game.grid.size()) self._grid_view = grid_view self._grid_view.pack() self._grid_view.draw(game.grid, game.find_connections()) # Leaderboard score table score_frame = tk.Frame(top) score_frame.pack(pady=5, padx=20) rows = 0 for i in self._high_scores.get_sorted_data(): name_label = tk.Label(score_frame, text=i.get('name')).grid(row=rows, column=0, sticky='nw', padx=20) score_label = tk.Label(score_frame, text=i.get('score')).grid(row=rows, column=1, sticky='ne', padx=20) rows += 1
def getmode(self): '''Apply game mode chosen from gamemode function''' if self._mode.get() == 'Regular': self._game = RegularGame() elif self._mode.get() == 'Make13': self._game = Make13Game() elif self._mode.get() == 'Lucky7': self._game = Lucky7Game() if self._mode.get() == 'Unlimited': self._game = UnlimitedGame() self._lologame = copy.deepcopy(self._game) self._grid_view.destroy() self._grid_view = view.GridView(self._master, self._game.grid.size()) self._grid_view.pack() self._grid_view.draw(self._game.grid, self._game.find_connections()) self._grid_view.config(bg='gray') self._game.on('resolve',self.resolve) self._gamemode_frame.destroy()
def play_loaded_game(self, player): """ Plays the loaded game """ print(player) return data = next(i for i in self.data if i.get('name') == self._load_var.get()) grid_list = data.get('grid') game = self._selected_game_play.deserialize(grid_list) # Initialize a Toplevel window for gameplay top = tk.Toplevel(self._parent_frame) grid_view = view.GridView(top, game.grid.size()) play_game = LoloApp(top, game=self._selected_game_play, player_name=data.get('name'), grid_view=grid_view, default_score=data.get('score'))
def objective_tiles(self): """ Draw grid of objective tiles onto the statusbar """ # Frame for tiles self._objectives_frame = tk.Frame(self._statusbar) self._objectives_frame.pack() # draw the objective tile grid and pack it objectives = game_regular.RegularGame.deserialize( self._game._objectives) self._objective_view = view.GridView( self._objectives_frame, (1, len(self._game._objectives[0]))) self._objective_view.pack(side=tk.RIGHT) self._objective_view.draw(objectives.grid, objectives.find_connections()) self._moves_left = tk.Label(self._objectives_frame, text='Moves left: {}'.format( self._game._moves)) self._moves_left.pack(side=tk.LEFT)
def __init__(self,master,game, grid_view=None): """ Constructor Parameters: master (tk.Tk|tk.Frame): The parent widget. game (model.AbstractGame): The game to play. Defaults to a game_regular.RegularGame. grid_view (view.GridView): The view to use for the game. Optional. """ master.title('Leaderboard') self._master = master #look for game data from file according to game's name if game.get_name()== 'Regular': highscore_mode = 'regular' elif game.get_name()=='Lucky 7': highscore_mode = 'lucky7' elif game.get_name()=='Make 13': highscore_mode = 'Make 13' elif game.get_name() == 'Unlimited': highscore_mode = 'unlimited' self._highscore = HighScoreManager(gamemode=highscore_mode) highest = self._highscore.get_sorted_data()[0] self._bestplayer = tk.Label(master, text='Best Player: {} with {} points!'.format(highest['name'], highest['score'])) self._bestplayer.pack() self._grid_list = highest['grid'] if game is None: game = RegularGame(types=3) self._game = game grid_view = view.GridView(master, self._game.grid.size()) self._grid_view = grid_view self._grid_view.pack() highscore_game = self._game.deserialize(self._grid_list) self._grid_view.draw(highscore_game.grid) #leader board displaying a number of high scores self._title= tk.Label(master,text='Leaderboard') self._title.pack() self._leaderboard = tk.Frame(master) self._leaderboard.pack(fill=tk.BOTH) self._leaderboard_name = tk.Frame(self._leaderboard) self._leaderboard_name.pack(side=tk.LEFT) self._leaderboard_score = tk.Frame(self._leaderboard) self._leaderboard_score.pack(side=tk.RIGHT) for name in self._highscore.get_sorted_data(): name = tk.Label(self._leaderboard_name,text=name['name']) name.pack(anchor=tk.NW) for score in self._highscore.get_sorted_data(): score = tk.Label(self._leaderboard_score,text=score['score']) score.pack(anchor=tk.NE)