基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能
描述:使用QtDesignner設(shè)計界面,pyQt5+python3實(shí)現(xiàn)主體方法制作的猜數(shù)字游戲。
游戲規(guī)則:先選擇游戲等級:初級、中級、高級、魔鬼級,選擇完游戲等級后點(diǎn)擊“確定”,然后后臺會自動生成一個與游戲等級匹配的“神秘數(shù)字”,游戲玩家在文本框內(nèi)輸入數(shù)字,再點(diǎn)擊文本框旁邊的“確定”,即可比較玩家所猜數(shù)字是否就是“神秘數(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;\">請猜一個數(shù)字:</span></p></body></html>"))
self.comboBox.setItemText(0, _translate("Form", "初級:數(shù)字小于20"))
self.comboBox.setItemText(1, _translate("Form", "中級:數(shù)字小于30"))
self.comboBox.setItemText(2, _translate("Form", "高級:數(shù)字小于50"))
self.comboBox.setItemText(3, _translate("Form", "魔鬼級:數(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;\">請選擇游戲難度:</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 #聲明一個模塊內(nèi)的全局變量;用于記錄猜數(shù)字的次數(shù)
rand=20#聲明一個模塊內(nèi)的全局變量;神秘數(shù)字的最大范圍
allTimes=7#聲明一個模塊內(nèi)的全局變量;游戲最大次數(shù)
class mwindow(QWidget, Ui_Form):
def __init__(self): #初始化
super(mwindow, self).__init__() #這是對繼承自父類的屬性進(jìn)行初始化。而且是用父類的初始化方法來初始化繼承的屬性。
self.setupUi(self)
#定義一個方法:從下拉框選擇游戲難度
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
#定義一個方法:選擇游戲難度后生成一個隨機(jī)的神秘數(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ī)會,數(shù)字范圍:1-%d' %(allTimes,rand))
# self.textBrowser.append(str(theNum)) #直接顯示神秘數(shù)字,用于調(diào)試時使用
#定義一個方法:點(diǎn)擊“確定”按鈕的事件,用于比較所猜數(shù)字和神秘數(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ī)會,再猜!" %(yourNum,allTimes-times)
self.textBrowser.append(text) #把提示信息寫入textBrowser
times += 1
elif yourNum > theNum and times <allTimes:
text = "你猜的數(shù)字%d大了!你還有%d次機(jī)會,再猜!" %(yourNum,allTimes-times)
self.textBrowser.append(text)
times += 1
elif yourNum == theNum and times <allTimes:
text = '你猜對了,就是%d,你一共猜了%s次!' % (theNum,times)
self.textBrowser.append(text)
else:
text = '%d次機(jī)會用完了你也沒猜對!神秘數(shù)字其實(shí)是:%d' %(allTimes,theNum)
self.textBrowser.append(text)
#定義一個方法:點(diǎn)擊“再來一局”時觸發(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)行時會一閃而
總結(jié)
以上所述是小編給大家介紹的基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python實(shí)現(xiàn)輸入任意一個大寫字母生成金字塔的示例
這篇文章主要介紹了python實(shí)現(xiàn)輸入任意一個大寫字母生成金字塔的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
django 解決自定義序列化返回處理數(shù)據(jù)為null的問題
這篇文章主要介紹了django 解決自定義序列化返回處理數(shù)據(jù)為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python seek()和tell()函數(shù)的具體使用
本文主要介紹了Python seek()和tell()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
詳解Python對JSON中的特殊類型進(jìn)行Encoder
這篇文章主要介紹了詳解Python對JSON中的特殊類型進(jìn)行Encoder,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用python將mdb數(shù)據(jù)庫文件導(dǎo)入postgresql數(shù)據(jù)庫示例
mdb格式文件可以通過mdbtools工具將內(nèi)中包含的每張表導(dǎo)出到csv格式文件。由于access數(shù)據(jù)庫和postgresQL數(shù)據(jù)庫格式上會存在不通性,所以使用python的文件處理,將所得csv文件修改成正確、能識別的格式2014-02-02
用Python實(shí)現(xiàn)局域網(wǎng)控制電腦
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)局域網(wǎng)控制電腦,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

