[PyQt] computerScore counter not found - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] computerScore counter not found (/thread-30971.html) |
computerScore counter not found - Maryan - Nov-16-2020 It's weird, I can't locate the mistake. If the player wins the round counter is ok, if the computer wins the score is not changing. import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QFont, QPixmap from PyQt5.QtCore import QTimer from random import randint textFont = QFont('Helvetica', 12) computerScore = 0 playerScore = 0 class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle('Rock Paper Scissors Game') self.setFixedSize(570,370) self.UI() def UI(self): # Computer & Player Score self.scoreComputer = QLabel('Computer score ', self) self.scoreComputer.move(30,20) self.scoreComputer.setFont(textFont) self.scorePlayer = QLabel('Player score ', self) self.scorePlayer.move(400,20) self.scorePlayer.setFont(textFont) # Computer & Player Images self.imageComputer = QLabel(self) self.imageComputer.setPixmap(QPixmap('RockPaperScissors\images\paper.png')) self.imageComputer.move(10,100) self.imagePlayer = QLabel(self) self.imagePlayer.setPixmap(QPixmap('RockPaperScissors\images\paper.png')) self.imagePlayer.move(350,100) # VS images self.imageVS = QLabel(self) self.imageVS.setPixmap(QPixmap('RockPaperScissors\images\game.png')) self.imageVS.move(240,130) # Buttons self.btnStart = QPushButton('Start', self) self.btnStart.move(150, 330) self.btnStart.clicked.connect(self.start) self.btnStop = QPushButton('Stop', self) self.btnStop.move(235, 330) self.btnStop.clicked.connect(self.stop) self.btnNewGame = QPushButton('New Game', self) self.btnNewGame.move(320, 330) self.btnNewGame.clicked.connect(self.newGame) # Timer self.timer = QTimer(self) self.timer.setInterval(100) self.timer.timeout.connect(self.playGame) self.show() def start(self): self.timer.start() def playGame(self): self.rndComputer = randint(1,3) if self.rndComputer == 1: self.imageComputer.setPixmap(QPixmap('RockPaperScissors\\images\\rock.png')) elif self.rndComputer == 2: self.imageComputer.setPixmap(QPixmap('RockPaperScissors\\images\\paper.png')) else: self.imageComputer.setPixmap(QPixmap('RockPaperScissors\\images\\scissors.png')) self.rndPlayer = randint(1,3) if self.rndPlayer == 1: self.imagePlayer.setPixmap(QPixmap('RockPaperScissors\\images\\rock.png')) elif self.rndPlayer == 2: self.imagePlayer.setPixmap(QPixmap('RockPaperScissors\\images\\paper.png')) else: self.imagePlayer.setPixmap(QPixmap('RockPaperScissors\\images\\scissors.png')) def gameLogic(self): global computerScore global playerScore if self.rndComputer == 1 and self.rndPlayer == 1: mbox = QMessageBox.information(self, 'Result','Draw Game') elif self.rndComputer == 1 and self.rndPlayer == 2: mbox = QMessageBox.information(self, 'Result','You Won') playerScore += 1 self.scorePlayer.setText('Your score {}'.format(playerScore)) elif self.rndComputer == 1 and self.rndPlayer == 3: mbox = QMessageBox.information(self, 'Result','Computer Won') computerScore += 1 self.scoreComputer.setText('Computer score {}'.format(computerScore)) elif self.rndComputer == 2 and self.rndPlayer == 1: mbox = QMessageBox.information(self, 'Result','Computer Won') computerScore += 1 self.scoreComputer.setText('Computer score {}'.format(computerScore)) elif self.rndComputer == 2 and self.rndPlayer == 2: mbox = QMessageBox.information(self, 'Result','Draw Game') elif self.rndComputer == 2 and self.rndPlayer == 3: mbox = QMessageBox.information(self, 'Result','You Won') playerScore += 1 self.scorePlayer.setText('Your score {}'.format(playerScore)) elif self.rndComputer == 3 and self.rndPlayer == 1: mbox = QMessageBox.information(self, 'Result','You Won') playerScore += 1 self.scorePlayer.setText('Your score {}'.format(playerScore)) elif self.rndComputer == 3 and self.rndPlayer == 2: mbox = QMessageBox.information(self, 'Result','Computer Won') computerScore += 1 self.scoreComputer.setText('Computer score {}'.format(computerScore)) elif self.rndComputer == 3 and self.rndPlayer == 3: mbox = QMessageBox.information(self, 'Result','Draw Game') if computerScore == 3 or playerScore == 3: mbox = QMessageBox.information(self, 'Info','Game Over. Computer Score is {}, Player Score is {}'.format(computerScore,playerScore)) self.btnStart.setEnabled(False) self.btnStop.setEnabled(False) def stop(self): self.timer.stop() self.gameLogic() def newGame(self): global computerScore global playerScore computerScore = 0 playerScore = 0 self.btnStart.setEnabled(True) self.btnStop.setEnabled(True) self.start() def main(): App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) if __name__ == '__main__': main() RE: computerScore counter not found - deanhystad - Nov-16-2020 The score is updated, but the computer score label is too narrow to see the score. The code for displaying images could be made much shorter if you saved the image files in a collection and used the rndPlayer and rndComputer values as an index. def playGame(self): imageFiles = ('rock.png', 'scissors.png', 'paper.png') self.rndComputer = randint(0, 2) # Why using 1..3? self.imageComputer.setPixmap(QPixmap(imageFiles[self.rndComputer])) self.rndPlayer = randint(0, 2) # Using 0..2 lets me use as index in imageFiles self.imagePlayer.setPixmap(QPixmap(imageFiles[self.rndPlayer])) if self.rndComputer == self.rndPlayer: # A draw if computer and player are same mbox = QMessageBox.information(self, 'Result','Draw Game') elif (self.rndComputer + 1) % 3 == self.rndPlayer: # Player wins mbox = QMessageBox.information(self, 'Result','You Won') self.playerScore += 1 self.scorePlayer.setText(f'Player {self.playerScore}') else: # Computer wins mbox = QMessageBox.information(self, 'Result','Computer Won') self.computerScore += 1 self.scoreComputer.setText(f'Computer {self.computerScore}')The logic to decide who wins can also be cleaned up quite a bit. The game is a draw if rndPlayer == rndComputer. You do not have to check for them both == 1 and both == 2 and both == 3. You have the player winning if computer, player == 1,2, 2,3, 3,1. This can be written as an equation. player wins if (rndComputer + 1) % 3 == rndPlayer. You might also want to think about eliminating the draws. This speeds up play and simplifies the win logic even more. In this example I used a win list instead of an equation. Something like this is easily expanded to let you play rock-paper-scissor-lizard-spock. def playGame(self): imageFiles = ('rock.png', 'scissors.png', 'paper.png') roll = random.sample(range(3), k=2) self.imageComputer.setPixmap(QPixmap(imageFiles[roll[0]])) self.imagePlayer.setPixmap(QPixmap(imageFiles[roll[1]])) if roll in [[0,1], [1,2], [2,0]]: mbox = QMessageBox.information(self, 'Result','You Won') self.playerScore += 1 else: mbox = QMessageBox.information(self, 'Result','Computer Won') self.computerScore += 1 self.scorePlayer.setText(f'Player {self.playerScore}') self.scoreComputer.setText(f'Computer {self.computerScore}') |