class Gun: def __init__(self,gun_type): self.gun_type=gun_type self.bullet_count=0 def add_bullet(self,count): self.bullet_count +=count def shoot(self,soldier_name): if self.bullet_count >0: arg='''士兵 %s 手持武器就位!弹药填充完毕! 手持 %s 朝 %s 开火!哒哒哒哒哒..... '''%(soldier_name,self.gun_type,enemy_name) print(arg) self.bullet_count -=1 else: print('没子弹啊!弹药呢?') class Soldier: def __init__(self,name,gun=None): self.soldier_name = name self.gun=gun def fire(self,soldier_name): if self.gun: self.gun.shoot(soldier_name) else: print('老子的意大利炮呢?') import random enemy_name=input('请输入敌人的名字:') print('发现敌人:%s'% enemy_name) print('全军出击~~~') gun_list=['AK-47','M16','G36','FAMAS','FN SCAR','AUG','FN FNC自动步枪','FN FAL','SG550'] soldier_list=['Saber','Lancer','Archer','Rider','Caster','Assassin','Berserker'] k=0 while k< len(soldier_list): gun=random.choice(gun_list) soldier_gun=Gun(gun) soldier_gun.add_bullet(1000) soldier = Soldier(soldier_list[k]) soldier.gun=soldier_gun soldier.fire(soldier_list[k]) k+=1