为什么不赢得我的游戏选手?#34;跑?

时间:2014-05-31 14:50:24

标签: python

好的...所以我正在查看我在学习python时编写的旧脚本,我遇到了一个我写的游戏引擎/跑步者。

我决定看看它,由于某种原因它不会运行。它应该做的是在用户输入“是”的时候运行每个级别,但由于某种原因它会卡在1/2级。

class Maps(object):
    def __init__(self):
        self.scene_run = False

    def start(scene_run):
        print 'this function has not yet been implimented yet'


class Level_1(Maps):

    def start(self):
        print "This is the first level"

        print "do you want to complete the level?"
        choice = raw_input("> ")

        if choice == "yes":
            self.scene_run = True

        if choice == "no":
            exit(0)

class Level_2(Maps):

    def start(self):
        print "This is the second level"

        print "do you want to complete the level?"
        choice = raw_input("> ")

        if choice == "yes":
            self.scene_run = True

        if choice == "no":
            exit(0)


class Level_3(Maps):

    def start(self):

        print "This is the third level"

        print "do you want to complete the level?"
        choice = raw_input("> ")

        if choice == "yes":
            self.scene_run = True

        if choice == "no":
            exit(0)   


levelsdict = {"1": Level_1(), '2': Level_2(), '3' : Level_3()}   

class Engine(object):

    def run (self, levelsdict):

        while True:
            if levelsdict["1"].scene_run == False:
                print "level 1 not done"
                levelsdict["1"].start()

            if levelsdict["1"].scene_run == True:
                print "level 1 done"
                levelsdict['2'].start()

            elif levelsdict['2'].scene_run == True:
                print "level 2 done"
                levelsdict["3"].start()

            elif levelsdict['3'].scene_run == True:
                print "level 3 done"
                exit(0)


runner = Engine()
runner.run(levelsdict)

1 个答案:

答案 0 :(得分:4)

if levelsdict["1"].scene_run == True:

首先检查这一点,如果用户输入yes,则总是执行并且永远不会达到下一个语句。

尝试更改elif's to if's以确保每次传递都执行

您还可以将表达式缩短为:

        while True:
            if not levelsdict["1"].scene_run: # same as ==False 
                print "level 1 not done"
                levelsdict["1"].start()

            if levelsdict["1"].scene_run: # same as ==True
                print "level 1 done"
                levelsdict['2'].start()

            if levelsdict['2'].scene_run:
                print "level 2 done"
                levelsdict["3"].start()

            if levelsdict['3'].scene_run:
                print "level 3 done"
                exit(0)