随着时代的发展,游戏成为很多人释放压力的重要途经,但怎样设计自己的英雄呢?英雄之间的对战又是什么样的呢?以下为两个英雄PK的简单模拟。
# 请设计如下类,不用测试
import random
# 1.英雄父类: -->初始化属性:姓名,血量
class Hero:
def __init__(self,name,blood):
self.name=name
self.blood=blood
# 2. 英雄子类1--智力英雄类:
# 增加属性:智力,初始化属性,
# 并且调用英雄父类init函数
# 行为consume:返回一个随机伤害数值
class Hero_intelligence(Hero):
def __init__(self,name,blood,intelligence):
super().__init__(name,blood)
self.intelligence=intelligence
def consume(self):
return random.randint(10,20)
# 3. 英雄子类2--力量英雄类:
# 增加属性: face,初始化属性,
# 并且调用英雄父类init函数
# 行为consume:返回一个随机伤害数值
class Hero_Power(Hero):
def __init__(self,name,blood,face):
super().__init__(name,blood)
self.face=face
def consume(self):
return random.randint(10,20)
# 4.构建游戏,创建玩家并测试:
hr1=Hero_intelligence('李白',100,200)
hr2=Hero_intelligence('猴子',100,500)
while True:
num=random.randint(0,1)
if hr2.blood<=0 or hr1.blood<=0:
print("game over!")
break
if num ==0:
consume=hr1.consume()
hr2.blood -=consume
if hr2.blood <=0:
print(hr2.name,"阵亡!")
else:
print(f'{hr1.name}剩余血量为{hr1.blood},{hr2.name}剩余血量为{hr2.blood}')
else:
consume=hr2.consume()
hr1.blood -= consume
if hr1.blood <=0:
print(hr1.name,"阵亡!")
else:
print(f'{hr1.name}剩余血量为{hr1.blood},{hr2.name}剩余血量为{hr2.blood}')
运行结果: