Python Forum
[PyQt] computerScore counter not found
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] computerScore counter not found
#2
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}')
Maryan likes this post
Reply


Messages In This Thread
computerScore counter not found - by Maryan - Nov-16-2020, 12:43 AM
RE: computerScore counter not found - by deanhystad - Nov-16-2020, 02:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020