Home > Back-end >  Is there a way to go back to a previous 'while' loop?
Is there a way to go back to a previous 'while' loop?

Time:10-13

I'm very new to python. Like, 5 weeks ago I couldn't have written a 'Hello World' script. I'm trying my hand at a good old text-adventure game, since it seems like that's something python is well-suited for. I decided to structure each 'room' of the game as a different "while" loop. But I'm finding that I can't return to a previous loop, which is problematic.

Here's a simplified version of one part of my code:

room = 1
while room == 1:
     player_input = input('>> ')
     if player_input = '/help'
          print('*long list of possible commands*')
          continue
     if player_input = 'Exit Door'
          room = 2
while room == 2:
     player_input = input('>> ')
     if player_input = '/help'
          print('*long list of possible commands*')
          continue
     if player_input = '/reset'
          room = 1

I saw someone in a similar situation talk about nesting while-loops, but is that really the best way to do this? I feel like that would get messy really fast, especially with branching routes through rooms.

CodePudding user response:

Normally games will run in a single 'game loop' which contains all your logic, rather than having a bunch of different loops. Then your rooms can become if statements. For example, in pseudo code:

room = 1
playing = true
while (playing):
    if (room == 1):
        ...do room 1 stuff
    else if (room == 2):
        ...do room 2 stuff

Then when you want the game to end, you can set playing to false and the loop exits.

CodePudding user response:

You could create some function for each room that would handle it. Then in while loop you would call function related to current room and when players walks to another room just change that function to new room's function.

Here is sample code:


def room_function_1(...):
   # handle this room

def room_function_2(...):
   # handle this room

...

function_mapping = {1: room_function_1,
                    2: room_function_2,
                    ...}

current_room = 1

while current_room != -1:
    user_input = input('>> ')
    current_room = function_mapping[current_room](user_input)

This solution makes it easy to add new room - you don't need to add another while loop, just need to add new room's function and some transitions to this room.

CodePudding user response:

Depends on what concepts you've already encountered. Like you could simply wrap your while loops in another while loop "game loop" that runs if the game is playing, and that selects a room as you did via a variable.

But you might also invest 10 minutes to read up on "functions" not that complicated actually:

def [name of the function]([List of parameters]):
    [Here goes your code for example the while loop]

where he [] indicate that you need to replace them with something. So for example.

def room1():
     while True:
         player_input = input('>> ')
         if player_input = '/help'
             print('*long list of possible commands*')
             continue
         if player_input = 'Exit Door'
             return [new room number]

That would be written somewhere at the top of your program and when you want to execute it you'd just call room1() when you need it. And the "return" exits the function and passes the value back to the main line story so there you'd for example use 2 and in in the function call room = room1().

CodePudding user response:

My answer is bit similar with @george7378. I arranged code.

room = 1
while True:
    if room == 1:
        player_input = input('>> ')
        if player_input == '/help':
            print('*long list of possible commands*')
            continue
        if player_input == 'Exit Door':
            print('room before', room)
            room = 2
            print('room after', room)
    if room == 2:
        player_input = input('>> ')
        if player_input == '/help':
            print('*long list of possible commands*')
            continue
        if player_input == '/reset':
            print('room before', room)
            room = 1
            print('room after', room)

Output:

>> 
Exit Door
room before 1
room after 2
>> 
/reset
room before 2
room after 1
>> 
  • Related