Python面向对象练习——基于面向对象设计一个对战游戏
1 #!/usr/bin/env python 2 # -*- conding:utf-8 -*- 3 class Riven: 4 ''' 5 定义一个瑞文类 6 ''' 7 camp = 'Noxus' 8 def __init__(self,nickname,aggresssiity=54,life_value=414,money=1001,armor=3): 9 self.nickname = nickname 10 self.aggressivity = aggresssiity 11 self.life_value = life_value 12 self.money = money 13 self.armor =armor 14 def attack(self,enemy): 15 damage_value = self.aggressivity - enemy.armor 16 enemy.life_value -= damage_value 17 18 class Garen: 19 ''' 20 定义一个盖伦类 21 ''' 22 camp='Demacia' 23 def __init__(self,nickname, 24 aggressivity=58, 25 life_value=455, 26 money=100, 27 armor=10): 28 self.nickname=nickname 29 self.aggressivity=aggressivity 30 self.life_value=life_value 31 self.money=money 32 self.armor=armor 33 def attack(self,enemy): 34 damage_value=self.aggressivity-enemy.armor 35 enemy.life_value-=damage_value 36 37 38 class BlackCleaver: 39 ''' 40 定义装备 41 ''' 42 def __init__(self,price=475,aggrev=9,life_value=100): 43 self.price=price 44 self.aggrev=aggrev # 攻击力 45 self.life_value=life_value # +100点生命值 46 def update(self,obj): 47 obj.money-=self.price #减钱 48 obj.aggressivity+=self.aggrev #加攻击 49 obj.life_value+=self.life_value #加生命值 50 def fire(self,obj): #这是该装备的主动技能,喷火,烧死对方 51 obj.life_value-=1000 #假设火烧的攻击力是1000 52 53 54 # 测试交互 55 r1=Riven('草丛伦') 56 g1=Garen('盖文') 57 b1=BlackCleaver() 58 59 print(r1.aggressivity,r1.life_value,r1.money) #r1的攻击力,生命值,护甲 60 61 if r1.money > b1.price: 62 r1.B=b1 # 买装备,增加一个属性 63 b1.update(r1) 64 65 66 print(r1.aggressivity,r1.life_value,r1.money) #r1的攻击力,生命值,护甲 67 68 print(g1.life_value) 69 r1.attack(g1) #普通攻击 70 print(g1.life_value) 71 r1.B.fire(g1) #用装备攻击 72 print(g1.life_value) #g1的生命值小于0就死了
为什么要坚持,想一想当初!