def has_game_ended(self): #check if the game has ended. status_list = [button["status"] for button in self.registered_buttons.get_button_dict().values()] if True in status_list: #the game has not ended yet return else: #the game has ended. Show a message with the game outcome. game_outcome = analyze_game_outcome(str(get_game_setting("player_one_name")), str(get_game_setting("player_two_name")), str(self.memory_match.get_player_one_score()),str(self.memory_match.get_player_two_score())) self.finished_game_message.setText(game_outcome) self.finished_game_message.show() # disable the next_turn_button self.next_turn_button.setEnabled(False) # set the turn back to zero self.memory_match.set_turn_to_zero() # remove the clicked buttons from the previous turn self.memory_match.remove_clicked_buttons() #set the scores 0 for both player_one and player_two and update the labels to this new score. self.memory_match.set_player_one_score_to_zero() self.memory_match.set_player_two_score_to_zero() self.player_one_score.setText("Player: "+str(get_game_setting("player_one_name"))+", score: "+str(self.memory_match.get_player_one_score())) self.player_two_score.setText("Player: "+str(get_game_setting("player_two_name"))+", score: "+str(self.memory_match.get_player_two_score())) #make player_one the first player for coming game self.memory_match.set_current_player("player_one") #Add plus one to the finished games statistic update_game_statistics("finished_games") #Refresh the contents of the buttons self.update_content_buttons()
def set_current_player(self, current_player): if current_player == "player_one": #time to set the new current player to player_two self.memory_match.set_current_player("player_two") self.current_playing_player.setText("Current playing player: " + str(get_game_setting("player_two_name"))) elif current_player == "player_two": #time to set the new current player to player_one self.memory_match.set_current_player("player_one") self.current_playing_player.setText("Current playing player: " + str(get_game_setting("player_one_name")))
def accumulate_point_to_current_player(self): if self.memory_match.get_current_player() == "player_one": self.memory_match.accumulate_player_one_score() self.player_one_score.setText("Player: " + str(get_game_setting("player_one_name")) + ", score: " + str( self.memory_match.get_player_one_score())) else: self.memory_match.accumulate_player_two_score() self.player_two_score.setText("Player: " + str(get_game_setting("player_two_name")) + ", score: " + str( self.memory_match.get_player_two_score()))
def update_grid(self): current_column_count, current_row_count = get_row_and_colum(int(get_game_setting("playfield_size"))) current_positions = [(column, row) for column in range(int(current_column_count)) for row in range(int(current_row_count))] self.registered_buttons.initialize_button_dict() self.remove_grid_buttons() self.add_buttons_to_grid(current_positions)
def initialize_match_settings(self): #set the memory_match object with the default beginning values and initialize the playfield names column_count, row_count = get_row_and_colum(int(get_game_setting("playfield_size"))) self.memory_match = MemoryMatch("player_one") self.registered_buttons = RegisterButtons(column_count, row_count) self.registered_buttons.initialize_button_dict() #accumulate +1 for started games. update_game_statistics("started_games")
def create_playfield(self): #create a matrix of buttons which will be the playing field. self.grid = QGridLayout() columns, rows = get_row_and_colum(int(get_game_setting("playfield_size"))) positions = [(column, row) for column in range(int(columns)) for row in range(int(rows))] self.add_buttons_to_grid(positions) return self.grid
def initUI(self): #create widgets self.current_playing_player = QLabel("Current playing player: "+str(get_game_setting("player_one_name"))) self.player_one_score = QLabel("Player: "+str(get_game_setting("player_one_name"))+", score: "+str(self.memory_match.get_player_one_score())) self.player_two_score = QLabel("Player: "+str(get_game_setting("player_two_name"))+", score: "+str(self.memory_match.get_player_two_score())) self.next_turn_button = QPushButton("Next turn") self.next_turn_button.setEnabled(False) self.next_turn_button.clicked.connect(self.next_turn) self.go_back_to_setup_button = QPushButton("Go to Setup") self.finished_game_message = QMessageBox() self.finished_game_message.setWindowTitle("The memory game is finished") self.finished_game_message.setText("") self.finished_game_message.setStandardButtons(QMessageBox.Ok) horizontal_go_back_to_setup_box = QHBoxLayout() horizontal_go_back_to_setup_box.addWidget(self.go_back_to_setup_button) horizontal_go_back_to_setup_box.addStretch() self.grid = self.create_playfield() horizontal_playing_player_box = QHBoxLayout() horizontal_playing_player_box.addWidget(self.current_playing_player) horizontal_playing_player_box.addStretch() horizontal_playing_player_box.addWidget(self.next_turn_button) horizontal_player_scores_box = QHBoxLayout() horizontal_player_scores_box.addWidget(self.player_one_score) horizontal_player_scores_box.addStretch() horizontal_player_scores_box.addWidget(self.player_two_score) self.vertical_box = QVBoxLayout() self.vertical_box.insertLayout(1, horizontal_go_back_to_setup_box) self.vertical_box.insertLayout(2, self.grid) self.vertical_box.insertLayout(3, horizontal_playing_player_box) self.vertical_box.insertLayout(4, horizontal_player_scores_box) self.setWindowTitle(self.title) self.setLayout(self.vertical_box) self.setGeometry(self.left, self.top, self.width, self.height)
def get_X_different_names(self, column, row): names = get_game_setting("game_card_names") #names = ["fish","cat","tiger","moon","sun","lion","river","ocean","peanut","elephant","monkey", "dolphin","jaguar","sand","tundra","hyena","treasure","diamond","ruby","emerald","potasium","camel","horse","leaf","knight","king","emperor","ocean","sea","boat","pirate","ninja","tree"] total_names_devided_by_two = (column * row) / 2 unique_names = [] used_integers = [] while len(unique_names) != int(total_names_devided_by_two): random_integer = random.randint(0, len(names) - 1) if not random_integer in used_integers: name = names[random_integer] unique_names.append(name) used_integers.append(random_integer) unique_names = unique_names + unique_names return sorted(unique_names, key=lambda k: random.random())