day-19 作业
day19 作业
1.什么是对象?什么是类?
对象: 特征与技能的结合体。
类:一系列对象相同的特征与技能的结合体。
2.绑定方法的有什么特点
谁来调用绑定方法,就会把谁当作第一个参数自动传给该方法。
3.基于面向对象设计一个对战游戏
# 仿制英雄联盟英雄角色
class Garen:
def __init__(self, name, life=455, money=500, aggressivity=56, armor=9):
self.name = name
self.life = 455
self.money = 500
self.aggressivity = 56
self.armor = 9
def attack(self, enemy):
hurt = self.aggressivity - enemy.armor
enemy.life -= hurt
class Riven:
def __init__(self, name, life=414, money=500, aggressivity=54, armor=12):
self.name = name
self.life = 414
self.money = 500
self.aggressivity = 54
self.armor = 9
def attack(self, enemy):
hurt = self.aggressivity - enemy.armor
enemy.life -= hurt
class DoranSword:
def __init__(self, price=450, aggressivity=9, life=100):
self.price = 450
self.aggressivity = 9
self.life = 100
def update(self, hero):
hero.money -= self.price
hero.aggressivity += self.aggressivity
hero.life += self.life
def fire(self, enemy):
enemy.life -= 2000
garen = Garen('Garen')
riven = Riven('Riven')
doransword = DoranSword()
print('Start!')
print(f'{garen.name}购买了装备DoranSword!')
if garen.money >= doransword.price:
garen.doransword=doransword
doransword.update(garen)
print(f'riven的初始生命值为:{riven.life}')
garen.attack(riven)
print(f'{garen.name}攻击了{riven.name},造成了{garen.aggressivity - riven.armor}点伤害!')
print(f'riven剩余生命值:{riven.life}')
garen.doransword.fire(riven)
print(f'{garen.name}使用了技能击杀了{riven.name}!')
print(f'riven剩余生命值:{riven.life}')
运行结果:
Start!
Garen购买了装备DoranSword!
riven的初始生命值为:414
Garen攻击了Riven,造成了56点伤害!
riven剩余生命值:358
Garen使用了技能击杀了Riven!
riven剩余生命值:-1642
Process finished with exit code 0