CS编写---初学者

    xiaoxiao2022-07-02  107

    CS编写

    菜鸟级别的我。。。。。

    嘻嘻嘻。。。。

    class Person: def __init__(self,name): self.name=name self.blood=100 def install_bullet(self,clip,bullet): clip.save_bullets(bullet) def install_clip(self,gun,clip): gun.mounting_clip(clip) def take_gun(self,gun): self.gun=gun def fire(self,enemy): self.gun.shoot(enemy) def __str__(self): return self.name+"剩余血量为:"+str(self.blood) def lose_blood(self,damage): self.blood-=damage class Clip: def __init__(self,capacity): self.capacity=capacity self.current_list=[] def save_bullets(self,bullet): if len(self.current_list)<self.capacity: self.current_list.append(bullet) def __str__(self): return "弹夹当前的子弹数量为:"+str(len(self.current_list))+"/"+str(self.capacity) def launch_bullet(self): if len(self.current_list)>0: bullet=self.current_list[-1] self.current_list.pop() return bullet else: return None class Bullet: def __init__(self,damage): self.damage=damage def hurt(self,enemy): enemy.lose_blood(self.damage) class Gun: def __init__(self): self.clip=None def __str__(self): if self.clip: return "枪当前有弹夹" else: return "枪没有弹夹" def mounting_clip(self,clip): if not self.clip: self.clip=clip def shoot(self,enemy): self.clip.launch_bullet() bullet=self.clip.launch_bullet() if bullet: bullet.hurt(enemy) else: print("没有子弹了,放空了枪。。。") soldier=Person("老王") clip=Clip(20) print(clip) i=0 while i<5: bullet=Bullet(5) soldier.install_bullet(clip,bullet) i+=1 print(clip) gun=Gun() print(gun) soldier.install_clip(gun,clip) print(gun) bullet=Bullet(5) enemy=Person("敌人") print(enemy) soldier.take_gun(gun) soldier.fire(enemy) print(clip) soldier.fire(enemy) print(clip) print(enemy)

     

    最新回复(0)