python-3.x - 每次在 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”按钮来绘制更多卡片并让卡片出现在屏幕上。关于我应该如何执行这项任务的任何想法?
解决方案
推荐阅读
- angular - 错误:序列中没有元素。Rxjs onErrorResumeNext() 和 first()
- sql - 基于“月”SQL 对行进行分组
- react-native - 在本机反应中将省略号添加到文本输入中的占位符
- kotlin - 尝试在 intellij 中寻找 kotlin 字节码时修复“没有手动输入 byte\;type\=a”的最佳方法
- java - 在 Java 上连接到 MongoDB 的问题
- php - 如何在 PHPWord 中使边框不“细”以防止 Adobe Reader 中“增强细线”设置的行为
- api-gateway - 最小化/消除 OCI API GW 授权的初始 FN 启动延迟
- c++ - 在多线程中运行的同一对象的多个实例,我如何防止所有线程更改全局变量?
- typescript - 'extends'关键字在联合上的奇怪行为
- google-cloud-platform - Cloud SQL 联合查询 - 私有数据库