游戏说明:输入姓名,选择性别,选择种族,职业,点击确定按钮,会生成各属性值,如果不满意,点击返回,重新选择。
首先准备一个保存下拉框选项数据的文件 rpgdict.py
dictrace = {1:'人族', 2:'龙族', 3:'灵族', 4:'天族'}
dictprofession = {1:{11:'枪手', 12:'咒术师', 13:'剑士', 14:'刺客', 15:'拳师'}, 2:{21:'气宗', 22:'力士', 23:'拳师'}, 3:{31:'召唤师', 32:'气宗'}, 4:{41:'枪手', 42:'气宗'}}
然后是界面,输入信息界面设计:
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(317, 476)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 30, 113, 21))
self.lineEdit.setObjectName("lineEdit")
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(100, 90, 115, 19))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(100, 120, 115, 19))
self.radioButton_2.setObjectName("radioButton_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 210, 111, 21))
self.comboBox.setObjectName("comboBox")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(100, 290, 111, 21))
self.comboBox_2.setObjectName("comboBox_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(11, 30, 81, 20))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(10, 90, 81, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 210, 81, 21))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(11, 290, 81, 20))
self.label_4.setObjectName("label_4")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 350, 111, 31))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 317, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "RPG游戏"))
self.radioButton.setText(_translate("MainWindow", "男"))
self.radioButton_2.setText(_translate("MainWindow", "女"))
self.label.setText(_translate("MainWindow", "请输入姓名"))
self.label_2.setText(_translate("MainWindow", "请选择性别"))
self.label_3.setText(_translate("MainWindow", "请选择种族"))
self.label_4.setText(_translate("MainWindow", "请选择职业"))
self.pushButton.setText(_translate("MainWindow", "确定"))
显示界面的设计:
from PyQt5 import QtCore, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(324, 590)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 30, 31, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(120, 30, 72, 15))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(60, 70, 31, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(120, 70, 72, 15))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(60, 110, 31, 16))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(Form)
self.label_6.setGeometry(QtCore.QRect(120, 110, 72, 15))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(Form)
self.label_7.setGeometry(QtCore.QRect(60, 150, 31, 16))
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(Form)
self.label_8.setGeometry(QtCore.QRect(120, 150, 72, 15))
self.label_8.setObjectName("label_8")
self.label_9 = QtWidgets.QLabel(Form)
self.label_9.setGeometry(QtCore.QRect(20, 190, 61, 16))
self.label_9.setObjectName("label_9")
self.label_10 = QtWidgets.QLabel(Form)
self.label_10.setGeometry(QtCore.QRect(60, 220, 31, 16))
self.label_10.setObjectName("label_10")
self.label_11 = QtWidgets.QLabel(Form)
self.label_11.setGeometry(QtCore.QRect(140, 220, 72, 15))
self.label_11.setObjectName("label_11")
self.label_12 = QtWidgets.QLabel(Form)
self.label_12.setGeometry(QtCore.QRect(60, 260, 31, 16))
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(Form)
self.label_13.setGeometry(QtCore.QRect(140, 260, 72, 15))
self.label_13.setObjectName("label_13")
self.label_14 = QtWidgets.QLabel(Form)
self.label_14.setGeometry(QtCore.QRect(60, 300, 31, 16))
self.label_14.setObjectName("label_14")
self.label_15 = QtWidgets.QLabel(Form)
self.label_15.setGeometry(QtCore.QRect(140, 300, 72, 15))
self.label_15.setObjectName("label_15")
self.label_16 = QtWidgets.QLabel(Form)
self.label_16.setGeometry(QtCore.QRect(60, 340, 31, 16))
self.label_16.setObjectName("label_16")
self.label_17 = QtWidgets.QLabel(Form)
self.label_17.setGeometry(QtCore.QRect(140, 340, 72, 15))
self.label_17.setObjectName("label_17")
self.label_18 = QtWidgets.QLabel(Form)
self.label_18.setGeometry(QtCore.QRect(60, 380, 61, 16))
self.label_18.setObjectName("label_18")
self.label_19 = QtWidgets.QLabel(Form)
self.label_19.setGeometry(QtCore.QRect(140, 380, 72, 15))
self.label_19.setObjectName("label_19")
self.label_20 = QtWidgets.QLabel(Form)
self.label_20.setGeometry(QtCore.QRect(60, 420, 61, 16))
self.label_20.setObjectName("label_20")
self.label_21 = QtWidgets.QLabel(Form)
self.label_21.setGeometry(QtCore.QRect(140, 420, 72, 15))
self.label_21.setObjectName("label_21")
self.label_22 = QtWidgets.QLabel(Form)
self.label_22.setGeometry(QtCore.QRect(60, 460, 31, 16))
self.label_22.setObjectName("label_22")
self.label_23 = QtWidgets.QLabel(Form)
self.label_23.setGeometry(QtCore.QRect(140, 460, 72, 15))
self.label_23.setObjectName("label_23")
self.label_24 = QtWidgets.QLabel(Form)
self.label_24.setGeometry(QtCore.QRect(60, 500, 31, 16))
self.label_24.setObjectName("label_24")
self.label_25 = QtWidgets.QLabel(Form)
self.label_25.setGeometry(QtCore.QRect(140, 500, 72, 15))
self.label_25.setObjectName("label_25")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(100, 540, 91, 31))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "RPG游戏"))
self.label.setText(_translate("Form", "姓名"))
self.label_2.setText(_translate("Form", "TextLabel"))
self.label_3.setText(_translate("Form", "性别"))
self.label_4.setText(_translate("Form", "TextLabel"))
self.label_5.setText(_translate("Form", "种族"))
self.label_6.setText(_translate("Form", "TextLabel"))
self.label_7.setText(_translate("Form", "职业"))
self.label_8.setText(_translate("Form", "TextLabel"))
self.label_9.setText(_translate("Form", "属性值:"))
self.label_10.setText(_translate("Form", "力量"))
self.label_11.setText(_translate("Form", "TextLabel"))
self.label_12.setText(_translate("Form", "智力"))
self.label_13.setText(_translate("Form", "TextLabel"))
self.label_14.setText(_translate("Form", "精神"))
self.label_15.setText(_translate("Form", "TextLabel"))
self.label_16.setText(_translate("Form", "防御"))
self.label_17.setText(_translate("Form", "TextLabel"))
self.label_18.setText(_translate("Form", "物理攻击"))
self.label_19.setText(_translate("Form", "TextLabel"))
self.label_20.setText(_translate("Form", "魔法攻击"))
self.label_21.setText(_translate("Form", "TextLabel"))
self.label_22.setText(_translate("Form", "暴击"))
self.label_23.setText(_translate("Form", "TextLabel"))
self.label_24.setText(_translate("Form", "速度"))
self.label_25.setText(_translate("Form", "TextLabel"))
self.pushButton.setText(_translate("Form", "返回"))
最后是实现各功能:
from rpgmain import *
from rpgmessage import *
import rpgdict
import sys
from PyQt5.QtWidgets import *
class Parent_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = Ui_MainWindow()
self.main_ui.setupUi(self)
self.dictrace = rpgdict.dictrace
self.dictprofession = rpgdict.dictprofession
self.radioButton = self.main_ui.radioButton
self.radiioButton_2 = self.main_ui.radioButton_2
self.lineEdit = self.main_ui.lineEdit
self.comboBox = self.main_ui.comboBox
self.comboBox_2 = self.main_ui.comboBox_2
# 清空下拉框项目
self.comboBox.clear()
self.comboBox.addItem('请选择')
# 初始化种族
for k,v in self.dictrace.items():
self.comboBox.addItem(v, k)
def comboBox_race(self, index):
# 取种族的键值
key = self.comboBox.itemData(index)
self.comboBox_2.clear()
if key:
self.comboBox_2.addItem('请选择')
# 初始化职业
for k, v in self.dictprofession[key].items():
self.comboBox_2.addItem(v, k)
# 获取姓名性别
def get_name_sex(self):
self.name = self.lineEdit.text()
if self.radioButton.isChecked() == True:
self.mainwindow_sex = '男'
else:
self.mainwindow_sex = '女'
def ability_value(self):
if self.race_name == '人族':
if self.profession_name == '枪手':
self.strength = 85
self.intelligence = 75
self.spirit = 65
self.defense = 55
self.physical_attack = 90
self.magic_attack = 20
self.critical_strike = 90
self.speed = 95
elif self.profession_name == '咒术师':
self.strength = 20
self.intelligence = 80
self.spirit = 95
self.defense = 50
self.physical_attack = 50
self.magic_attack = 70
self.critical_strike = 60
self.speed = 50
elif self.profession_name == '剑士':
self.strength = 90
self.intelligence = 30
self.spirit = 30
self.defense = 95
self.physical_attack = 90
self.magic_attack = 10
self.critical_strike = 90
self.speed = 70
elif self.profession_name == '刺客':
self.strength = 85
self.intelligence = 70
self.spirit = 70
self.defense = 50
self.physical_attack = 90
self.magic_attack = 20
self.critical_strike = 95
self.speed = 100
elif self.profession_name == '拳师':
self.strength = 85
self.intelligence = 40
self.spirit = 30
self.defense = 80
self.physical_attack = 85
self.magic_attack = 20
self.critical_strike = 85
self.speed = 75
elif self.race_name == '龙族':
if self.profession_name == '气宗':
self.strength = 80
self.intelligence = 80
self.spirit = 60
self.defense = 50
self.physical_attack = 60
self.magic_attack = 80
self.critical_strike = 85
self.speed = 70
elif self.profession_name == '力士':
self.strength = 95
self.intelligence = 20
self.spirit = 20
self.defense = 90
self.physical_attack = 100
self.magic_attack = 10
self.critical_strike = 90
self.speed = 50
elif self.profession_name == '拳师':
self.strength = 90
self.intelligence = 30
self.spirit = 30
self.defense = 80
self.physical_attack = 90
self.magic_attack = 20
self.critical_strike = 85
self.speed = 55
elif self.profession_name == '剑士':
self.strength = 95
self.intelligence = 20
self.spirit = 30
self.defense = 95
self.physical_attack = 95
self.magic_attack = 10
self.critical_strike = 90
self.speed = 50
elif self.race_name == '灵族':
if self.profession_name == '召唤师':
self.strength = 10
self.intelligence = 85
self.spirit = 95
self.defense = 30
self.physical_attack = 20
self.magic_attack = 50
self.critical_strike = 40
self.speed = 50
elif self.profession_name == '气宗':
self.strength = 70
self.intelligence = 85
self.spirit = 80
self.defense = 40
self.physical_attack = 50
self.magic_attack = 90
self.critical_strike = 80
self.speed = 85
elif self.race_name == '天族':
if self.profession_name == '枪手':
self.strength = 85
self.intelligence = 80
self.spirit = 65
self.defense = 60
self.physical_attack = 90
self.magic_attack = 20
self.critical_strike = 85
self.speed = 90
elif self.profession_name == '气宗':
self.strength = 75
self.intelligence = 85
self.spirit = 65
self.defense = 50
self.physical_attack = 55
self.magic_attack = 85
self.critical_strike = 80
self.speed = 80
elif self.profession_name == '剑士':
self.strength = 85
self.intelligence = 75
self.spirit = 65
self.defense = 55
self.physical_attack = 90
self.magic_attack = 20
self.critical_strike = 90
self.speed = 95
def result(self):
# 取当前索引
race_index = self.comboBox.currentIndex()
profession_index = self.comboBox_2.currentIndex()
# 取当前种族职业名称
self.race_name = self.comboBox.itemText(race_index)
self.profession_name = self.comboBox_2.itemText(profession_index)
# 显示结果窗口
child.show()
class Child_Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.child_ui = Ui_Form()
self.child_ui.setupUi(self)
def showresult(self):
window.hide()
# 调用Parent_Window类的方法
window.get_name_sex()
window.result()
window.ability_value()
# 显示姓名
self.labei_2 = self.child_ui.label_2
self.labei_2.setText(str(window.name))
# 显示性别
self.label_4 = self.child_ui.label_4
self.label_4.setText(str(window.mainwindow_sex))
# 显示种族职业
self.label_6 = self.child_ui.label_6
self.label_8 = self.child_ui.label_8
self.label_6.setText(str(window.race_name))
self.label_8.setText(str(window.profession_name))
# 显示属性值
self.child_ui.label_11.setText(str(window.strength))
self.child_ui.label_13.setText(str(window.intelligence))
self.child_ui.label_15.setText(str(window.spirit))
self.child_ui.label_17.setText(str(window.defense))
self.child_ui.label_19.setText(str(window.physical_attack))
self.child_ui.label_21.setText(str(window.magic_attack))
self.child_ui.label_23.setText(str(window.critical_strike))
self.child_ui.label_25.setText(str(window.speed))
# 重新输入信息
def reset(self):
window.show()
child.hide()
if __name__=='__main__':
app = QApplication(sys.argv)
window = Parent_Window()
child = Child_Window()
window.show()
# 给下拉框绑定方法
window.comboBox.activated.connect(window.comboBox_race)
# 给按钮绑定方法
btn_1 = window.main_ui.pushButton
btn_1.clicked.connect(child.showresult)
btn_2 = child.child_ui.pushButton
btn_2.clicked.connect(child.reset)
sys.exit(app.exec_())
这个实现功能主要就是界面的切换和关联下拉框。
版权声明:本文为博主原创文章,转载请注明出处。https://blog.csdn.net/dcjmessi/article/details/82972221