Search code examples
pythonbreak

Why can't I exit the while loop in my python text game?


This is the main code:

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()

and this is the MainMod module:

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

I'm using a module instead of classes, because I don't know how to use classes yet. Anyway, what happens in the program is that if I am in the door location, it simply prints "DAMN SON" and doesn't break out of the Room loop.

I added the break statement later on to find out if it would help. Sadly it didn't. I am also a bit tired, so I think I made a logic mistake somewhere.

Final edit: The code was functional all along, I was just testing it incorrectly! Thanks for the awnsers, I'll close this question now.


Solution

  • Since I could not imagine it didn't work, I added two markers (print commands), to room 1 and 2:

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

    and

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

    This is what happened:

    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
    

    It turned out to be working fine. The break is redundant however. The loop will break anyway, since the condition becomes False.