def __init__(self, parent, trackName, player1, player2): QtGui.QFrame.__init__(self) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.trackName = trackName self.player1Name = player1 self.player2Name = player2 # Create a layout self.layout = QtGui.QGridLayout() self.layout.setMargin(1) self.layout.setSpacing(1) # Load the formula game from Formulagame -class self.game = Formulagame(str(trackName), str(player1), str(player2)) # Initialize the needed variables self.oldNextPlayerMoves = [] self.helpWindow = None self.popUpWindow = None grid = self.game.getGrid() for row in range(len(grid)): for column in range(len(grid[row])): label = QtGui.QLabel(grid[row][column]) self.layout.addWidget( label, column, row ) self.setLayout(self.layout)
class GameArea(QtGui.QFrame): def __init__(self, parent, trackName, player1, player2): QtGui.QFrame.__init__(self) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.trackName = trackName self.player1Name = player1 self.player2Name = player2 # Create a layout self.layout = QtGui.QGridLayout() self.layout.setMargin(1) self.layout.setSpacing(1) # Load the formula game from Formulagame -class self.game = Formulagame(str(trackName), str(player1), str(player2)) # Initialize the needed variables self.oldNextPlayerMoves = [] self.helpWindow = None self.popUpWindow = None grid = self.game.getGrid() for row in range(len(grid)): for column in range(len(grid[row])): label = QtGui.QLabel(grid[row][column]) self.layout.addWidget( label, column, row ) self.setLayout(self.layout) # keyPressEvent methdod listens the key presses, calls the Formulagame.makeMove() -method and refreshes the window def keyPressEvent(self, e): if 56 >= e.key() >= 49: if self.game.getProgress() == True: move, nextPlayerMoves = self.game.makeMove(e.key() - 48) if self.game.getProgress() == True: self.refreshWindow(move, nextPlayerMoves) else: if move == "hit": finalText = "Player %s hit the other player. His car broke and he lost the game!" % (str(nextPlayerMoves)) elif move == "wall": finalText = "Player %s hit the wall! The car broke down and he lost the game!" % (str(nextPlayerMoves)) elif move == "win": finalText = "Player %s won the race! Winners name and laps is saved to the results.txt -file!" % (str(nextPlayerMoves[0])) self.saveResults(self.trackName, nextPlayerMoves[0], nextPlayerMoves[1]) self.popUpWindow = PopUpWindow(finalText) # Save the results to the file def saveResults(self, trackName, playerName, moves): f = open('results.txt', 'r+') f.seek(0,2) f.write(str(trackName)+ ": " + str(playerName) + " , " + str(moves) + " moves\n") f.close() # A method to refresh the window between the moves def refreshWindow(self, points, newPlayerPoints): try: # Remove the previous player's possible points from the track for i in self.oldNextPlayerMoves: square = str(self.game.getSquare(i[0], i[1])) self.layout.itemAtPosition(i[1], i[0]).widget().setText(square) # Refresh the square which player drove by for i in range(len(points)): if i == (len(points)): square = str(self.game.getSquare(points[i][0], points[i][1])) self.layout.itemAtPosition(points[i][1], [i][0]).widget().setText(square) # Mark the squares which the next player can drive to for i in range(len(newPlayerPoints)): self.layout.itemAtPosition(newPlayerPoints[i][1], newPlayerPoints[i][0]).widget().setText(str(i+1)) self.oldNextPlayerMoves = newPlayerPoints except AttributeError: pass