切换球员

时间:2017-06-06 21:01:19

标签: python python-3.x

我做了一个计算玩家当前健康状况并在它们之间切换的程序。我需要在玩家之间切换帮助。这就是我到目前为止所做的:

class Player():

    def turn(life):
        players = ['Player 1', 'Player 2']
        life = 100
        for player in players:
            while life >= 0:
                print (life)
                print ("+, -, or end turn")
                choice = input("> ")

                if choice == "+":
                    print ("Damage taken")
                    healing = int(input("> "))
                    life += healing
                elif choice == "-":
                    print ("Damage taken")
                    damage = int(input("> "))
                    life -= damage
                elif choice == "end turn" or "end":
                    return

            else:
                print ("You lose!")

play = Player()
play.turn()

2 个答案:

答案 0 :(得分:2)

我之前一直处于类似的位置。我建议做一些与你可能想要的答案有所不同的事情。我不建议寻找这个问题的解决方案,而是建议查看游戏设计模式的资源。

虽然我认为最初的学习曲线可能有点高,但我认为如果你学会使用适当的游戏机制设计模式,你会发现构建你想要的东西要容易得多。

您可以选择几种不同的资源。我使用http://gameprogrammingpatterns.com/(我与这个人无关),但我也有c ++背景。我会四处寻找可能最直观的东西并尝试一下。

一切顺利!

答案 1 :(得分:0)

这是一个开始:

class Player:
    def __init__(self, name, health=100):
        self.name = name
        self.health = health

    def hit(self, n):
        assert n >= 0
        self.health -= n

    def heal(self, n):
        assert n >= 0
        self.health += n

    @property
    def is_dead(self):
        return self.health <= 0

class Game:
    def __init__(self):
        self.players = [Player("Adam"), Player("Bert")]

    def turn(self, me, him):
        # fill this in!

    def play(self):
        a, b = self.players
        while True:
            self.turn(a, b)
            if b.is_dead:
                print("{} won!".format(a.name))
                break
            else:
                # swap players
                a, b = b, a

if __name__ == "__main__":
    Game().play()
相关问题