Commit b707f655 by BellCodeEditor

save project

parent f795e0d4
Showing with 25 additions and 10 deletions
# 英雄角色类
class Hero: class Hero:
def __init__(self, name): def __init__(self, name):
self.level = 1
self.hp = 300
self.attack = 100
self.name = name self.name = name
def combat(self,enemy): self.level = 1
self.hp = 250
self.attack = 40
self.max_hp = self.hp
def combat(self, enemy): # 普通攻击
info1 = self.name + "对" + enemy.name + "发起进攻," info1 = self.name + "对" + enemy.name + "发起进攻,"
info2 = "造成" + str(self.attack) + "点伤害," info2 = "造成" + str(self.attack) + "点伤害,"
enemy.hp -= self.attack enemy.hp -= self.attack
...@@ -14,10 +14,25 @@ class Hero: ...@@ -14,10 +14,25 @@ class Hero:
info = info1 + info2 + info3 info = info1 + info2 + info3
print(info) print(info)
else: else:
info3 = enemy.name + "嘎了" info3 = enemy.name + "阵亡,游戏结束"
info = info1 + info2 + info3 info = info1 + info2 + info3
print(info) print(info)
exit() exit()
lixin = Hero("李信") # 创建玩家类Player放入参数Hero
houyi = Hero("后羿") class Player(Hero):
lixin.combat(houyi) # 创建__init__()函数方法并放入参数self,name
\ No newline at end of file def __init__(self,name):
# 使用super()函数继承父类方法__init__()并放入参数name
super().__init__(name)
# 重写英雄血量200
self.hp=200
# 重写英雄攻击力50
self.attack=50
# 创建玩家角色后羿
houyi=Player("后羿")
# 打印玩家血量
print(houyi.hp)
# 打印玩家攻击力
print(houyi.attack)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment