python+pyqt5实现实现24点小游戏点小游戏
主要为大家详细介绍了python+pyqt5实现24点小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了python实现24点游戏的具体代码,供大家参考,具体内容如下
描述:一副牌中A、J、Q、K可以当成是1、11、12、13。任意抽取4张牌,用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌对应的数字必须用一次且只能用一次。在规定时间内输入算式,
输入正确加十分,输入错误生命值减一,点击确定提交并进入下一题,点击清空可清空算式。点击开始游戏进入游戏,可重新开始游戏。
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QBasicTimer
from PyQt5.QtGui import QPixmap
import sys
class Ui_Form(QWidget):
def setupUi(self,Form):
Form.setObjectName("Form")
Form.resize(946, 515)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(70, 30, 131, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.on_click1)
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(280, 30, 131, 41))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.on_click4)
self.pushButton_4.setEnabled(False)
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(700, 120, 191, 301))
self.textEdit.setObjectName("textEdit")
self.textEdit.setReadOnly(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(70, 300, 111, 31))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(170, 300, 361, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setMaxLength(15)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(70, 100, 101, 171))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(220, 100, 101, 171))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(370, 100, 101, 171))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(510, 100, 101, 171))
self.label_5.setObjectName("label_5")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(170, 360, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.on_click2)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(300, 360, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.on_click3)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(170, 430, 401, 21))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.timer = QBasicTimer()
self.step = 0
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "24点游戏"))
self.pushButton.setText(_translate("Form", "开始游戏"))
self.pushButton_4.setText(_translate("Form", "重新开始"))
self.textEdit.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">"
"p, li { white-space: pre-wrap; }"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">规则:</p>"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">一副牌中A、J、Q、K可以当成是1、11、12、13。任意抽取4张牌,用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌对应的数字必须用一次且只能用一次。在规定时间内输入算式,输入正确加十分,输入错误生命值减一,点击确定提交并进入下一题,点击清空可清空算式。点击开始游戏进入游戏。</p></body></html>"))
self.label.setText(_translate("Form", "请输入算式:"))
self.label_2.setText(_translate("Form", ""))
self.label_3.setText(_translate("Form", ""))
self.label_4.setText(_translate("Form", ""))
self.label_5.setText(_translate("Form", ""))
self.pushButton_2.setText(_translate("Form", "确定"))
self.pushButton_3.setText(_translate("Form", "清空"))
以上为设置界面
import itertools
import random
#已做题目数
self.question_number = 0
# 生命值
self.life = 3
# 分数
self.score = 0
# 重新开始标记
self.recover = 0
# 发牌器 随机抽取四个数字
def cardFun(self):
for i in range(4):
cardNum.append(int(random.random() * 100 % 13) + 1)
self.listSet = list(set(itertools.permutations(cardNum, 4)))
return self.listSet # 存放A(4,4)种排列方式的列表
# 显示四个数字对应的扑克牌
def cardCompute(self):
cardList = self.cardFun()
for i in range(len(cardList)):
self.cardGroup = cardList[i]
self.picture_1()
self.picture_2()
self.picture_3()
self.picture_4()
def fresh(self):