1. 程式人生 > >python+pyqt5實現RPG遊戲角色生成器

python+pyqt5實現RPG遊戲角色生成器

遊戲說明:輸入姓名,選擇性別,選擇種族,職業,點選確定按鈕,會生成各屬性值,如果不滿意,點選返回,重新選擇。

首先準備一個儲存下拉框選項資料的檔案 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_())

這個實現功能主要就是介面的切換和關聯下拉框。

生成的介面: 在這裡插入圖片描述 有問題歡迎評論