时间:2021-05-22
本文实例为大家分享了python实现24点游戏的具体代码,供大家参考,具体内容如下
描述:一副牌中A、J、Q、K可以当成是1、11、12、13。任意抽取4张牌,用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌对应的数字必须用一次且只能用一次。在规定时间内输入算式,输入正确加十分,输入错误生命值减一,点击确定提交并进入下一题,点击清空可清空算式。点击开始游戏进入游戏,可重新开始游戏。
from PyQt5 import QtCore, QtWidgetsfrom PyQt5.QtWidgets import *from PyQt5.QtCore import QBasicTimerfrom PyQt5.QtGui import QPixmapimport sysclass 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://pute() self.timer.start(600, self) self.step = self.step + 1 self.progressBar.setValue(self.step) # 使用一个计时器来启动进度条 def timerEvent(self, e): # 如果时间到,则停止计时,已做题数加一 if self.step >= 100: self.timer.stop() self.question_number += 1 if self.question_number ==3: self.recover = 1 self.calstr = self.lineEdit.text() if self.calstr == "": self.life -= 1 else: self.result = int(eval(self.calstr)) if self.result == 24: self.score += 10 else: self.life -= 1 self.lineEdit.setText('') # 弹出结果消息框 QMessageBox.information(self, "恭喜你", "答题结束\n您的生命值为"+str(self.life)+",分数为"+str(self.score), QMessageBox.Close) self.pushButton_2.setEnabled(False) self.pushButton_3.setEnabled(False) else: self.life -= 1 QMessageBox.information(self, "很遗憾", "规定时间已到\n进入下一题", QMessageBox.Yes) self.fresh() return self.step = self.step + 1 self.progressBar.setValue(self.step) # 进度条 def progress_bar(self): if self.timer.isActive(): self.timer.stop() else: # 规定时间为1分钟 self.timer.start(600,self) # 显示抽取的第一张牌 def picture_1(self): if self.cardGroup[0] == 1: self.label_2.setPixmap(QPixmap('./h1.png')) # 图片路径 elif self.cardGroup[0] == 2: self.label_2.setPixmap(QPixmap('./h2.png')) elif self.cardGroup[0] == 3: self.label_2.setPixmap(QPixmap('./h3.png')) elif self.cardGroup[0] == 4: self.label_2.setPixmap(QPixmap('./h4.png')) elif self.cardGroup[0] == 5: self.label_2.setPixmap(QPixmap('./h5.png')) elif self.cardGroup[0] == 6: self.label_2.setPixmap(QPixmap('./h6.png')) elif self.cardGroup[0] == 7: self.label_2.setPixmap(QPixmap('./h7.png')) elif self.cardGroup[0] == 5: self.label_2.setPixmap(QPixmap('./h8.png')) elif self.cardGroup[0] == 9: self.label_2.setPixmap(QPixmap('./h9.png')) elif self.cardGroup[0] == 10: self.label_2.setPixmap(QPixmap('./h10.png')) elif self.cardGroup[0] == 11: self.label_2.setPixmap(QPixmap('./h11.png')) elif self.cardGroup[0] == 12: self.label_2.setPixmap(QPixmap('./h12.png')) elif self.cardGroup[0] == 13: self.label_2.setPixmap(QPixmap('./h13.png')) # 将扑克牌填充整个标签 self.label_2.setScaledContents(True) # 显示抽取的第二张牌 def picture_2(self): if self.cardGroup[1] == 1: self.label_3.setPixmap(QPixmap('./b1.png')) elif self.cardGroup[1] == 2: self.label_3.setPixmap(QPixmap('./b2.png')) elif self.cardGroup[1] == 3: self.label_3.setPixmap(QPixmap('./b3.png')) elif self.cardGroup[1] == 4: self.label_3.setPixmap(QPixmap('./b4.png')) elif self.cardGroup[1] == 5: self.label_3.setPixmap(QPixmap('./b5.png')) elif self.cardGroup[1] == 6: self.label_3.setPixmap(QPixmap('./b6.png')) elif self.cardGroup[1] == 7: self.label_3.setPixmap(QPixmap('./b7.png')) elif self.cardGroup[1] == 5: self.label_3.setPixmap(QPixmap('./b8.png')) elif self.cardGroup[1] == 9: self.label_3.setPixmap(QPixmap('./b9.png')) elif self.cardGroup[1] == 10: self.label_3.setPixmap(QPixmap('./b10.png')) elif self.cardGroup[1] == 11: self.label_3.setPixmap(QPixmap('./b11.png')) elif self.cardGroup[1] == 12: self.label_3.setPixmap(QPixmap('./b12.png')) elif self.cardGroup[1] == 13: self.label_3.setPixmap(QPixmap('./b13.png')) self.label_3.setScaledContents(True) # 显示抽取的第三张牌 def picture_3(self): if self.cardGroup[2] == 1: self.label_4.setPixmap(QPixmap('./f1.png')) elif self.cardGroup[2] == 2: self.label_4.setPixmap(QPixmap('./f2.png')) elif self.cardGroup[2] == 3: self.label_4.setPixmap(QPixmap('./f3.png')) elif self.cardGroup[2] == 4: self.label_4.setPixmap(QPixmap('./f4.png')) elif self.cardGroup[2] == 5: self.label_4.setPixmap(QPixmap('./f5.png')) elif self.cardGroup[2] == 6: self.label_4.setPixmap(QPixmap('./f6.png')) elif self.cardGroup[2] == 7: self.label_4.setPixmap(QPixmap('./f7.png')) elif self.cardGroup[2] == 5: self.label_4.setPixmap(QPixmap('./f8.png')) elif self.cardGroup[2] == 9: self.label_4.setPixmap(QPixmap('./f9.png')) elif self.cardGroup[2] == 10: self.label_4.setPixmap(QPixmap('./f10.png')) elif self.cardGroup[2] == 11: self.label_4.setPixmap(QPixmap('./f11.png')) elif self.cardGroup[2] == 12: self.label_4.setPixmap(QPixmap('./f12.png')) elif self.cardGroup[2] == 13: self.label_4.setPixmap(QPixmap('./f13.png')) self.label_4.setScaledContents(True) # 显示抽取的第四张牌 def picture_4(self): if self.cardGroup[3] == 1: self.label_5.setPixmap(QPixmap('./m1.png')) elif self.cardGroup[3] == 2: self.label_5.setPixmap(QPixmap('./m2.png')) elif self.cardGroup[3] == 3: self.label_5.setPixmap(QPixmap('./m3.png')) elif self.cardGroup[3] == 4: self.label_5.setPixmap(QPixmap('./m4.png')) elif self.cardGroup[3] == 5: self.label_5.setPixmap(QPixmap('./m5.png')) elif self.cardGroup[3] == 6: self.label_5.setPixmap(QPixmap('./m6.png')) elif self.cardGroup[3] == 7: self.label_5.setPixmap(QPixmap('./m7.png')) elif self.cardGroup[3] == 5: self.label_5.setPixmap(QPixmap('./m8.png')) elif self.cardGroup[3] == 9: self.label_5.setPixmap(QPixmap('./m9.png')) elif self.cardGroup[3] == 10: self.label_5.setPixmap(QPixmap('./m10.png')) elif self.cardGroup[3] == 11: self.label_5.setPixmap(QPixmap('./m11.png')) elif self.cardGroup[3] == 12: self.label_5.setPixmap(QPixmap('./m12.png')) elif self.cardGroup[3] == 13: self.label_5.setPixmap(QPixmap('./m13.png')) self.label_5.setScaledContents(True)if __name__ == "__main__": cardNum = [] # 存放随机牌组 cardGroup = () # 调用牌组 app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QMainWindow() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())实现各控件的功能
开始界面
点击确定提交,输入错误时会弹出消息框
答题结束时汇总成绩
有问题欢迎评论
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持,关注公众号的更多精彩内容。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
之前给大家分享过灭霸命令GitHub地址:https://github.com/hotvulcan/Thanos.sh,本文使用Python+PyQt5实现灭霸
本文实例为大家分享了python+pyqt5编写md5生成器的具体代码,供大家参考,具体内容如下学了一下pyqt5,写一个小程序来实践一下。下图是程序界面:下面
本文实例为大家分享了C语言纸牌24点小游戏的具体实现代码,供大家参考,具体内容如下此程序参考C语言实现经典24点纸牌益智游戏,并做出一些改进。一、题目要求:24
本文实例为大家分享了Java实现24点小游戏的具体代码,供大家参考,具体内容如下程序设计要求:24点游戏是经典的纸牌益智游戏。常见游戏规则:从扑克中每次取出4张
描述:使用QtDesignner设计界面,pyQt5+python3实现主体方法制作的猜数字游戏。游戏规则:先选择游戏等级:初级、中级、高级、魔鬼级,选择完游戏