Python暂停,而真正的循环,直到条件满足

时间:2016-06-13 21:48:57

标签: python python-3.x

我在python中制作游戏。到目前为止,游戏由一个类(动物)和一些变量和方法组成。您可以按三个键之一进行攻击。它位于while True循环中。当你按 1 键时,它会进行一次攻击,对于 2 3 键也是如此。但是,在运行时,它会一遍又一遍地运行此语句:

print("""
Use key 1 to do {} damage, key 2 for {} damage, and key 3 for
                {} damage
            """.format(
                Animal.attack0_damage, Animal.attack1_damage, Animal.attack2_damage
))

在用户按下其中一个键之前,如何暂停while True循环?如果需要,我的全部代码都在这里。

from msvcrt import getch
class Animal():
    health = 100
    mana = 100
    attack0_damage = 1
    attack1_damage = 5
    attack2_damage = 10
    def __init__(self, name, species):
        self.name = name
        self.type = species
    def stat_mod(self):
        if self.type == "Hippo":
            Animal.health += 30
            Animal.mana -= 5
            Animal.attack0_damage += 10
            Animal.attack1_damage += 10
            Animal.attack2_damage += 10
        elif self.type == "Armadillo":
            Animal.health += 20
            Animal.mana += 10
            Animal.attack0_damage += 1
        elif self.type == "Crocodile":
            Animal.health += 25
            Animal.mana += 5
            Animal.attack0_damage += 5
            Animal.attack1_damage += 5
            Animal.attack2_damage += 5
    def attack(self, whatToAttack):
        while True:
            print("""
                Use key 1 to do {} damage, key 2 for {} damage, and key 3 for
                {} damage
            """.format(
                Animal.attack0_damage, Animal.attack1_damage, Animal.attack2_damage
            ))
            key = ord(getch())
            print(Animal.health)
            print(Animal.mana)
            print(whatToAttack.health)
            if key == 49:
                whatToAttack.health -= Animal.attack0_damage
                print(Animal.health)
                print(Animal.mana)
                print(whatToAttack.health)
            elif key == 50:
                whatToAttack.health -= Animal.attack1_damage
                mana -= 5
                print(Animal.health)
                print(Animal.mana)
                print(whatToAttack.health)
            elif key == 51:
                whatToAttack.health -= Animal.attack2_damage
                mana -= 10
                print(Animal.health)
                print(Animal.mana)
                print(whatToAttack.health)
            else:
                print("Invalid key! You can only press the keys 1, 2, and 3!")
            if whatToAttack.health <= 0:
                print("You beat {}!".format(whatToAttack.name())
                break
        input("Press enter to exit.")
hippo = Animal("Joe", "Hippo")
hippo.stat_mod()
armadillo = Animal("Jeff", "Armadillo")
armadillo.stat_mod()
hippo.attack(armadillo)

0 个答案:

没有答案
相关问题