文字打怪小游戏

# 文字打怪小游戏
class Villain:
    object = {}
    # object{}收集“反派”

    def __init__(self, name, health, describe):
        self.name = name
        self.health, self.full_health = health, health
        # full_health不变,用来比较动态血量
        self.describe = describe
        Villain.object[self.name] = self

    @property
    def check_health(self):
        # 血量动态
        if self.health >= .5 * self.full_health:
            return '{} health is strong'.format(self.name)
        if .5 * self.full_health > self.health >= .00000000000001:
            return '{} is weaker'.format(self.name)
        if self.health <= 0:
            return 'You killed {}'.format(self.name)

    @check_health.setter
    def check_health(self, value):
        self.health = value

    def __call__(self, *args, **kwargs):
        return '{}, {} who had {} health, now has {} health'.format(self.name,  self.describe, self.full_health, round(self.health, 5))


class Goblin(Villain):
    def __init__(self):
        super().__init__('Goblin', 5, 'the thief')


class Orge(Villain):
    def __init__(self):
        super().__init__('Orge', 12, '格鲁尔之孙')


goblin = Goblin()
orge = Orge()


class FightSkill:
    # 十八般武艺
    def __init__(self, skill_name, power):
        self.skill_name = skill_name
        self.skill_power = power

    @property
    def power(self):
        # 技能杀伤力
        return self.skill_power

    def fight(self, noun):
        # 战斗动态
        if noun in Villain.object:
            Villain.object[noun].health -= self.power
            if Villain.object[noun].health > 0:
                return '{}\'s health is {} {} {}'.format(noun, round(Villain.object[noun].health), '-'*5, Villain.object[noun].check_health)
            else:
                Villain.object[noun].health = 0
                # 定义血量最低为0
                return '{}\'s health is {} {} {}'.format(noun, 0, '-'*5,  Villain.object[noun].check_health)
        else:
            return '{} wrong'.format(self.skill_name.capitalize())


class Hit(FightSkill):
    # 固定掉血1
    def __init__(self):
        super().__init__('hit', 1)


class Kik(FightSkill):
    # 固定掉血2
    def __init__(self):
        super().__init__('kik', 2)


class Combo(FightSkill):
    # 80%掉血
    def __init__(self):
        super().__init__('combo', 0.8)

    def fight(self, noun):
        if noun in Villain.object:
            Villain.object[noun].health *= self.power
            if Villain.object[noun].health > 0:
                return '{}\'s health is {} {} {}'.format(noun, round(Villain.object[noun].health), '-'*5, Villain.object[noun].check_health)
            else:
                Villain.object[noun].health = 0
                return '{}\'s health is {} {} {}'.format(noun, 0, '-'*5,  Villain.object[noun].check_health)
        else:
            return '{} wrong'.format(self.skill_name.capitalize())
hit = Hit().fight
kik = Kik().fight
combo = Combo().fight


def say(noun):
    return 'You said {}'.format(noun)


def examine(noun):
    if noun.capitalize() in Villain.object:
        return Villain.object[noun.capitalize()]()
    else:
        return 'examine no way of {}'.format(noun)

verb_dict = {'say': say, 'examine': examine, 'hit': hit, 'kik': kik, 'combo': combo}


def get_input(input_words):
    command = input_words.split()
    verb_word = command[0]
    if verb_word in verb_dict:
        verb = verb_dict[verb_word]
    else:
        print('Unknown verb "{}"'.format(verb_word))
        return
    if len(command) >= 2:
        noun = command[1]
        print(verb(noun.capitalize()))
    else:
        print(verb("nothing"))


flag = 1
while True:
    if flag:
        # 只在游戏开始时执行一次
        print('The game begin')
        flag = 0
    input_word = input("Action:")
    if input_word in['out', 'exit']:
        # 设置退出条件
        print('The game Stop')
        break
    else:
        get_input(input_word)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值