目录 史上最强之Pyqt写游戏 潜水了大约半个月,这半个月实在抽不出时间来写一下博客,因为最近确实有点忙,不仅在练习微信小程序,有空还接了一下一些人的程序设计,虽然很多都是小打小闹,但是也给了我很多的乐趣,说到底的还真是面相¥编程,有了目标驱动,动力就足,干啥啥都有劲,以至于现在停下来感觉都快半身不遂了。编程快虽乐,还是要谨慎而行啊! 接下来这几天有空的话我一下这几个星期的积累,也算是经验,比如下面上线的这个微信小程序,用python做后台以及前端用vant做框架,还有一些小游戏,小系统,总的来说每一个都可以是一个简单的课程或者毕业小设计。 好了,闲话不多聊,下面回归正题。以前用python做GUI的时候,总是使用tkinter,因为感觉它比较简单方便,有兴趣的同学可以去我的python小项目栏目下看看,里面以前的项目基本都是tkinter做的,在这过程中也偶尔听说pyqt比tkinter好玩多了,但是当时并未在意,想着框架不在多,自己用着好就是好。但是好奇心驱使,最近接了个小设计,一个类似小游戏的东西,大概意思就是两个人分别点击两张图片,如果图片相同,则该玩家加一分,继续翻,如果不相同,则换下一个玩家。 一开始没有什么思路,想着游戏应该用pygame来做,但是鄙人对pygame不熟悉,所以放弃了,后面脑子想了一下用tkinter来做行不行,试了下太复杂了,又pass,最后想到还有个pyqt没玩过,为什么不试试呢?二话不想开始装库,这过程就不赘述了,ImapBox上有一堆教程,还有界面设计designer的使用,也不多赘述,网上有教程,我放出来给大家了,由于一开始我参考的那两篇博客不知道是哪个了,我这边就说一下我踩的坑。 打开后就是这样的界面,当然你们的应该是创建界面,我这个是我这个项目已经设计好的页面,至于为啥我的是中文,网上也有教程,不赘述,找百度。(说句悄悄话,英语太差,没办法。不过pyqt确实比tkinter好用,能拖拉设计。。。。) 设计完后找到你的.ui文件导出成py文件,命令就是下面这行,注意要进入到存放ui文件的目录,或者你直接加个绝对路径。 class Ui_MainWindow这个类就是你导出成py文件后他自动给你生成的, 我们需要下面这个调用它,俗称搞对象。 不出意外的话你就会得到下面这样的情况,没错,这就是你的对象,只有躯体,没有灵魂的!而我们真正要做的就是给他加上灵魂。 首先给按钮穿上衣服—-》》self.pushButton_1.setStyleSheet(“QPushButton{border-image: url(mr/1.jpg)}”) 然后给每个按钮绑定一个事件—-》》self.pushButton_1.clicked.connect(lambda:self.setimg(“1”)) 最后加个背景音乐,完事。 需要注意的是,起初我是想每个按钮绑定一个事件,后面发现太复杂,就想到全部按钮绑定到同一个事件,每次点击按钮时传入该按钮的编号,然后再将字符转换为变量—》》buttonname=”self.pushButton_%s”%name buttonname=eval(buttonname),用来更换不同的背景图片,也就是给你的对象换衣服。 随机选择列表内的一个元素赋值给b,然后图片b给到当前按钮,从图片列表删除b元素,因为一开始b是空的,当第二次点击的时候我们判断当前随机获取到的元素是否与b相同,不同则换人,同就继续,该玩家积分加一 项目虽小,但是一开始并没有思路,当逻辑弄清楚后,我们再转为代码就容易多了,所以我们在做东西的时候应该先脑子里有个整体思路,不然会白白浪费大把时间,写出的东西还一团糟。 其实该代码还能再简化一下,每个按钮都是类似的,我们可以循环生成,但是要将x,y搞清楚怎么分布的,这样我们就能生成32个或者64个或者更多按钮,就不限于16个了,至于按钮排布规律留给你们去想了,小白撰写不易,嘴下留情,点个赞吧!
项目展示
前言
项目来源
pip install PyQt5 -i https://pypi.doubanio.com/simple pip install pyqt5-tools -i https://pypi.doubanio.com/simple
pip install pyqt5-tools -i https://pypi.doubanio.com/simple
pyuic5 -o name.py name.ui
最后代码
# %%writefile game.py from PyQt5.QtWidgets import QApplication, QMainWindow,QGridLayout from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia from PyQt5.QtCore import QUrl, Qt import sys import os,random,win32api,win32con global a a=[] for root, dirs, files in os.walk('./img'): a=files*2 #当前路径下所有非目录子文件 global b b='' global d d=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"] global count,play_1,play_2 count=0 play_1=0 play_2=0 class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1047, 813) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton_1 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_1.setGeometry(QtCore.QRect(20, 10, 191, 161)) self.pushButton_1.setObjectName("pushButton") self.pushButton_1.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_1.clicked.connect(lambda:self.setimg("1")) self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(830, 10, 191, 161)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_2.clicked.connect(lambda:self.setimg("2")) self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(570, 10, 191, 161)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_3.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_3.clicked.connect(lambda:self.setimg("3")) self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(290, 10, 191, 161)) self.pushButton_4.setObjectName("pushButton_4") self.pushButton_4.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_4.clicked.connect(lambda:self.setimg("4")) self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_5.setGeometry(QtCore.QRect(20, 190, 191, 161)) self.pushButton_5.setObjectName("pushButton_5") self.pushButton_5.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_5.clicked.connect(lambda:self.setimg("5")) self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_6.setGeometry(QtCore.QRect(830, 190, 191, 161)) self.pushButton_6.setObjectName("pushButton_6") self.pushButton_6.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_6.clicked.connect(lambda:self.setimg("6")) self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_7.setGeometry(QtCore.QRect(570, 190, 191, 161)) self.pushButton_7.setObjectName("pushButton_7") self.pushButton_7.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_7.clicked.connect(lambda:self.setimg("7")) self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_8.setGeometry(QtCore.QRect(290, 190, 191, 161)) self.pushButton_8.setObjectName("pushButton_8") self.pushButton_8.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_8.clicked.connect(lambda:self.setimg("8")) self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_9.setGeometry(QtCore.QRect(20, 370, 191, 161)) self.pushButton_9.setObjectName("pushButton_9") self.pushButton_9.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_9.clicked.connect(lambda:self.setimg("9")) self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_10.setGeometry(QtCore.QRect(830, 370, 191, 161)) self.pushButton_10.setObjectName("pushButton_10") self.pushButton_10.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_10.clicked.connect(lambda:self.setimg("10")) self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_11.setGeometry(QtCore.QRect(570, 370, 191, 161)) self.pushButton_11.setObjectName("pushButton_11") self.pushButton_11.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_11.clicked.connect(lambda:self.setimg("11")) self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_12.setGeometry(QtCore.QRect(290, 370, 191, 161)) self.pushButton_12.setObjectName("pushButton_12") self.pushButton_12.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_12.clicked.connect(lambda:self.setimg("12")) self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_13.setGeometry(QtCore.QRect(20, 560, 191, 161)) self.pushButton_13.setObjectName("pushButton_13") self.pushButton_13.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_13.clicked.connect(lambda:self.setimg("13")) self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_14.setGeometry(QtCore.QRect(830, 560, 191, 161)) self.pushButton_14.setObjectName("pushButton_14") self.pushButton_14.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_14.clicked.connect(lambda:self.setimg("14")) self.pushButton_15 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_15.setGeometry(QtCore.QRect(570, 560, 191, 161)) self.pushButton_15.setObjectName("pushButton_15") self.pushButton_15.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_15.clicked.connect(lambda:self.setimg("15")) self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_16.setGeometry(QtCore.QRect(290, 560, 191, 161)) self.pushButton_16.setObjectName("pushButton_16") self.pushButton_16.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}") self.pushButton_16.clicked.connect(lambda:self.setimg("16")) self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_17.setGeometry(QtCore.QRect(600, 750, 101, 41)) self.pushButton_17.setObjectName("pushButton_17") self.pushButton_17.clicked.connect(self.stop) self.pushButton_18 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_18.setGeometry(QtCore.QRect(470, 750, 101, 41)) self.pushButton_18.setObjectName("pushButton_18") self.pushButton_18.clicked.connect(self.pause) self.pushButton_19 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_19.setGeometry(QtCore.QRect(350, 750, 101, 41)) self.pushButton_19.setObjectName("pushButton_19") self.pushButton_19.clicked.connect(self.play) url = QUrl.fromLocalFile(r"1.mp3") content = QtMultimedia.QMediaContent(url) self.player = QtMultimedia.QMediaPlayer() self.player.setMedia(content) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def stop(self): self.player.stop() def play(self): self.player.play() def pause(self): self.player.pause() def setimg(self,name): global a,b,d,count,play_1,play_2 if a==[]: win32api.MessageBox(0,'游戏结束啦!n玩家一得分:%s,玩家二得分:%s'%(play_1,play_2),"提醒",win32con.MB_ICONWARNING) else: if name in d: count+=1 d.remove(name) buttonname="self.pushButton_%s"%name buttonname=eval(buttonname) if b=='': b=random.choice(a) a.remove(b) buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%b) else: c=random.choice(a) a.remove(c) buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%c) if c==b: if str(count) in ["2","6","10","14"]: play_1+=1 win32api.MessageBox(0, "恭喜你选对了!可以继续游戏。n当前玩家一得分:%s,玩家二得分:%s"%(play_1,play_2), "提醒",win32con.MB_OK) b='' else: play_2+=1 win32api.MessageBox(0, "恭喜你选对了!可以继续游戏。n当前玩家一得分:%s,玩家二得分:%s"%(play_1,play_2), "提醒",win32con.MB_OK) b='' else: win32api.MessageBox(0,'很遗憾你没选对!下一位玩家', "提醒",win32con.MB_ICONWARNING) b='' else: win32api.MessageBox(0,'你已经点过该图了!', "提醒",win32con.MB_ICONWARNING) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "点图大战")) self.player.play() self.pushButton_17.setText(_translate("MainWindow", "停止播放音乐")) self.pushButton_18.setText(_translate("MainWindow", "暂停播放音乐")) self.pushButton_19.setText(_translate("MainWindow", "继续播放音乐")) if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
url = QUrl.fromLocalFile(r"1.mp3") content = QtMultimedia.QMediaContent(url) self.player = QtMultimedia.QMediaPlayer() self.player.setMedia(content)
def setimg(self,name): global a,b,d,count,play_1,play_2 if a==[]: win32api.MessageBox(0,'游戏结束啦!n玩家一得分:%s,玩家二得分:%s'%(play_1,play_2),"提醒",win32con.MB_ICONWARNING) else: if name in d: count+=1 d.remove(name) buttonname="self.pushButton_%s"%name buttonname=eval(buttonname) if b=='': b=random.choice(a) a.remove(b) buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%b) else: c=random.choice(a) a.remove(c) buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%c) if c==b: if str(count) in ["2","6","10","14"]: play_1+=1 win32api.MessageBox(0, "恭喜你选对了!可以继续游戏。n当前玩家一得分:%s,玩家二得分:%s"%(play_1,play_2), "提醒",win32con.MB_OK) b='' else: play_2+=1 win32api.MessageBox(0, "恭喜你选对了!可以继续游戏。n当前玩家一得分:%s,玩家二得分:%s"%(play_1,play_2), "提醒",win32con.MB_OK) b='' else: win32api.MessageBox(0,'很遗憾你没选对!下一位玩家', "提醒",win32con.MB_ICONWARNING) b='' else: win32api.MessageBox(0,'你已经点过该图了!', "提醒",win32con.MB_ICONWARNING)
整体思路
global a a=[] for root, dirs, files in os.walk('./img'): a=files*2 #当前路径下所有非目录子文件
global b b='' global d d=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"] global count,play_1,play_2 count=0 play_1=0 play_2=0
结论
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算