python3+pyQt5+QtDesignner實現窗口化猜數字遊戲

描述:使用QtDesignner設計界面,pyQt5+python3實現主體方法制做的猜數字遊戲。html

遊戲規則:先選擇遊戲等級:初級、中級、高級、魔鬼級,選擇完遊戲等級後點擊「肯定」,而後後臺會自動生成一個與遊戲等級匹配的「神祕數字」,遊戲玩家在文本框內輸入數字,再點擊文本框旁邊的「肯定」,便可比較玩家所猜數字是否就是「神祕數字」。python

遊戲界面app

 

源代碼:dom

代碼1guessNumberGame.py (界面代碼)ide

 

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'guessNumberGame.ui'
 4 #
 5 # Created by: PyQt5 UI code generator 5.11.3
 6 #
 7 # WARNING! All changes made in this file will be lost!
 8 
 9 from PyQt5 import QtCore, QtGui, QtWidgets
10 
11 class Ui_Form(object):
12     def setupUi(self, Form):
13         Form.setObjectName("Form")
14         Form.resize(555, 463)
15         self.label = QtWidgets.QLabel(Form)
16         self.label.setGeometry(QtCore.QRect(40, 90, 181, 31))
17         self.label.setObjectName("label")
18         self.comboBox = QtWidgets.QComboBox(Form)
19         self.comboBox.setGeometry(QtCore.QRect(230, 30, 171, 31))
20         self.comboBox.setObjectName("comboBox")
21         self.comboBox.addItem("")
22         self.comboBox.addItem("")
23         self.comboBox.addItem("")
24         self.comboBox.addItem("")
25         self.pushButton_2 = QtWidgets.QPushButton(Form)
26         self.pushButton_2.setGeometry(QtCore.QRect(420, 30, 91, 31))
27         self.pushButton_2.setObjectName("pushButton_2")
28         self.pushButton = QtWidgets.QPushButton(Form)
29         self.pushButton.setGeometry(QtCore.QRect(420, 90, 91, 31))
30         self.pushButton.setObjectName("pushButton")
31         self.textBrowser = QtWidgets.QTextBrowser(Form)
32         self.textBrowser.setGeometry(QtCore.QRect(40, 151, 471, 201))
33         self.textBrowser.setObjectName("textBrowser")
34         self.lineEdit = QtWidgets.QLineEdit(Form)
35         self.lineEdit.setGeometry(QtCore.QRect(230, 90, 171, 31))
36         self.lineEdit.setObjectName("lineEdit")
37         self.label_3 = QtWidgets.QLabel(Form)
38         self.label_3.setGeometry(QtCore.QRect(40, 30, 181, 31))
39         self.label_3.setObjectName("label_3")
40         self.pushButton_3 = QtWidgets.QPushButton(Form)
41         self.pushButton_3.setGeometry(QtCore.QRect(220, 380, 111, 41))
42         font = QtGui.QFont()
43         font.setFamily("Agency FB")
44         font.setPointSize(12)
45         self.pushButton_3.setFont(font)
46         self.pushButton_3.setObjectName("pushButton_3")
47 
48         self.retranslateUi(Form)
49         QtCore.QMetaObject.connectSlotsByName(Form)
50 
51     def retranslateUi(self, Form):
52         _translate = QtCore.QCoreApplication.translate
53         Form.setWindowTitle(_translate("Form", "猜數字遊戲"))
54         self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:14pt;\">請猜一個數字:</span></p></body></html>"))
55         self.comboBox.setItemText(0, _translate("Form", "初級:數字小於20"))
56         self.comboBox.setItemText(1, _translate("Form", "中級:數字小於30"))
57         self.comboBox.setItemText(2, _translate("Form", "高級:數字小於50"))
58         self.comboBox.setItemText(3, _translate("Form", "魔鬼級:數字小於100"))
59         self.pushButton_2.setText(_translate("Form", "肯定"))
60         self.pushButton.setText(_translate("Form", "肯定"))
61         self.label_3.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:14pt;\">請選擇遊戲難度:</span></p></body></html>"))
62         self.pushButton_3.setText(_translate("Form", "再來一局"))
界面代碼

 

