基于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", "再來(lái)一局"))
界面代碼
代碼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)行初始化。而且是用父類的初始化方法來(lái)初始化繼承的屬性。
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('開(kāi)始游戲吧,你有%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) #把提示信息寫(xiě)入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ì)用完了你也沒(méi)猜對(duì)!神秘?cái)?shù)字其實(shí)是:%d' %(allTimes,theNum)
self.textBrowser.append(text)
#定義一個(gè)方法:點(diǎn)擊“再來(lái)一局”時(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)程(如果沒(méi)有這句話,程序運(yùn)行時(shí)會(huì)一閃而
總結(jié)
以上所述是小編給大家介紹的基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
詳解四種Python中基本形態(tài)學(xué)濾波的實(shí)現(xiàn)
最基礎(chǔ)的形態(tài)學(xué)操作有四個(gè),分別是腐蝕、膨脹、開(kāi)計(jì)算和閉計(jì)算。這篇文章主要介紹了這四種形態(tài)學(xué)濾波的實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04
pandas實(shí)戰(zhàn):分析三國(guó)志人物示例實(shí)現(xiàn)
這篇文章主要介紹了pandas實(shí)戰(zhàn):分析三國(guó)志人物示例實(shí)現(xiàn),本文章內(nèi)容詳細(xì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,需要的朋友可以參考下2023-01-01
Python基礎(chǔ)練習(xí)之用戶登錄實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Python基礎(chǔ)練習(xí)之用戶登錄實(shí)現(xiàn)代碼分享,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-11-11
使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法
這篇文章主要介紹了使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python腳本實(shí)現(xiàn)集群檢測(cè)和管理功能
這篇文章主要介紹了Python腳本實(shí)現(xiàn)集群檢測(cè)和管理功能,本文講解了實(shí)現(xiàn)想法、開(kāi)發(fā)工具選擇、經(jīng)驗(yàn)分享、代碼示例等內(nèi)容,需要的朋友可以參考下2015-03-03
Django基礎(chǔ)知識(shí) URL路由系統(tǒng)詳解
這篇文章主要介紹了Django基礎(chǔ)知識(shí) URL路由系統(tǒng)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
pandas對(duì)dataFrame中某一個(gè)列的數(shù)據(jù)進(jìn)行處理的方法
這篇文章主要介紹了pandas對(duì)dataFrame中某一個(gè)列的數(shù)據(jù)進(jìn)行處理的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
PyTorch 1.0 正式版已經(jīng)發(fā)布了
今天小編就為大家分享一篇關(guān)于PyTorch 1.0 正式版已經(jīng)發(fā)布了!小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
python?opencv實(shí)現(xiàn)目標(biāo)外接圖形
這篇文章主要為大家詳細(xì)介紹了python?opencv實(shí)現(xiàn)目標(biāo)外接圖形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

