zoukankan      html  css  js  c++  java
  • python学习日记第三天(实例)

    ------------恢复内容开始------------

    '''反恐精英cs(文字版)
    对象:
    玩家(战士)
    敌人
    枪:
    弹夹
    子弹
    对象关系:
    (1)战士和敌人都为人,默认血量100
    (2)射击时消耗弹夹子弹
    (3)每次击中敌人敌人掉五滴血
    类:
    1.战士和敌人类
    属性:姓名(name),血量(blood),枪(gud)
    方法:安装子弹,安装弹夹,拿枪,开枪
    2.子弹类
    属性:杀伤力
    方法:伤害敌人(让敌人掉血)
    3.弹夹类
    属性:容量,当前保存子弹
    方法:保存子弹(安装子弹),弹出子弹(开枪)
    4.枪类
    属性:弹夹(默认没有,需安装)
    方法:连接弹夹,射子弹
    '''
    #定义战士类
    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 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):
    #弹夹出子弹
    bullet=self.clip.launch_bullet()
    if bullet:
    bullet.hurt(enemy)
    else:
    print("没有子弹了,放的空枪...")

    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)
    soldier=Person("幻幽")
    #创建一个弹夹
    clip=Clip(20)
    print(clip)
    #添加5颗子弹
    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)
    print(enemy)
    soldier.fire(enemy)
    print(clip)
    print(enemy)



    ------------恢复内容结束------------

  • 相关阅读:
    python数据类型:字典Dictionary
    python数据类型:元组
    python数据类型:列表List和Set
    python数据类型:字符串
    python数据类型:Number数字
    Python控制语句
    Python运算符
    python基础语法
    Linux shell Script初识
    linux awk详解
  • 原文地址:https://www.cnblogs.com/hz-garden/p/12807070.html
Copyright © 2011-2022 走看看