Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python制作具有生命的游戏?_Python - Fatal编程技术网

如何用Python制作具有生命的游戏?

如何用Python制作具有生命的游戏?,python,Python,我想做一个包含“生命”成分的游戏。我想以3条生命开始游戏。每次用户死亡时,剩余的生命将减少1,游戏将以新的剩余生命数重新启动 问题:如果我在游戏中失去了一条生命,即使我只有1条或9条生命,它也会说“还剩下2条生命”。为什么剩余的生命数永远不会低于2 这是我代码的一部分: import random def main(): livesRemaining = 3 print "Welcome to Escape From The Shackles! " print "3

我想做一个包含“生命”成分的游戏。我想以3条生命开始游戏。每次用户死亡时,剩余的生命将减少1,游戏将以新的剩余生命数重新启动

问题:如果我在游戏中失去了一条生命,即使我只有1条或9条生命,它也会说“还剩下2条生命”。为什么剩余的生命数永远不会低于2

这是我代码的一部分:

import random

def main():

    livesRemaining = 3

    print "Welcome to Escape From The Shackles! "
    print "3 transparent vials with liquid lie in front of you on a table. One of them is green, another is yellow, yet another is red."
    colors = ["green", "red", "yellow"]
    random.shuffle(colors)

    color = raw_input("Which one of the three colors do you choose to drink?")
    color = color.lower()

    if color == (colors[0]):
        print "The liquid seeps into your system and poisons you. You die!"
        livesRemaining = livesRemaining - 1
        print "You have " + str(livesRemaining) + " lives remaining."
        main()


    elif color == (colors[1]):
        print "Your head begins to spin and you become unconscious! Next thing you know, you're in a raft in a river. No soul is present nearby..."
        print "After paddling for 15 minutes in the river, you encounter a crossroads! "
        right_or_left = raw_input("Do you choose to paddle right or left?")
        right_or_left = yellow_right_or_left.lower()

        if yellow_right_or_left == "right":
            print "You take a right turn and continue to paddle. A mighty waterfall confronts you."
            print "You die!"
            livesRemaining = livesRemaining - 1
            print "You have " + str(livesRemaining) + " lives remaining."
            main()

您希望将其包装到while循环中,而不是调用自身\

import random

livesRemaining = 3

def main():
    global livesRemaining

    print "Welcome to Escape From The Shackles! "
    while True:
        if livesRemaining == 0:
            break

        print "3 transparent vials with liquid lie in front of you on a table. One of them is green, another is yellow, yet another is red."
        colors = ["green", "red", "yellow"]
        random.shuffle(colors)

        color = raw_input("Which one of the three colors do you choose to drink?")
        color = color.lower()

        if color == (colors[0]):
            print "The liquid seeps into your system and poisons you. You die!"
            livesRemaining -= 1
            print "You have " + str(livesRemaining) + " lives remaining."
            main()


        elif color == (colors[1]):
            print "Your head begins to spin and you become unconscious! Next thing you know, you're in a raft in a river. No soul is present nearby..."
            print "After paddling for 15 minutes in the river, you encounter a crossroads! "
            right_or_left = raw_input("Do you choose to paddle right or left?")
            right_or_left = yellow_right_or_left.lower()

            if yellow_right_or_left == "right":
                print "You take a right turn and continue to paddle. A mighty waterfall confronts you."
                print "You die!"
                livesRemaining -= 1
                print "You have " + str(livesRemaining) + " lives remaining."
        print "Game Over"

我会像这样重新组织代码:

def welcome_message(livesRemaining):
    print "Welcome to Escape From The Shackles!"
    print "You have", livesRemaining, "lives remaining."

def vial_scene():
    print "3 transparent vials with liquid lie in front of you on a table." \
          " One of them is green, another is yellow, yet another is red."
    colors = ["green", "red", "yellow"]
    random.shuffle(colors)
    color = raw_input("Which one of the three colors do you choose to drink?")
    color = color.lower()
    if color == colors[0]:
        print "The liquid seeps into your system and poisons you. You die!"
        return 'dies'
    elif color == colors[1]:
        print "Your head begins to spin and you become unconscious!"
        return 'head_spin'

def river_scene():
    print "Next thing you know, you're in a raft in a river." \
          " No soul is present nearby ..."
    print "After paddling for 15 minutes in the river, you encounter" \
          " a crossroads!"
    right_or_left = raw_input("Do you choose to paddle right or left?")
    right_or_left = right_or_left.lower()
    if right_or_left == "right":
        print "You take a right turn and continue to paddle." \
              " A mighty waterfall confronts you."
        print "You die!"
        return 'dies'

def main():
    livesRemaining = 3
    while livesRemaining > 0:
        welcome_message(livesRemaining)
        vial_result = vial_scene()
        if vial_result == 'dies':
            livesRemaining -= 1
            continue  # jump to beginning of while loop
        elif vial_result == 'head_spin':
            river_result = river_scene()
            if river_result == 'dies':
                livesRemaining -= 1
                continue  # jump to beginning of while loop
            else:
                print "The river doesn't kill you."
        else:
            print "The vial doesn't kill you and gives you no head spin."
        print "You survived the two problems (vial and river)."
        break  # to leave the while loop

死后你总是再次调用
main()
,在
main()
的第一条语句中,
livesRemaining
被重置为3。哦,好的。我理解这个问题。你知道我怎么修吗?我已经尝试了这么长时间,但我还没有达到目的。我非常感谢你的帮助。非常感谢。这个问题很好地处理了python中的全局变量和局部变量:我非常感谢您!它现在起作用了!我已经尝试了这么长时间…我如何才能添加“再次播放”功能。我试过了,但每当我输入“y”时,它都会不断提示我是否想再玩一次。这就是我尝试的:if LivesMaining==0:print“GAME OVER!”再次=原始输入(“您想再次玩吗?如果想,请键入“y”);if Reach==y:continue else:breakif LivesMaining==0:print“GAME OVER!”再次=原始输入(“您想再玩一次吗?如果想,请键入‘y’”)如果想再玩一次==“y”:LivesMaining=3其他:精彩绝伦!谢谢!谢谢!非常有帮助!