首页 > 解决方案 > 每次在 PyQt5 中单击按钮时都会发生不同的事件

问题描述

目前,我正在使用 PyQt5 开发 Black Jack 游戏。我正在尝试创建一个“命中”的功能,例如绘制更多卡片,但我不知道如何使其工作。

import sys
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel, QPushButton
import random
from PyQt5.QtCore import Qt


class BlackJack(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):
        # Setup layout management
        grid= QGridLayout()
        self.setLayout(grid)

        # The cards
        heart=[str(i)+'H' for i in range(2,11)]
        heart_sym=['AH','JH','QH','KH']
        heart=heart+heart_sym
        diamond=[str(i)+'D' for i in range(2,11)]
        diamond_sym=['AD','JD','QD','KD']
        diamond=diamond+diamond_sym
        club=[str(i)+'C' for i in range(2,11)]
        club_sym=['AC','JC','QC','KC']
        club=club+club_sym
        spade=[str(i)+'S' for i in range(2,11)]
        spade_sym=['AS','JS','QS','KS']
        spade=spade+spade_sym
        fulldeck=heart+diamond+club+spade

        # Initial Score

        myscorelabel=QLabel('My Score:')
        cpuscorelabel=QLabel('CPU Score:')

        grid.addWidget(myscorelabel,3,0)
        grid.addWidget(cpuscorelabel,2,0)

        # Card Value
        cardvalue={'AH':1,'AD':1,'AC':1,'AS':1,'2H':2,'2D':2,'2C':2,'2S':2,
                    '3H':3,'3D':3,'3C':3,'3S':3,'4H':4,'4D':4,'4C':4,'4S':4,
                    '5H':5,'5D':5,'5C':5,'5S':5,'6H':6,'6D':6,'6C':6,'6S':6,
                    '7H':7,'7D':7,'7C':7,'7S':7,'8H':7,'8D':8,'8C':8,'8S':8,
                    '9H':9,'9D':9,'9C':9,'9S':9,'10H':10,'10D':10,'10C':10,'10S':10,
                    'JH':11,'JD':11,'JC':11,'JS':11,'QH':12,'QD':12,'QC':12,'QS':12,'KH':13,'KD':13,'KC':13,'KS':13
                    }

        # Initial 2 Cards
        def first():
            global cpuscore
            global myscore
            global cpuc1
            global cpuc2
            global myc1
            global myc2
            cpuc1=random.choice(fulldeck)
            fulldeck.remove(cpuc1)
            cpuc2=random.choice(fulldeck)
            fulldeck.remove(cpuc2)
            cpuscore=cardvalue[cpuc1]+cardvalue[cpuc2]

            myc1=random.choice(fulldeck)
            fulldeck.remove(myc1)
            myc2=random.choice(fulldeck)
            fulldeck.remove(myc2)
            myscore=cardvalue[myc1]+cardvalue[myc2]
            print('Hello')
        while True:
            first()
            if cpuscore>21:
                continue
            elif myscore>21:
                continue
            else:

                myscorelabel.setText('My score: {0}'.format(myscore))
                cpuscorelabel.setText('CPU score: {0}'.format(cpuscore))
                break  
        # Game screen
        cpu_c1=QLabel(self)
        cpu_c1pix=QPixmap('E:\\myname\\Cards for Python project\\red_back')
        cpu_c1pix=cpu_c1pix.scaled(200,200, Qt.KeepAspectRatio, Qt.FastTransformation)
        cpu_c1.setPixmap(cpu_c1pix)
        grid.addWidget(cpu_c1,1,0)

        cpu_c2=QLabel(self)
        cpu_c2pix=QPixmap('E:\\myname\\Cards for Python project\\red_back')
        cpu_c2pix=cpu_c2pix.scaled(200,200, Qt.KeepAspectRatio, Qt.FastTransformation)
        cpu_c2.setPixmap(cpu_c1pix)
        grid.addWidget(cpu_c2,1,1)

        my_c1=QLabel(self)
        my_c1pix=QPixmap('E:\\myname\\Cards for Python project\\{0}'.format(myc1))
        my_c1pix=my_c1pix.scaled(200,200, Qt.KeepAspectRatio, Qt.FastTransformation)
        my_c1.setPixmap(my_c1pix)
        grid.addWidget(my_c1,4,0)

        my_c2=QLabel(self)
        my_c2pix=QPixmap('E:\\myname\\Cards for Python project\\{0}'.format(myc2))
        my_c2pix=my_c2pix.scaled(200,200, Qt.KeepAspectRatio, Qt.FastTransformation)
        my_c2.setPixmap(my_c2pix)
        grid.addWidget(my_c2,4,1)

        self.bhit=QPushButton('Hit')
        self.bhit.clicked.connect(self.hit)
        grid.addWidget(self.bhit,5,0)

        self.bstand=QPushButton('Stand')
        self.bstand.clicked.connect(self.stand)
        grid.addWidget(self.bstand,5,1)

        # Window's essentials
        self.setGeometry(500,200,1000,500)
        self.setWindowTitle('Black Jack')
        self.setWindowIcon(QIcon('E:\\myname\\blackjackicon'))
        self.show()

    def hit(self):            
        print('You hit')
    def stand(self):
        print('You stand')


if __name__ == '__main__':

    app = QApplication(sys.argv)
    BlackJack = BlackJack()
    sys.exit(app.exec_())

我想要做的是使用“bhit”按钮来绘制更多卡片并让卡片出现在屏幕上。关于我应该如何执行这项任务的任何想法?

标签: python-3.xpyqt5blackjack

解决方案


推荐阅读