python+pyqt5实现24点小游戏
程序员文章站
2022-06-10 19:57:32
本文实例为大家分享了python实现24点游戏的具体代码,供大家参考,具体内容如下 描述:一副牌中A、J、Q、K可以当成是1、11、12、13。任意抽取4张牌,用加、减、乘、除(可加括号)把牌面上的数算成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\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'simsun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n" "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">规则:</p>\n" "<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): self.lineedit.settext('') self.cardfun() self.cardcompute() # 停止计时 self.progress_bar() # 重新开始计时 self.step = 0 self.step = self.step + 1 self.progressbar.setvalue(self.step) # 开始游戏 def on_click1(self): self.cardfun() self.cardcompute() self.progress_bar() # 将开始游戏按钮设置为不可用 self.pushbutton.setenabled(false) # 将重新开始游戏按钮设置为可用 self.pushbutton_4.setenabled(true) # 确定提交 def on_click2(self): # 已做题目数增加 self.question_number += 1 # 停止计时 self.timer.stop() # 当做到最后一题 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.calstr = self.lineedit.text() if self.calstr == "": self.life -= 1 qmessagebox.information(self, "很遗憾", "算式输入不正确\n进入下一题", qmessagebox.yes) self.fresh() else: self.result = int(eval(self.calstr)) if self.result == 24: self.score += 10 qmessagebox.information(self, "恭喜你", "算式输入正确\n进入下一题", qmessagebox.yes) self.fresh() else: self.life -= 1 qmessagebox.information(self, "很遗憾", "算式输入不正确\n进入下一题", qmessagebox.yes) self.fresh() # 清空 def on_click3(self): self.lineedit.settext('') #重新开始游戏 def on_click4(self): # 初始化已做题数、生命值、分数、时间 self.question_number = 0 self.life = 3 self.score = 0 self.step = 0 self.timer.stop() qmessagebox.information(self, "重新开始游戏", "生命值和分数已重置", qmessagebox.yes) if self.recover == 1: self.pushbutton_2.setenabled(true) self.pushbutton_3.setenabled(true) self.cardfun() self.cardcompute() 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_())
实现各控件的功能
开始界面
点击确定提交,输入错误时会弹出消息框
答题结束时汇总成绩
有问题欢迎评论
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持,关注公众号的更多精彩内容。
上一篇: 穴位按摩快速去痘痘 额头痘按摩曲池穴
下一篇: 足窍阴在哪?常按有什么好处?