day19作业

1.什么是对象?什么是类?

​ 对象是特征和功能的结合体

​ 类是一系列对象相同的特征和技能的集合体

2.绑定方法的有什么特点

​ 绑定方式是由对象来调用类内部的函数,特点是会把对象当作第一个参数传入函数内,不用额外传参数

3.基于面向对象设计一个对战游戏

import random
import time
class Yasso:
    camp = 'lonia'
    def __init__(self,name):
        self.name = name
        self.atk = 48
        self.armor = 15
        self.health = 430
        self.money = 1200

    def attack(self, zed):
        damage = self.atk - zed.armor
        zed.health -= damage
        print(f'''{self.name}攻击了{zed.name},伤害:-{damage}
                {zed.name}剩余血量:{zed.health}\n''')
        if zed.health <= 0:
            return True


class Zed:
    camp = 'shadow'
    def __init__(self,name):
        self.name = name
        self.atk = 49
        self.armor = 17
        self.health = 445
        self.money = 1200

    def attack(self, yasso):
        damage = self.atk - yasso.armor
        yasso.health -= damage
        print(f'''{self.name}攻击了{yasso.name},伤害:-{damage}
        {yasso.name}剩余血量:{yasso.health}\n''')
        if yasso.health <= 0:
            return True


class Belt:
    weapon_type = 'defence'
    def __init__(self,name):
        self.name = name
        self.price = 1000
        self.health = 380

    def equip(self,obj):
        obj.money -= self.price
        obj.health += self.health


class BigSword:
    weapon_type = 'attack'
    def __init__(self,name):
        self.name = name
        self.price = 1200
        self.atk = 60

    def equip(self,obj):
        obj.money -= self.price
        obj.atk += self.atk


wick = Zed('火影劫')
tank = Yasso('快乐风男')
titan = Belt('巨人腰带')
storm = BigSword('暴风大剑')

print(f'''
玩家wick使用英雄{wick.name}       
生命值:{wick.health}       
攻击力:{wick.atk}
护甲:{wick.armor}
金币:{wick.money}\n''')
time.sleep(1.8)

print(f'''玩家tank使用英雄{tank.name}
生命值:{tank.health}
攻击力:{tank.atk}
护甲:{tank.armor}
金币:{tank.money}\n''')
time.sleep(1.8)


if wick.money >= storm.price:
    wick.storm = storm
    storm.equip(wick)
    print(f'{wick.name}购买了{storm.name}')
    time.sleep(1)

if tank.money >= titan.price:
    tank.titan = titan
    titan.equip(tank)
    print(f'{tank.name}购买了{titan.name}')
    time.sleep(1)

print(f'''
玩家wick使用英雄{wick.name}       
生命值:{wick.health}       
攻击力:{wick.atk}
护甲:{wick.armor}
金币:{wick.money}
装备:{wick.storm.name}\n''')
time.sleep(1.8)

print(f'''玩家tank使用英雄{tank.name}
生命值:{tank.health}
攻击力:{tank.atk}
护甲:{tank.armor}
金币:{tank.money}
装备:{tank.titan.name}\n''')
time.sleep(1.8)


while True:
    rand = random.randint(1,3)

    if rand == 1:

        res = wick.attack(tank)
        time.sleep(0.8)
        if res:
            print(f'''玩家wick使用{wick.name}很轻松的击杀了菜鸡玩家tank使用的{tank.name}''')
            break

    if rand == 2:

        res1 = tank.attack(wick)
        time.sleep(0.8)
        if res1:
            print(f'''玩家tank使用{tank.name}卑鄙无耻的侥幸击杀了玩家wick使用的{wick.name}''')
            break

猜你喜欢

转载自www.cnblogs.com/wick2019/p/11642967.html