Python3 类与对象之王者荣耀对战小游戏
王者荣耀对战小游戏
# 定义英雄: 亚瑟
class Arthur:
hero_type = 'Tank'
def __init__(self, attack_value=164, armor=98, life_value=3622, money=1000):
self.attack_value = attack_value
self.armor = armor
self.life_value = life_value
self.money = money
# 普攻
def attack(self, enemy):
damage_value = self.attack_value - enemy.armor
enemy.life_value -= damage_value
# 定义英雄: 鲁班七号
class LubanN07:
hero_type = 'ADC'
def __init__(self, attack_value=174, armor=88, life_value=3401, money=1000):
self.attack_value = attack_value
self.armor = armor
self.life_value = life_value
self.money = money
# 普攻
def attack(self, enemy):
damage_value = self.attack_value - enemy.armor
enemy.life_value -= damage_value
# 定义装备: 暗影战斧
class ShadowBattleAxe:
def __init__(self, price=500, attack_value=85, armor=50, life_value=400):
self.price = price
self.attack_value = attack_value
self.armor = armor
self.life_value = life_value
def update(self, obj):
obj.money -= self.price # 减钱
obj.attack_value += self.attack_value # 加攻击
obj.armor += self.armor # 加防御
obj.life_value += self.life_value # 加生命值
# 装备的主动技能
def fire(self, obj):
damage_value = 1000 - obj.armor # 火焰攻击为1000
obj.life_value -= damage_value
DBJ = Arthur()
XLB = LubanN07()
AXE = ShadowBattleAxe()
print('DBJ:', DBJ.hero_type, DBJ.attack_value, DBJ.armor, DBJ.life_value)
print('XLB:', XLB.hero_type, XLB.attack_value, XLB.armor, XLB.life_value)
while True:
# 装备可多次购买
if XLB.money >= AXE.price:
# 装备提升属性
AXE.update(XLB)
XLB.AXE = AXE
# 普攻
XLB.attack(DBJ)
# 装备技能伤害
XLB.AXE.fire(DBJ)
print(f'XLB attacked DBJ! life value of DBJ: {DBJ.life_value}')
if DBJ.life_value <= 0:
print('XLB WIN!')
break
# 普攻
DBJ.attack(XLB)
print(f'DBJ attacked XLB! life value of XLB: {XLB.life_value}')
if XLB.life_value <= 0:
print('DBJ WIN!')
break
DBJ: Tank 164 98 3622
XLB: ADC 174 88 3401
XLB attacked DBJ! DBJ life value: 2559!
DBJ attacked XLB! XLB life value: 3775!
XLB attacked DBJ! DBJ life value: 1411!
DBJ attacked XLB! XLB life value: 4199!
XLB attacked DBJ! DBJ life value: 263!
DBJ attacked XLB! XLB life value: 4223!
XLB attacked DBJ! DBJ life value: -885!
XLB WIN