代碼2runGuess.py (方法主體代碼)ui

 

 1 # -*- coding: utf-8 -*-
 2 import sys,random,time
 3 from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
 4 from guessNumberGame import Ui_Form
 5 
 6 times=1 #聲明一個模塊內的全局變量;用於記錄猜數字的次數
 7 rand=20#聲明一個模塊內的全局變量;神祕數字的最大範圍
 8 allTimes=7#聲明一個模塊內的全局變量;遊戲最大次數
 9 class mwindow(QWidget, Ui_Form):
10     def __init__(self): #初始化
11         super(mwindow, self).__init__()  #這是對繼承自父類的屬性進行初始化。並且是用父類的初始化方法來初始化繼承的屬性。
12         self.setupUi(self)
13     #定義一個方法:從下拉框選擇遊戲難度
14     def gameLevel(self):
15         times=1
16         global rand,allTimes
17         level=self.comboBox.currentIndex()
18         if level==0:
19             rand=20
20             allTimes=7
21         if level==1:
22             rand=30
23             allTimes=10
24         if level==2:
25             rand=50
26             allTimes = 15
27         if level==3:
28             rand=100
29             allTimes = 20
30 
31     #定義一個方法:選擇遊戲難度後生成一個隨機的神祕數字
32     def getRandNum(self):
33         global theNum,times
34         times=1           #每次選擇遊戲難度並點擊「肯定」後,已猜數字次數都從新歸爲1
35         w.pushButton.setEnabled(True)   #設置pushButton可點擊(即選擇了遊戲難度以後,pushButton纔可點擊)
36         theNum=random.randint(1,rand)
37         self.textBrowser.append('開始遊戲吧,你有%d次機會,數字範圍:1-%d' %(allTimes,rand))
38         # self.textBrowser.append(str(theNum)) #直接顯示神祕數字,用於調試時使用
39 
40     #定義一個方法:點擊「肯定」按鈕的事件,用於比較所猜數字和神祕數字
41     def guess(self):
42         global allTimes,times #使用全局變量times
43         yourNum = int(self.lineEdit.text()) #從文本框獲取到輸入的數字,並轉化爲int型
44         if yourNum < theNum and times < allTimes:
45             text = "你猜的數字%d小了!你還有%d次機會,再猜!" %(yourNum,allTimes-times)
46             self.textBrowser.append(text)   #把提示信息寫入textBrowser
47             times += 1
48         elif yourNum > theNum and times <allTimes:
49             text =  "你猜的數字%d大了!你還有%d次機會,再猜!" %(yourNum,allTimes-times)
50             self.textBrowser.append(text)
51             times += 1
52         elif yourNum == theNum and times <allTimes:
53             text = '你猜對了,就是%d,你一共猜了%s次!' % (theNum,times)
54             self.textBrowser.append(text)
55         else:
56             text = '%d次機會用完了你也沒猜對!神祕數字實際上是:%d' %(allTimes,theNum)
57             self.textBrowser.append(text)
58 
59     #定義一個方法:點擊「再來一局」時觸發的事件
60     def reStart(self):
61         self.textBrowser.clear()  #清除textBrowser內的內容
62         self.lineEdit.clear()     #清除lineEdit內的內容
63         w.pushButton.setEnabled(False)  #設置pushButton不可點擊(即在選擇遊戲難度以前,pushButton不可點擊)
64 
65 if __name__ == '__main__':
66     app = QApplication(sys.argv)
67     w = mwindow()
68     w.pushButton.setEnabled(False)  #設置pushButton不可點擊(即在選擇遊戲難度以前,pushButton不可點擊)
69     w.pushButton.clicked.connect(w.guess)   #綁定guess方法
70     w.pushButton_2.clicked.connect(w.getRandNum)
71     w.comboBox.currentIndexChanged.connect(w.gameLevel)
72     w.pushButton_3.clicked.connect(w.reStart)
73     w.show()
74     sys.exit(app.exec_()) #使程序一直循環運行直到主窗口被關閉終止進程(若是沒有這句話,程序運行時會一閃而過)
相關文章
相關標籤/搜索