python学习日记-class类练习(面对对象编程)

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鹅i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值