基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能
描述:使用QtDesignner設(shè)計(jì)界面,pyQt5+python3實(shí)現(xiàn)主體方法制作的猜數(shù)字游戲。
游戲規(guī)則:先選擇游戲等級(jí):初級(jí)、中級(jí)、高級(jí)、魔鬼級(jí),選擇完游戲等級(jí)后點(diǎn)擊“確定”,然后后臺(tái)會(huì)自動(dòng)生成一個(gè)與游戲等級(jí)匹配的“神秘?cái)?shù)字”,游戲玩家在文本框內(nèi)輸入數(shù)字,再點(diǎn)擊文本框旁邊的“確定”,即可比較玩家所猜數(shù)字是否就是“神秘?cái)?shù)字”。
游戲界面:
源代碼:
代碼1: guessNumberGame.py (界面代碼)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'guessNumberGame.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(555, 463) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(40, 90, 181, 31)) self.label.setObjectName("label") self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(230, 30, 171, 31)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(420, 30, 91, 31)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(420, 90, 91, 31)) self.pushButton.setObjectName("pushButton") self.textBrowser = QtWidgets.QTextBrowser(Form) self.textBrowser.setGeometry(QtCore.QRect(40, 151, 471, 201)) self.textBrowser.setObjectName("textBrowser") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(230, 90, 171, 31)) self.lineEdit.setObjectName("lineEdit") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(40, 30, 181, 31)) self.label_3.setObjectName("label_3") self.pushButton_3 = QtWidgets.QPushButton(Form) self.pushButton_3.setGeometry(QtCore.QRect(220, 380, 111, 41)) font = QtGui.QFont() font.setFamily("Agency FB") font.setPointSize(12) self.pushButton_3.setFont(font) self.pushButton_3.setObjectName("pushButton_3") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "猜數(shù)字游戲")) self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:14pt;\">請(qǐng)猜一個(gè)數(shù)字:</span></p></body></html>")) self.comboBox.setItemText(0, _translate("Form", "初級(jí):數(shù)字小于20")) self.comboBox.setItemText(1, _translate("Form", "中級(jí):數(shù)字小于30")) self.comboBox.setItemText(2, _translate("Form", "高級(jí):數(shù)字小于50")) self.comboBox.setItemText(3, _translate("Form", "魔鬼級(jí):數(shù)字小于100")) self.pushButton_2.setText(_translate("Form", "確定")) self.pushButton.setText(_translate("Form", "確定")) self.label_3.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:14pt;\">請(qǐng)選擇游戲難度:</span></p></body></html>")) self.pushButton_3.setText(_translate("Form", "再來一局"))
界面代碼
代碼2: runG uess.py (方法主體代碼)
# -*- coding: utf-8 -*- import sys,random,time from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow from guessNumberGame import Ui_Form times=1 #聲明一個(gè)模塊內(nèi)的全局變量;用于記錄猜數(shù)字的次數(shù) rand=20#聲明一個(gè)模塊內(nèi)的全局變量;神秘?cái)?shù)字的最大范圍 allTimes=7#聲明一個(gè)模塊內(nèi)的全局變量;游戲最大次數(shù) class mwindow(QWidget, Ui_Form): def __init__(self): #初始化 super(mwindow, self).__init__() #這是對(duì)繼承自父類的屬性進(jìn)行初始化。而且是用父類的初始化方法來初始化繼承的屬性。 self.setupUi(self) #定義一個(gè)方法:從下拉框選擇游戲難度 def gameLevel(self): times=1 global rand,allTimes level=self.comboBox.currentIndex() if level==0: rand=20 allTimes=7 if level==1: rand=30 allTimes=10 if level==2: rand=50 allTimes = 15 if level==3: rand=100 allTimes = 20 #定義一個(gè)方法:選擇游戲難度后生成一個(gè)隨機(jī)的神秘?cái)?shù)字 def getRandNum(self): global theNum,times times=1 #每次選擇游戲難度并點(diǎn)擊“確定”后,已猜數(shù)字次數(shù)都重新歸為1 w.pushButton.setEnabled(True) #設(shè)置pushButton可點(diǎn)擊(即選擇了游戲難度之后,pushButton才可點(diǎn)擊) theNum=random.randint(1,rand) self.textBrowser.append('開始游戲吧,你有%d次機(jī)會(huì),數(shù)字范圍:1-%d' %(allTimes,rand)) # self.textBrowser.append(str(theNum)) #直接顯示神秘?cái)?shù)字,用于調(diào)試時(shí)使用 #定義一個(gè)方法:點(diǎn)擊“確定”按鈕的事件,用于比較所猜數(shù)字和神秘?cái)?shù)字 def guess(self): global allTimes,times #使用全局變量times yourNum = int(self.lineEdit.text()) #從文本框獲取到輸入的數(shù)字,并轉(zhuǎn)化為int型 if yourNum < theNum and times < allTimes: text = "你猜的數(shù)字%d小了!你還有%d次機(jī)會(huì),再猜!" %(yourNum,allTimes-times) self.textBrowser.append(text) #把提示信息寫入textBrowser times += 1 elif yourNum > theNum and times <allTimes: text = "你猜的數(shù)字%d大了!你還有%d次機(jī)會(huì),再猜!" %(yourNum,allTimes-times) self.textBrowser.append(text) times += 1 elif yourNum == theNum and times <allTimes: text = '你猜對(duì)了,就是%d,你一共猜了%s次!' % (theNum,times) self.textBrowser.append(text) else: text = '%d次機(jī)會(huì)用完了你也沒猜對(duì)!神秘?cái)?shù)字其實(shí)是:%d' %(allTimes,theNum) self.textBrowser.append(text) #定義一個(gè)方法:點(diǎn)擊“再來一局”時(shí)觸發(fā)的事件 def reStart(self): self.textBrowser.clear() #清除textBrowser內(nèi)的內(nèi)容 self.lineEdit.clear() #清除lineEdit內(nèi)的內(nèi)容 w.pushButton.setEnabled(False) #設(shè)置pushButton不可點(diǎn)擊(即在選擇游戲難度之前,pushButton不可點(diǎn)擊) if __name__ == '__main__': app = QApplication(sys.argv) w = mwindow() w.pushButton.setEnabled(False) #設(shè)置pushButton不可點(diǎn)擊(即在選擇游戲難度之前,pushButton不可點(diǎn)擊) w.pushButton.clicked.connect(w.guess) #綁定guess方法 w.pushButton_2.clicked.connect(w.getRandNum) w.comboBox.currentIndexChanged.connect(w.gameLevel) w.pushButton_3.clicked.connect(w.reStart) w.show() sys.exit(app.exec_()) #使程序一直循環(huán)運(yùn)行直到主窗口被關(guān)閉終止進(jìn)程(如果沒有這句話,程序運(yùn)行時(shí)會(huì)一閃而
總結(jié)
以上所述是小編給大家介紹的基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python實(shí)現(xiàn)輸入任意一個(gè)大寫字母生成金字塔的示例
這篇文章主要介紹了python實(shí)現(xiàn)輸入任意一個(gè)大寫字母生成金字塔的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10django 解決自定義序列化返回處理數(shù)據(jù)為null的問題
這篇文章主要介紹了django 解決自定義序列化返回處理數(shù)據(jù)為null的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python seek()和tell()函數(shù)的具體使用
本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python+Qt身體特征識(shí)別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識(shí)別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12詳解Python對(duì)JSON中的特殊類型進(jìn)行Encoder
這篇文章主要介紹了詳解Python對(duì)JSON中的特殊類型進(jìn)行Encoder,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07使用python將mdb數(shù)據(jù)庫(kù)文件導(dǎo)入postgresql數(shù)據(jù)庫(kù)示例
mdb格式文件可以通過mdbtools工具將內(nèi)中包含的每張表導(dǎo)出到csv格式文件。由于access數(shù)據(jù)庫(kù)和postgresQL數(shù)據(jù)庫(kù)格式上會(huì)存在不通性,所以使用python的文件處理,將所得csv文件修改成正確、能識(shí)別的格式2014-02-02用Python實(shí)現(xiàn)局域網(wǎng)控制電腦
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)局域網(wǎng)控制電腦,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01