Teach you how to develop a "Rock Paper Scissors" game with Python [source code attached]

Teach you how to develop a "Rock Paper Scissors" game with Python [source code attached]

/1 Introduction/

Recently I am learning PyQt5 visual interface, which is a very rich gui library. Compared with the tkinter library, it has more powerful functions, more beautiful interface, and easy operation. So I started to try my best and use PyQt5 to make a visual "rock-paper-scissors" game.

This program can realize game round statistics, both sides' scores, the number of draws, and the restart function. Whether it is 1 game decider, or two wins in three games, and three wins in seven games, it can be solved perfectly.

In the middle grid of the second row, the scores of both red and blue are placed.

Now the basic framework has been built.

When you click the cross in the upper right corner, you will be prompted, and the effect is shown in the following figure:

At this point, this little game is finished, the interface is relatively simple, and interested friends can continue to develop more advanced functions.

For beginners who want to learn Python web development, Python crawler technology, Python data analysis, artificial intelligence and other technologies more easily, here is also a zero-based system learning tutorial for everyone, plus the Python technology learning tutorial built by the teacher qq skirt: 245345507 , Get it for free.

Or add teacher WeChat ID: XTUOL1988 [Remarks to learn Python]

If you have any questions during the learning process, there are practical teachers who have been engaged for many years to answer questions for free! Let you really learn Python and find a job that you are satisfied with!

