描述:使用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", "
請猜一個數字:
"))
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", "
請選擇游戲難度:
"))
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
總結
以上所述是小編給大家介紹的基于python3 pyQt5 QtDesignner實現窗口化猜數字游戲功能 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

