class Game: def __init__(self): self._current_player = 0 self._players = [Player(0), Player(1)] self._board = Board() self._gameUI = GameUI(self._players[0]) def game_loop(self): valid_keys = [1, 2, 3, 4, 5, 6, 7] update_ui = False done = False player_won = False row = -1 column = -1 while not done: update_ui = False # check for player input events for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYUP: if player_won: # N key if event.key == 110: done = True # Y key elif event.key in [121, 122]: player_won = False done = False self.restart() else: # Try to insert to a column column = (event.key - 49) if column + 1 in valid_keys: row = self.add_chip(column) # Adding a chip wa possible if row > -1: player_won = self.check_player_wins( self.get_current_player()) update_ui = True # UI has to be updated if update_ui: self._gameUI.draw_board(self.get_current_player(), row, column) if player_won: self._gameUI.draw_player_won(self.get_current_player()) else: self.switch_player() self._gameUI.draw_player(self.get_current_player()) def switch_player(self): self._current_player += 1 self._current_player = self._current_player % 2 def restart(self): self._board.clear() self._gameUI.init_ui(self.get_current_player()) def add_chip(self, column): player = self.get_current_player() return self._board.add_chip(player, column) def check_player_wins(self, player): return self._board.check_player_wins(player) def get_board(self): return self._board def get_player(self, id): return self._players[id] def get_current_player(self): return self._players[self._current_player]
class Game: def __init__(self): self._players = [Player(0), Player(1)] self._current_player = 0 self._board = Board() self._gameUI = GameUI(self._players[0]) def end_game(self, player): if messagebox.askquestion(player.get_name() + 'WON!', 'Restart game?') == 'yes': self.switch_player() self._gameUI.draw_player(self.get_current_player()) def switch_player(self): self._current_player += 1 self._current_player = self._current_player % 2 def restart(self): self._board.clear_board() self._gameUI.init_ui(self.get_current_player()) def add_chip(self, column): player = self.get_current_player() return self._board.try_add_chip(player, column) def check_player_wins(self, player): return self._board.check_player_wins(player) def get_board(self): return self._board def get_player(self, id): return self._players[id] def get_current_player(self): return self._players[self._current_player] def game_loop(self): row = -1 column = -1 valid_keys = [1, 2, 3, 4, 5, 6, 7] update_ui = False end_game = False player_won = False while not end_game: update_ui = False # check for player input events for event in pygame.event.get(): if event.type == pygame.QUIT: end_game = True elif event.type == pygame.KEYUP: column = (event.key - 49) if column + 1 in valid_keys: row = self.add_chip(column) if row > -1: player_won = self.check_player_wins( self.get_current_player()) update_ui = True if update_ui: self._gameUI.draw_board(self.get_current_player(), row, column) if player_won: if messagebox.askquestion( self.get_current_player().get_name() + ' WON!', 'Restart game?') == 'yes': player_won = False end_game = False self.restart() else: end_game = True else: self.switch_player() self._gameUI.draw_player(self.get_current_player())