Hi,

I'm trying to create a textgame in python, and after searching for a few topics about it I though I knew how to make it. Many textgames use a system like this:

def room_one():
    #do stuff
    if something:
        room_two()

def room_two():
    #do stuff

Most of the time the functions are alliterating until the player enters a command the game knows.

This works fine and all, but I just realized that using this system will result in a sort of recursion between functions. When you let the player go to another room, the function calling that function is not closed/ended yet.

So my questions are: Am I correct about this? Is this bad at all? If so, how would I solve this, or is there a better way to make a textgame?

Thanks,
Lapixx

The better way would be using objects

Not sure about the rules on these, but I wrote an article recently on this (and writing another one):
http://thekks.net/916

Here is what i would do. I would have something that is the Main function, that calls room1 to start off with... Then IF room1 wants to call room2 it returns a special value that means i know the Main function has to call room2.

def room1():
    #is you need to call room two then:
    if something:
        #return the number 2
        return 2

def room2():
    print "ROOM 2"

def Main():
    var = room1()
    if var == 2:
        room2()

So by having return values you can make it a lot easier to see what is going on as there is only one place to look (the Main function) as well as you don't get stuck in some massive recursive loop.

Hope that helps :)

@Paul Thompson: I was thinking about that too, but this would get really complex (and practically impossible) when you want more rooms than just 2 or 3 in your game. Still though, you could create a single loop in the Main() function to catch the return values and executing the corresponding function.

Actually, I think it's not really a good idea to keep this system, since the functions are more or less used as a "label" as seen in eg CMD.

Perhaps ultimatebuster's idea is better, even though it might get a bit more difficult in some situations.

How about you have something that means that if your functions are going to be something along the lines of room1, room2, room3, room4 and such.

So what you could do was return the room number you wanted to go to. eg. 1,2,3,4,5 then you can use the eval statement to make a string of the function such as:

RoomInt = 3
eval("room"+str(RoomInt)+'()')

So that will first give you a string of 'room3()' and then evaluate it which will then call the function. That way you can have soo many functions.

hope that helps

Yeah, that's true. Still, making use of classes would be better in this case when you look at the big picture. Even though I'm not making a huge textgame, I'm afraid things might get a bit complicated if I keep working like this. Making every room an object has the advantage that different variables are separated from each other (think about items that are in certain rooms).

Oh i definitely agree. Classes are the way to go, i was just suggesting an alternative in case you were not comfortable with classes :)

I would myself base the game with dict of places, which would contain dict of things in that place, functions that can be done etc. Of course it would have also list of exists and in case of some special method needed to use the exit, exit function for the room. Program would be one small while loop, like event loop in GUI programs, prompting the next command from user, which I would process to function form to avoid endless if..elsif etc.

Classes could be used where necessary of course.

Hmm, my only problem so far, with any of the above mentioned methods, is that not every room acts the same.

For example, displaying a room description, waiting for user input, and going to the next room with either objects or a dict is pretty simple. But in order to make the game a little more complex, some doors could require a key to be opened.

I don't see a efficient way to do this though.

I said to have actions included in room exit way.
User says 'open door' you check rooms dict for open method and call that method. The method returns error function if not all requirements are met, move function otherwise.
Defining at least room, exit and player as class might be clever way to do it even the whole game player as its state (including his location). Do first one room, test, add neigbor rooms and after testing that do whole map of rooms and tasks.

add triggers to your door.

have an open method in your Door class. This way you can create a LockedDoor from the original Door class and modify the open method to check for a certain key.

class Door:
    def open(self):
        #open action

class LockedDoor(Door):
    def open(self):
        if key in player.inventory:
            #open door
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.