问题描述:编写一个简化的创建游戏角色的程序。
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过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()
总结:获取属性参考了别的文章。。。