首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Python制作游戏?

如何用Python制作游戏?
EN

Stack Overflow用户
提问于 2018-07-03 10:01:23
回答 2查看 4.5K关注 0票数 0

我想做一个游戏,其中包括‘生活’的组成部分。我想从3条生命开始游戏。每次用户死后,剩下的生命就会减少1,游戏就会随着剩余的生命数的增加而重新开始。

问题:如果我玩游戏,失去一条生命,它总是说"2条生命仍然存在“,即使我只有1或9条生命。为什么残存的生命数从来不低于2?

这是我代码的一部分:

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-03 10:22:49

您希望将它包装到一个while循环中,而不是调用它本身\

代码语言:javascript
复制
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"
票数 0
EN

Stack Overflow用户

发布于 2018-07-03 10:27:49

我会重新组织代码,如下所示:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51151739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档