Python文本游戏 - 不能退出一个循环

时间:2016-09-25 11:05:49

标签: python

这是主要代码:

import MainMod
print("Welcome!")
print("Note: In this games you use wasd+enter to move!\nYou press 1 key and then enter,if you press multiple kets it wont work.\nYou will always move by 5 meters.")
CurrentRoom = 1

#Limits work this way!1st and 2nd number are X values(1st is <---- limit,2nd is ---> limit)
#3rd and 4th are y values(1st is v limit,2nd is ^ limit)
# X and Y are coordinates; 0,0 is the starting point of every room
while True:
    if CurrentRoom ==1:
        print("This is room 1")
        MainMod.roomlimits = [-15 , 15, -15 , 15]
        MainMod.doorloc1 = [-15,10,15]
        MainMod.doorloc2 = [15,-2,2]

    while CurrentRoom == 1:
        MainMod.MainLel()

        if MainMod.door1 == 1:
            print("DAMN SON")
            CurrentRoom = 2
            break
        elif MainMod.door2 == 1:
            print("Plz no")
            CurrentRoom = 3
            break

    while CurrentRoom == 2:
        MainMod.MainLel()

这是MainMod模块:

x = 0
y = 0
roomlimits = 0
doorloc1=0
doorloc2=0
door1 = 0
door2 = 0
direct = 0

def MainLel():
    global direct
    movementinput()
    movement(direct)
    doorcheck()

def movement(dir):
    global x,y,roomlimits,door1,door2,doorloc1,doorloc2

    if dir == "w":
        y += 5
        if y > roomlimits[3]:
            y = roomlimits[3]
        print("Youre current coordinates are x:",x," y:",y)
    elif dir == "s":
        y -= 5
        if y < roomlimits[2]:
            y = roomlimits[2]
        print("Youre current coordinates are x:",x," y:",y)
    elif dir == "d":
        x += 5
        if x > roomlimits[1]:
            x = roomlimits[1]
        print("Youre current coordinates are x:",x," y:",y)

    elif dir == "a":
        x -=    5
        if x < roomlimits[0]:
            x = roomlimits[2]
        print("Youre current coordinates are x:",x," y:",y)

def movementinput():
    global direct
    while True:
        direct = input("")
        if direct in ("w","a","s","d","W","A","D","S"):
            break
        else:
            print("You failure.")

def doorcheck():
    global x,y,doorloc1,doorloc2,door1,door2
    if x ==  doorloc1[0] and doorloc1[1] <= y <= doorloc1[2]:
        door1 = 1
    elif y == doorloc2[0] and doorloc2[1] <= x <= doorloc2[2]:
        door2 = 1
    else:
        door1,door2 = 0,0

我使用模块而不是类,因为我还不知道如何使用类,无论如何,程序中发生的事情是,如果我在门的位置,它只是打印&#34; DAMN SON&#34;并没有突破Room循环,任何帮助?编辑注意:我稍后添加了break语句,试试它是否会有所帮助,遗憾的是它没有,我也有点累,所以我猜我在某个地方犯了一个逻辑错误,提前感谢你的帮助。

最终编辑:代码一直都是功能性的,我只是测试不正确!感谢你们的问题,现在关闭这个问题。

1 个答案:

答案 0 :(得分:1)

由于我无法想象它不起作用,我在房间1和2中添加了两个标记(打印命令):

while CurrentRoom == 1:
    print("one")
    mod.MainLel()

while CurrentRoom == 2:
    print("two")
    mod.MainLel()

这就是发生的事情:

Youre current coordinates are x: -5  y: 15
one
a
Youre current coordinates are x: -10  y: 15
one
a
Youre current coordinates are x: -15  y: 15
DAMN SON
two
a
Youre current coordinates are x: -15  y: 15
two

结果证明工作正常。但break是多余的。无论如何,循环都会中断,因为条件变为False