python+pyqt5实现24点小游戏


Posted in Python onJanuary 24, 2019

本文实例为大家分享了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_())

实现各控件的功能

开始界面

python+pyqt5实现24点小游戏

点击确定提交,输入错误时会弹出消息框

python+pyqt5实现24点小游戏

答题结束时汇总成绩

python+pyqt5实现24点小游戏

有问题欢迎评论

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木,关注三水点靠木公众号的更多精彩内容。

Python 相关文章推荐
python使用BeautifulSoup分页网页中超链接的方法
Apr 04 Python
django定期执行任务(实例讲解)
Nov 03 Python
Python2.7+pytesser实现简单验证码的识别方法
Dec 29 Python
Python SqlAlchemy动态添加数据表字段实例解析
Feb 07 Python
Python使用 Beanstalkd 做异步任务处理的方法
Apr 24 Python
Python参数解析模块sys、getopt、argparse使用与对比分析
Apr 02 Python
python使用sessions模拟登录淘宝的方式
Aug 16 Python
python sklearn常用分类算法模型的调用
Oct 16 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
在keras里面实现计算f1-score的代码
Jun 15 Python
Python中过滤字符串列表的方法
Dec 22 Python
Python中re模块的元字符使用小结
Apr 07 Python
python中实现控制小数点位数的方法
Jan 24 #Python
对python以16进制打印字节数组的方法详解
Jan 24 #Python
python3实现点餐系统
Jan 24 #Python
使用Python批量修改文件名的代码实例
Jan 24 #Python
Python并发:多线程与多进程的详解
Jan 24 #Python
python用opencv批量截取图像指定区域的方法
Jan 24 #Python
python+pyqt5实现KFC点餐收银系统
Jan 24 #Python
You might like
PHP错误抑制符(@)导致引用传参失败Bug的分析
2011/05/02 PHP
php中遍历二维数组并以表格的形式输出的方法
2017/01/03 PHP
PHP+ajax实现二级联动菜单功能示例
2018/08/10 PHP
PHP pthreads v3下同步处理synchronized用法示例
2020/02/21 PHP
javascript下arguments,caller,callee,call,apply示例及理解
2009/12/24 Javascript
a标签的href和onclick 的事件的区别介绍
2013/07/26 Javascript
js去除输入框中所有的空格和禁止输入空格的方法
2014/06/09 Javascript
javascript生成大小写字母
2015/07/03 Javascript
JS中script标签defer和async属性的区别详解
2016/08/12 Javascript
详解Nodejs基于mongoose模块的增删改查的操作
2016/12/21 NodeJs
小程序分享模块超级详解(推荐)
2019/04/10 Javascript
eslint 的三大通用规则详解
2019/05/16 Javascript
个人小程序接入支付解决方案
2019/05/23 Javascript
浅谈element中InfiniteScroll按需引入的一点注意事项
2020/06/05 Javascript
Vue单文件组件开发实现过程详解
2020/07/30 Javascript
[01:01:14]完美世界DOTA2联赛PWL S2 SZ vs Rebirth 第一场 11.21
2020/11/23 DOTA
Unicode和Python的中文处理
2017/03/19 Python
python实现的AES双向对称加密解密与用法分析
2017/05/02 Python
Python+request+unittest实现接口测试框架集成实例
2018/03/16 Python
Python简单获取网卡名称及其IP地址的方法【基于psutil模块】
2018/05/24 Python
python re.sub()替换正则的匹配内容方法
2019/07/22 Python
Python图像处理之图片文字识别功能(OCR)
2019/07/30 Python
Django REST 异常处理详解
2020/07/15 Python
python 爬虫请求模块requests详解
2020/12/04 Python
工商管理系学生的自我评价分享
2013/11/29 职场文书
简历的自我评价
2014/02/03 职场文书
暑期培训随笔感言
2014/03/10 职场文书
一年级评语大全
2014/04/23 职场文书
新学期国旗下演讲稿
2014/05/08 职场文书
俞敏洪一分钟演讲稿
2014/08/26 职场文书
大一工商管理职业生涯规划:有梦最美,行动相随
2014/09/18 职场文书
小学生校园广播稿
2014/09/28 职场文书
入党积极分子对十八届四中全会期盼的思想汇报
2014/10/17 职场文书
党的群众路线教育实践活动个人对照检查材料(校长)
2014/11/05 职场文书
大学生毕业评语
2014/12/31 职场文书
社会治安综合治理责任书
2015/01/29 职场文书