Python 2.7 尝试用Python创建一个基本的基于文本的游戏。如何实现if循环;“长矛”;不筑巢

Python 2.7 尝试用Python创建一个基本的基于文本的游戏。如何实现if循环;“长矛”;不筑巢,python-2.7,if-statement,for-loop,game-engine,Python 2.7,If Statement,For Loop,Game Engine,我试图实现一个循环,循环选择是否拥有长矛。 英雄应该用长矛杀死熊。没有长矛,他应该被吃掉 最初,我将长矛设置为False,然后在英雄拿起长矛时将其更改为TRUE。但是,当循环回bear_room()时,spear的值重置为FALSE 有没有办法绕过这个问题而不必嵌套2个if函数? 如果没有,第二个if循环是否有更干净的实现 我尝试使用if“true”和“true”以及“if true和false”来判断长矛是否由英雄持有。现在这不起作用。但是,代码仍按原样运行 代码如下: from sy

我试图实现一个循环,循环选择是否拥有长矛。 英雄应该用长矛杀死熊。没有长矛,他应该被吃掉

最初,我将长矛设置为False,然后在英雄拿起长矛时将其更改为TRUE。但是,当循环回bear_room()时,spear的值重置为FALSE

有没有办法绕过这个问题而不必嵌套2个if函数? 如果没有,第二个if循环是否有更干净的实现

我尝试使用if“true”和“true”以及“if true和false”来判断长矛是否由英雄持有。现在这不起作用。但是,代码仍按原样运行

代码如下:

    from sys import exit

    def dead(why):
        print why, "good Job!"
        exit(0)
    def bear_room():
        print """In front of you is a big bear. It stares at you. You notice a door behind the bear. To the side of the bear you see a spear propped up against the wall. What do you do?"""
        print"1. Fight the bear"
        print"2. Pick up the spear"
        print"3. try to run away"    
        bear = raw_input(">")
        spear = False
        if (bear == "1" and spear == True) :
            print "You fight and kill the bear"

        elif (bear == "1" and spear == False):
            dead("the bear claws your face off")

        elif (bear == "2" and spear == True):
            print "You already have the spear"
            bear_room()

        elif (bear == "2" and spear == False):
            print "you picked up the spear"
            spear = True
            print "Now do you want to fight the bear?"
            print "1. Fight the bear with the spear"
            print "2. Try to run away from the bear"
            choice = raw_input(">")
            if choice == "1":
                print """You fought and killed the bear, you can now
                go through the door"""
            elif choice == "2":
               dead("The bear eats you from behind")
            else:
                """You drop the spear, and retreat back to the entrance of  the room. """
                bear_room()

        elif (bear == "3"):
        dead("The Bear catches you and rips your head off")

        else:
            print "Please choose what to do!"
            bear_room()

    bear_room()

使用参数声明
bear_room

def bear_room(spear = False):
取出首字母
spear=False
,无论何时递归调用
bear_room
确保传入
bear_room(spear)
,以反映spear是否已更改


请注意,如果只调用
bear\u room()
而不传入任何参数,
spear
将重置为False

将函数
bear\u room()
的签名更改为
bear\u room(spear\u status=False)
使用spear\u状态初始化spear变量


无论你在哪里
调用bear_room()
都可以称之为
bear_room(spear)

你也可以使用对象。这里有一个例子

from sys import exit

class Person:
        def __init__(self, name):
                self.name = name
                self.items = []


        def die(self,why):
                print why, "good Job", self.name
                exit(0)

def bear_room(person):
        print """In front of you is a big bear. It stares at you. You notice a door behind the bear. To the side of the bear you see a spear propped up against the wall. What do you do?"""
        print"1. Fight the bear"

        has_spear = "Spear" in person.items
        if not has_spear: print"2. Pick up the spear"
        print  "3. try to run away"
        bear = raw_input(">")

        if (bear == "1" and has_spear) :
                print "You fight and kill the bear"
                return True

        elif (bear == "1" and not has_spear):
                person.die("the bear claws your face off")

        elif (bear == "2" and not has_spear):
                print "you picked up the spear"
                person.items.append("Spear")
                return False

        elif (bear == "3" and has_spear):
                person.die("The Bear catches you and rips your head off")

        elif (bear == "3" and not has_spear):
                person.die("The Bear eats you from behind")
        elif has_spear:
                print "You drop the spear retreat back to the entrance of  the room. "
                return True
        else:
            print "Please choose what to do!"
            return False


warrior = Person("Soldat")
while True:
    if bear_room(warrior): break