class练习
cs对战
# !/usr/bin/python # -*- coding: UTF-8 -*- """ @author:HAITAO @file:class_test.py.py @time:2021/08/02 """ class Police: '''警察''' def __init__(self,name,role): '''实例对象''' self.name = name self.role = role if self.role == "队长": self.hit_points = 500 else: self.hit_points = 200 '''绑定方法''' def show_status(self): '''查看警察状态''' msg = "警员{}的血量为:{}".format(self.name,self.hit_points) print(msg) def bomb(self,*terrorist_list): '''向恐怖分子列表投炸弹''' for terrorist in terrorist_list: terrorist.blood -= 200 terrorist.show_status() class Terrorist: '''恐怖分子''' def __init__(self,name,role): self.name = name self.role = role if self.role == '头目': self.blood = 400 else: self.blood = 200 def shoot(self,polist_object): '''射击警察''' polist_object.hit_points -= 100 polist_object.show_status() def strafe(self,*polist_list,xueliang=50): '''扫射警察列表''' for polist_object in polist_list: polist_object.hit_points -= xueliang polist_object.show_status() def show_status(self): '''查看恐怖分子状态''' msg = "恐怖分子{}的血量为:{}".format(self.name,self.blood) print(msg) def init(): global shoot_blood,strafe_blood,bomb_blood,p1,p2,t1,t2 shoot_blood = 100 strafe_blood = 50 bomb_blood = 200 '创建警察列表' p1 = Police('张三','队长') p2 = Police('李四','队员') p1_init = [p1.name,p1.role,p1.hit_points] p2_init = [p2.name,p2.role,p2.hit_points] print(f'【警察队伍】\n{p1_init}\n{p2_init}') '创建匪徒列表' t1 = Terrorist('王五','头目') t2 = Terrorist("周六","团伙成员") t1_init = [t1.name, t1.role, t1.blood] t2_init = [t2.name, t2.role, t2.blood] print(f'【匪徒队伍】\n{t1_init}\n{t2_init}') def run(): '''匪徒动作''' print(''.center(20, '-')) print("匪徒头目_王五射了警察队长张三一枪,-{}血".format(shoot_blood)) t1.shoot(p1) print(''.center(20, '-')) print("匪徒队员_李四扫射到警察张三,李四一枪,-{}血".format((strafe_blood))) t2.strafe(p1,p2) '''警察动作''' print(''.center(20,'-')) print("警察队长张三向匪徒王五,周六投了炸弹,-{}血".format(bomb_blood)) p1.bomb(t2,t1) if __name__ == '__main__': init() run()