文字打怪小游戏

来源:互联网 发布:如何开一家淘宝网店 编辑:程序博客网 时间:2024/04/29 03:09
# 文字打怪小游戏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().fightkik = Kik().fightcombo = Combo().fightdef 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 = 1while 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)
原创粉丝点击