The last is the source code, which is a bit long

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import random
class red(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(160, 120)#self.resize(40, 30)
        global rednum
        rednum = int(random.random() * 3) # random
        print(rednum)
        choices = ['redcloth.jpg','redscissors.jpg','redstone.jpg']
        file=str(choices[int(rednum)])
        self.setStyleSheet("QPushButton{border-image: url(%s)}"%(file))
class blue(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(160, 120)#self.resize(40, 30)
        global bluenum
        bluenum = int(random.random() * 3) # random
        choices = ['bluecloth.jpg','bluescissors.jpg','bluestone.jpg']
        file=str(choices[int(bluenum)])
        self.setStyleSheet("QPushButton{border-image: url(%s)}"%(file))
class GameWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.virusnum=0
        self.setWindowTitle("Rock-paper-scissors game")
        self.setWindowIcon(QIcon('icon.jpg'))
        self.gametimes=0
        self.redscores=0
        self.bluescores = 0
        self.tietimes=0
        self.imagelayout = QGridLayout()
        self.setLayout(self.imagelayout) # Partial layout

        self.redText = QTextBrowser()
        self.redText.setText('Red player')
        self.redText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.redText, 0, 0, Qt.AlignCenter)

        self.actionText = QTextBrowser()
        self.actionText.setText('Round 0')
        self.actionText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.actionText, 0, 1, Qt.AlignCenter)

        self.blueText = QTextBrowser()
        self.blueText.setText('Blue player')
        self.blueText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.blueText, 0, 2, Qt.AlignCenter)

        self.startPushButton = QPushButton("Start the game")
        self.startPushButton.setFixedSize(60, 40)
        self.startPushButton.clicked.connect(self.gamestart)
        self.imagelayout.addWidget(self.startPushButton, 2, 1, Qt.AlignCenter)

        self.restartPushButton = QPushButton("Restart")
        self.restartPushButton.setFixedSize(60, 40)
        self.restartPushButton.clicked.connect(self.restart)
        self.imagelayout.addWidget(self.restartPushButton, 2, 2, Qt.AlignCenter)
        #红方Picture Layout
        self.redlayout = QGridLayout() # Grid layout
        self.redWidget = QWidget()
        self.redWidget.setFixedSize(300, 200)
        self.imagelayout.addWidget(self.redWidget, 1, 0)
        self.redWidget.setLayout(self.redlayout)

        #Score layout
        self.scorelayout = QGridLayout() # Grid layout
        self.scoreWidget = QWidget()
        self.scoreWidget.setFixedSize(300, 200)
        self.imagelayout.addWidget(self.scoreWidget, 1, 1)
        self.scoreWidget.setLayout(self.scorelayout)
        self.scoreRedText = QTextBrowser()
        self.scoreRedText.setText('Red score: {} points'.format(self.redscores))
        self.scoreRedText.setFixedSize(100, 30)
        self.scorelayout.addWidget(self.scoreRedText, 0, 0)

        self.scoreVsText = QTextBrowser()
        self.scoreVsText.setText('VS')
        self.scoreVsText.setFixedSize(30, 30)
        self.scorelayout.addWidget(self.scoreVsText, 0, 1)

        self.scoreBlueText = QTextBrowser()
        self.scoreBlueText.setText('Blue party score: {} points'.format(self.bluescores))
        self.scoreBlueText.setFixedSize(100, 30)
        self.scorelayout.addWidget(self.scoreBlueText, 0, 2)

        self.tieText = QTextBrowser()
        self.tieText.setText('Number of draws: {} times'.format(self.tietimes))
        self.tieText.setFixedSize(100, 50)
        self.scorelayout.addWidget(self.tieText, 1, 0, 1, 3, Qt.AlignCenter)

        #蓝方Picture Layout
        self.bluelayout = QGridLayout() # Grid layout
        self.blueWidget = QWidget()
        self.blueWidget.setFixedSize(300, 200)
        self.imagelayout.addWidget(self.blueWidget, 1, 2)
        self.blueWidget.setLayout(self.bluelayout)

    def gamestart(self):
        self.gametimes+= 1
        self.actionText.setText('Round {0}'.format(self.gametimes))
        self.redlayout.addWidget(red(), 0, 0)
        self.bluelayout.addWidget(blue(), 0, 0)
        self.judge()
    def judge(self):
        if rednum==0:
            if bluenum==0:
                self.tietimes+=1
                self.tieText.setText('Number of draws: {} times'.format(self.tietimes))
            elif bluenum==1:
                self.bluescores+=1
                self.scoreBlueText.setText('Blue party score: {} points'.format(self.bluescores))
            elif bluenum==2:
                self.redscores += 1
                self.scoreRedText.setText('Red score: {} points'.format(self.redscores))
        elif rednum==1:
            if bluenum == 0:
                self.redscores += 1
                self.scoreRedText.setText('Red score: {} points'.format(self.redscores))
            elif bluenum == 1:
                self.tietimes+=1
                self.tieText.setText('Number of draws: {} times'.format(self.tietimes))
            elif bluenum == 2:
                self.bluescores += 1
                self.scoreBlueText.setText('Blue party score: {} points'.format(self.bluescores))
        elif rednum==2:
            if bluenum == 0:
                self.bluescores += 1
                self.scoreBlueText.setText('Blue party score: {} points'.format(self.bluescores))
            elif bluenum == 1:
                self.redscores += 1
                self.scoreRedText.setText('Red score: {} points'.format(self.redscores))
            elif bluenum == 2:
                self.tietimes+=1
                self.tieText.setText('Number of draws: {} times'.format(self.tietimes))
    def restart(self):
        self.gametimes=0
        self.redscores=0
        self.bluescores=0
        self.tietimes=0
        self.actionText.setText('Round {0}'.format(self.gametimes))
        self.scoreRedText.setText('Red score: {} points'.format(self.redscores))
        self.scoreBlueText.setText('Blue party score: {} points'.format(self.bluescores))
        self.tieText.setText('Number of draws: {} times'.format(self.tietimes))

    def closeEvent(self, event):
        reply = QMessageBox.question(self,'Message',
                                     "Are you sure to quit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
class Game(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Rock-paper-scissors game")
        self.setWindowIcon(QIcon('icon.jpg'))
        self.virusnum = 0
        self.gametimes = 0
        self.redscores = 0
        self.bluescores = 0
        self.tietimes = 0
        self.imagelayout = QGridLayout()
        self.setLayout(self.imagelayout) # Partial layout

        self.redText = QTextBrowser()
        self.redText.setText('Red player')
        self.redText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.redText, 0, 0, Qt.AlignCenter)

        self.actionText = QTextBrowser()
        self.actionText.setText('Round 0')
        self.actionText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.actionText, 0, 1, Qt.AlignCenter)

        self.blueText = QTextBrowser()
        self.blueText.setText('Blue player')
        self.blueText.setFixedSize(70, 50)
        self.imagelayout.addWidget(self.blueText, 0, 2, Qt.AlignCenter)
app = QApplication(sys.argv)
gameshow = GameWindow()
#gameshow=Game()
gameshow.setFixedSize(900, 400)
gameshow.show()
sys.exit(app.exec_())
Reference: https://cloud.tencent.com/developer/article/1669454 Teach you how to develop a "Rock Paper Scissors" game with Python [with source code]-Cloud+Community-Tencent Cloud