1. 程式人生 > >Python:“RPG角色生成器“

Python:“RPG角色生成器“

問題描述:編寫一個簡化的建立遊戲角色的程式。 本題目要求的遊戲角色應有以下屬性:名字、性別、種族、職業、力量、敏捷、體力、智力、智慧、生命值和魔法值。 名字:不超過50個字元。 性別:可以選擇男性和女性。 種族:一共可選五個種族,人類、精靈、獸人、矮人和元素。 職業:可選六種職業,狂戰士、聖騎士、刺客、獵手、祭司和巫師。 其餘屬性均為整數。 本題目要求首先使用者輸入角色姓名,然後由使用者選擇角色性別,然後由使用者選擇種族,然後選擇職業,然後自動分配力量、敏捷、體力、智力和智慧屬性,並計算生命值和魔法值。 生命值=體力*20。 魔法值=(智力+智慧)*10。 對於職業也有一定的限制,如下圖: 在這裡插入圖片描述 屬性分配大致按以下比例: 在這裡插入圖片描述

# _author:“YYX” # date: 2018/10/5

import random


class CreateRole:

    def __init__(self):
        self.user_name = ""  # 角色姓名
        self.user_sex = ""  # 角色性別
        self.user_race = ""  # 角色種族
        self.user_occupation = ""  # 角色職業
        self.user_power = 0  # 力量
        self.user_quick = 0  # 敏捷
        self.user_physical = 0  # 體力
        self.user_brains = 0  # 智力
        self.user_intelligence = 0  # 智慧
        self.user_life = 0  # 生命值
        self.user_magic = 0  # 魔法值

    # 獲取角色姓名
    def get_name(self):
        while True:
            self.user_name = input("請輸入您遊戲角色的姓名:")
            if len(self.user_name) > 50:
                print("暱稱長度超過限制!")
            else:
                break
        return self.user_name

    # 獲取角色性別
    def get_sex(self):
        while True:
            x = input("請選擇您遊戲角色的性別(0男性,1女性):")
            if x.isdigit():  # 判斷輸入的值是否是數字
                x = int(x)
                if x == 0:
                    self.user_sex = '男性'
                    break
                elif x == 1:
                    self.user_sex = '女性'
                    break
                else:
                    print("性別請在0和1之間選擇!")
            else:
                print("請輸入數字!")
        return self.user_sex

    # 獲取角色種族
    def get_race(self):
        dic_race = {
            0: "人類",
            1: "精靈",
            2: "獸人",
            3: "矮人",
            4: "元素"
        }
        while True:
            x = input("請選擇您遊戲的種族(0人類,1精靈,2獸人,3矮人,4元素):")
            if x.isdigit():
                x = int(x)
                if x not in range(0, 5):
                    print("種族請在0-4之間選擇!")
                else:
                    self.user_race = dic_race[x]
                    break
            else:
                print("請輸入數字!")
        return self.user_race

    # 獲取角色職業
    def get_occupation(self):
        dic_occupation = {
            0: "狂戰士",
            1: "聖騎士",
            2: "刺客",
            3: "獵手",
            4: "祭司",
            5: "巫師"
        }
        if self.user_race == "人類":
            while True:
                x = input("請選擇您遊戲的職業(0狂戰士,1聖騎士,2刺客,3獵手,4祭司,5巫師)")
                if x.isdigit():
                    x = int(x)
                    if x not in range(0, 6):
                        print("人類的職業請在0-5之間選擇!")
                    else:
                        self.user_occupation = dic_occupation[x]
                        break
                else:
                    print("請輸入數字!")
        elif self.user_race == "精靈":
            while True:
                x = input("請選擇您遊戲的職業(2刺客,3獵手,4祭司,5巫師)")
                if x.isdigit():
                    x = int(x)
                    if x == 2 or x == 3 or x == 4 or x == 5:
                        self.user_occupation = dic_occupation[x]
                        break
                    else:
                        print("精靈的職業請在2-5之間選擇!")
                else:
                    print("請輸入數字!")
        elif self.user_race == "獸人":
            while True:
                x = input("請選擇您遊戲的職業(0狂戰士,3獵手,4祭司)")
                if x.isdigit():
                    x = int(x)
                    if x == 0 or x == 3 or x == 4:
                        self.user_occupation = dic_occupation[x]
                        break
                    else:
                        print("獸人的職業請在0,3,4中選擇!")
                else:
                    print("請輸入數字!")
        elif self.user_race == "矮人":
            while True:
                x = input("請選擇您遊戲的職業(0狂戰士,1聖騎士,4祭司)")
                if x.isdigit():
                    x = int(x)
                    if x == 0 or x == 1 or x == 4:
                        self.user_occupation = dic_occupation[x]
                        break
                    else:
                        print("精靈的職業請在0,1,4中選擇!")
                else:
                    print("請輸入數字!")
        elif self.user_race == "元素":
            while True:
                x = input("請選擇您遊戲的職業(4祭司,5巫師)")
                if x.isdigit():
                    x = int(x)
                    if x == 5 or x == 4:
                        self.user_occupation = dic_occupation[x]
                        break
                    else:
                        print("精靈的職業請在4,5中選擇!")
                else:
                    print("請輸入數字!")
        return self.user_occupation

    # 獲取角色屬性
    def get_property(self):
        list_kua = [8, 4, 6, 1, 1]
        list_she = [5, 3, 6, 4, 2]
        list_ci = [4, 7, 4, 3, 2]
        list_lie = [3, 8, 3, 2, 4]
        list_ji = [3, 4, 3, 7, 3]
        list_wu = [2, 4, 2, 4, 8]
        list_property = [list_kua, list_she, list_ci, list_lie, list_ji, list_wu]
        list_occupation = ["狂戰士", "聖騎士", "刺客", "獵手", "祭司", "巫師"].index(self.user_occupation)
        r = random.randint(10, 20)
        avg = (100-r)/20
        self.user_power = avg*list_property[list_occupation][0]
        self.user_quick = avg*list_property[list_occupation][1]
        self.user_physical = avg*list_property[list_occupation][2]
        self.user_brains = avg*list_property[list_occupation][3]
        self.user_intelligence = avg*list_property[list_occupation][4]
        if list_occupation == 0:
            self.user_power += r
        elif list_occupation == 1:
            self.user_physical += r
        elif list_occupation == 2 or 3:
            self.user_quick += r
        elif list_occupation == 4:
            self.user_brains += r
        elif list_occupation == 5:
            self.user_intelligence += r
        self.user_life = round(self.user_physical)*20
        self.user_magic = (round(self.user_intelligence)+round(self.user_brains))*10

    # 顯示角色
    def show_all(self):
        print("=="*20)
        print(" "*16+"姓名:%s" % self.user_name)
        print("==" * 20)
        print(" "*16+"性別:%s" % self.user_sex)
        print("==" * 20)
        print(" "*16+"種族:%s" % self.user_race)
        print("==" * 20)
        print(" "*16+"職業:%s" % self.user_occupation)
        print("==" * 20)
        print(" "*16+"力量:%d" % round(self.user_power))
        print("==" * 20)
        print(" "*16+"敏捷:%d" % round(self.user_quick))
        print("==" * 20)
        print(" "*16+"體力:%d" % round(self.user_physical))
        print("==" * 20)
        print(" "*16+"智力:%d" % round(self.user_brains))
        print("==" * 20)
        print(" "*16+"智慧:%d" % round(self.user_intelligence))
        print("==" * 20)
        print(" "*14+"魔法值:%d" % self.user_magic)
        print("==" * 20)
        print(" "*14+"生命值:%d" % self.user_life)
        print("==" * 20)


if __name__ == "__main__":
    c = CreateRole()
    c.get_name()
    c.get_sex()
    c.get_race()
    c.get_occupation()
    c.get_property()
    c.show_all()

總結:獲取屬性參考了別的文章。。。