def __init__(self, grid_size, max_generation): self.grid_size = grid_size self.grid_size_x, self.grid_size_y = grid_size self.generation = 0 self.max_generation = max_generation self.active_cells = 0 self.game_board = Grid(grid_size, Cell) window_size = tuple([x * 5 for x in grid_size]) self.view = GameView(window_size) self.empty_grid = Grid(self.grid_size, Cell)
def start_loading(self, level: int): game_view = GameView() for message in game_view.setup(level): if self.progress < GAME_LOAD_COUNT: self.progress_message = message self.progress += 1 time.sleep(1) # to get loading effect in case of fast load else: self.progress_message = "Finishing.." self.window.show_view(game_view)
def __init__(self): super(GameController, self).__init__() self.view = GameView() self.model = GameModel() self.hud = None self.board = None self.players = [] self.currentPlayer = 0 # Who do we see in the views? self.playersTurn = 0 # Who's turn is it?
class GameController (Controller): def __init__(self): super(GameController, self).__init__() self.view = GameView() self.model = GameModel() self.hud = None self.board = None self.players = [] self.currentPlayer = 0 # Who do we see in the views? self.playersTurn = 0 # Who's turn is it? #def win(player?): # pass def notify(self, event): if isinstance(event, events.MouseReleased): grabbedCard = None for card in self.players[self.currentPlayer].hand.cards: if card.view.grabbed: grabbedCard = card break if grabbedCard: for laneIdx, lane in enumerate(self.board.lanes, 0): if lane.view.inBounds(event.mousePos): if not self.players[self.playersTurn].hasEnoughMana(grabbedCard.model.manaCost): grabbedCard.release() break self.players[self.playersTurn].expendMana(grabbedCard.model.manaCost) if self.playersTurn == 0: lane.placeCard(grabbedCard, 0) else: lane.placeCard(grabbedCard, lane.laneLength()-1) self.players[self.currentPlayer].hand.removeCard(grabbedCard) break else: card.release() if isinstance(event, events.StartTurn): print "Starting Player %s Turn" %self.playersTurn for lane in self.board.lanes: lane.startTurn(self.playersTurn) # Now the player who's turn it is draws a card self.players[self.playersTurn].drawCard() self.players[self.playersTurn].gainMana() #AI Takes Over if self.playersTurn == 0: playLane = self.getSuggestedPlay() randomCard = self.players[self.playersTurn].hand.cards[0] if (self.players[self.currentPlayer].hasEnoughMana(randomCard.model.manaCost)): self.players[self.currentPlayer].expendMana(randomCard.model.manaCost) self.board.lanes[playLane].placeCard(randomCard, 0) self.players[self.currentPlayer].hand.removeCard(randomCard) g.event_manager.post(events.ButtonEndTurn(1)) if isinstance(event, events.MouseDown): print "mousedown" if event.mouseButton == 2: print "right click" g.event_manager.post(events.StartTurn()) # End turn is called if isinstance(event, events.ButtonEndTurn): print "Ending turn! YAAAY" self.playersTurn += 1 self.playersTurn %= len(self.players) # Since we're not networked. derp self.currentPlayer = self.playersTurn self.updateCardVisibilities() self.view.updateAll() self.update() g.event_manager.post(events.StartTurn()) if isinstance(event, events.ShipDamage): section = self.board.lanes.index(event.lane) if event.card.model.ownerId == 1: damagedPlayer = 0 if event.card.model.ownerId == 0: damagedPlayer = 1 self.players[damagedPlayer].takeDamage(section, event.card.model.power + event.card.model.attackBonus) if isinstance(event, events.GameOver): self.results.display(not self.players.index(event.player)) def getSuggestedPlay(self): if self.playersTurn == 0: enemyPlayer = 1 else: enemyPlayer = 0 return self.board.getBestLane(enemyPlayer) def updateCardVisibilities(self): for idx, player in enumerate(self.players): if idx == self.currentPlayer: player.setVisibility(True) else: player.setVisibility(False)
# current_screen.show() # # current_screen = GameView(screen, location['num'], location['coords'], account) data = { 'view': 'main', 'position': { 'location': 0, 'coords': (0, 0) }, 'account': '' } while True: if data['view'] == 'game': current_screen = GameView(screen, data) if data['view'] == 'map': current_screen = MapView(screen, data) elif data['view'] == 'esc': current_screen = PauseView(screen, data) elif data['view'] == 'main': current_screen = MainView(screen, data) elif data['view'] == 'intro': current_screen = StoryView(screen, data) data = current_screen.show() else: current_screen = ErrorWindow(screen, 'Connection failed') current_screen.show()
class Game(): def __init__(self, grid_size, max_generation): self.grid_size = grid_size self.grid_size_x, self.grid_size_y = grid_size self.generation = 0 self.max_generation = max_generation self.active_cells = 0 self.game_board = Grid(grid_size, Cell) window_size = tuple([x * 5 for x in grid_size]) self.view = GameView(window_size) self.empty_grid = Grid(self.grid_size, Cell) def run_game(self, frames_per_second=100, display_after_end_of_game=True): """ Begins running the game at an optional delay and persists the window unless display_after_end_of_game is False """ keep_displaying_window = True while (self.generation < self.max_generation) and keep_displaying_window: self.update_generation() keep_displaying_window = self.view.update_screen(self.generation) time.sleep(1 / frames_per_second) while (keep_displaying_window and display_after_end_of_game): keep_displaying_window = self.view.update_screen(self.generation) sys.exit() def update_generation(self): """Checks each cell to see if it survives to the next generation""" next_generation = copy.deepcopy( self.empty_grid ) # create new "dead" grid using deepcopy to avoid added iteration in the grid.__init__() self.view.redraw_screen() # reset screen row_number = 0 for row in self.game_board: cell_number = 0 for cell in row: number_neighbors = self.check_neighboring_living_cells( (row_number, cell_number)) if cell.is_living(): if number_neighbors == 2 or number_neighbors == 3: next_generation.getGridItem( row_number, cell_number).toggle_living( ) # carry over living cell to next generation self.view.draw_cell( (row_number, cell_number)) # draw cell else: if number_neighbors == 3: next_generation.getGridItem( row_number, cell_number ).toggle_living( ) # non-living cell becomes living in the next generation self.view.draw_cell( (row_number, cell_number)) # draw cell cell_number += 1 row_number += 1 self.game_board = next_generation #This generation has ended, copy next generation's grid into current grid self.generation += 1 def check_neighboring_living_cells(self, coordinates): """Check neighboring cells to see if they are living and return the number of living cells""" x, y = coordinates num_neighbors = 0 for neighbor_coordinates in NEIGHBORING_CELLS: neighbor_x, neighbor_y = neighbor_coordinates neighbor_x = (neighbor_x + x) % self.grid_size_x neighbor_y = (neighbor_y + y) % self.grid_size_y if self.game_board.getGridItem(neighbor_x, neighbor_y).is_living(): num_neighbors += 1 return num_neighbors def load_coordinates_into_grid(self, coordinate_list): """Takes a list of coordinates, a tuple(int x, int y), and sets the corresponding cell to living Raises an IndexError if the coordinates are not """ for coordinate in coordinate_list: x, y = coordinate if (x < self.grid_size_x and x >= 0 and y < self.grid_size_y and y >= 0): self.game_board.getGridItem(x, y).toggle_living() else: raise (IndexError) def get_game_board(self): """Returns the current generation's game board""" return self.game_board