描述:使用QtDesignner設計界面,pyQt5+python3實現主體方法制作的猜數字游戲。
游戲規則:先選擇游戲等級:初級、中級、高級、魔鬼級,選擇完游戲等級后點擊“確定”,然后后臺會自動生成一個與游戲等級匹配的“神秘數字”,游戲玩家在文本框內輸入數字,再點擊文本框旁邊的“確定”,即可比較玩家所猜數字是否就是“神秘數字”。
游戲界面:
源代碼:
代碼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", "猜數字游戲"))
self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:14pt;\">請猜一個數字:</span></p></body></html>"))
self.comboBox.setItemText(0, _translate("Form", "初級:數字小于20"))
self.comboBox.setItemText(1, _translate("Form", "中級:數字小于30"))
self.comboBox.setItemText(2, _translate("Form", "高級:數字小于50"))
self.comboBox.setItemText(3, _translate("Form", "魔鬼級:數字小于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 #聲明一個模塊內的全局變量;用于記錄猜數字的次數
rand=20#聲明一個模塊內的全局變量;神秘數字的最大范圍
allTimes=7#聲明一個模塊內的全局變量;游戲最大次數
class mwindow(QWidget, Ui_Form):
def __init__(self): #初始化
super(mwindow, self).__init__() #這是對繼承自父類的屬性進行初始化。而且是用父類的初始化方法來初始化繼承的屬性。
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
#定義一個方法:選擇游戲難度后生成一個隨機的神秘數字
def getRandNum(self):
global theNum,times
times=1 #每次選擇游戲難度并點擊“確定”后,已猜數字次數都重新歸為1
w.pushButton.setEnabled(True) #設置pushButton可點擊(即選擇了游戲難度之后,pushButton才可點擊)
theNum=random.randint(1,rand)
self.textBrowser.append('開始游戲吧,你有%d次機會,數字范圍:1-%d' %(allTimes,rand))
# self.textBrowser.append(str(theNum)) #直接顯示神秘數字,用于調試時使用
#定義一個方法:點擊“確定”按鈕的事件,用于比較所猜數字和神秘數字
def guess(self):
global allTimes,times #使用全局變量times
yourNum = int(self.lineEdit.text()) #從文本框獲取到輸入的數字,并轉化為int型
if yourNum < theNum and times < allTimes:
text = "你猜的數字%d小了!你還有%d次機會,再猜!" %(yourNum,allTimes-times)
self.textBrowser.append(text) #把提示信息寫入textBrowser
times += 1
elif yourNum > theNum and times <allTimes:
text = "你猜的數字%d大了!你還有%d次機會,再猜!" %(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次機會用完了你也沒猜對!神秘數字其實是:%d' %(allTimes,theNum)
self.textBrowser.append(text)
#定義一個方法:點擊“再來一局”時觸發的事件
def reStart(self):
self.textBrowser.clear() #清除textBrowser內的內容
self.lineEdit.clear() #清除lineEdit內的內容
w.pushButton.setEnabled(False) #設置pushButton不可點擊(即在選擇游戲難度之前,pushButton不可點擊)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = mwindow()
w.pushButton.setEnabled(False) #設置pushButton不可點擊(即在選擇游戲難度之前,pushButton不可點擊)
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_()) #使程序一直循環運行直到主窗口被關閉終止進程(如果沒有這句話,程序運行時會一閃而
總結
以上所述是小編給大家介紹的基于python3 pyQt5 QtDesignner實現窗口化猜數字游戲功能 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。