datahappy
datahappy

Reputation: 856

Python game logic

As many other askers on this site seem to be doing, I am using Learn Python the Hard Way to, well, learn Python.

I am on Lesson 36, in which we create our own BBS-style text game based on the one he led us through in Lesson 35.

http://learnpythonthehardway.org/book/ex36.html

http://learnpythonthehardway.org/book/ex35.html

I wanted to up the "difficulty" of the game, so I made the maze a little more complex. Instead of one entrance in and one out of each room, some rooms have multiple doors. Some of those doors lead to nothing, but some rooms can be entered from multiple rooms within the map.

So, in the monster_room, the player can enter via the monkey_room or the empty_room. The problem is, the monster_room runs the same code, no matter where the player enters from. Since I built the empty_room first, the door choices and results are based on that room.

Here's the monster_room code:

def monster_room():
    print "You have stumbled into a dark room with a giant, but friendly, monster."
    print "There are four doors:"
    print "One straight ahead, one to your right, one to your left, and the one you just entered through."
    print "Which door would you like to choose?"

    door = raw_input("> ")

    if "left" in door:
        dead("The door magically recedes into the wall behind you and you find yourself forever trapped in a black room with no doors, no windows, and no hope of escape.")
    elif "right" in door:
        monkey_room()
    elif "straight" in door:
        dead("You step into the abyss and fall through nothingness to your certain death.")
    else:
        print "You found a magical shortcut to the Treasure Room!"
        treasure_room()

Okay, so pretty simple, right? But, if someone enters from the monkey room, the positions of the doors is different. Left would lead to the empty room, right to the abyss, straight to trapped forever, and back the way you came still a magical shortcut.

I know I could just create a monster_room_2 or something that would only be entered from the monkey_room and have all the doors in the "right place", but I thought there might be a way to have the game give options based on the function that sent them there. Does that make sense?

Any help would be greatly appreciated.

Upvotes: 1

Views: 1178

Answers (1)

stderr
stderr

Reputation: 8732

You could just set a global value for the current room and then use that.

CURRENT_ROOM = "whatever"

def monster_room():
    global CURRENT_ROOM
    if CURRENT_ROOM = "monkey":
        """Display monkey room choices""" 
    else:
        """display other room choices"""
    # don't forget to set your global to the room selected:
    CURRENT_ROOM = new_room

You could certainly use the function instead of strings if you like:

CURRENT_ROOM = monkey_room

However, global variables are a code smell. You're going to be better off using a class, and/or passing the current room in as a variable.

I'd do something like:

class Game:
    def __init__(self):
        self.current_room = self.initial_room

    def initial_room(self):
        ...

    def monster_room(self):
        ...

    def monkey_room(self):
        ...

    def display_room(self):
        self.current_room()

So in the game "loop" you'd create an instance of Game and use it to help keep track of where you are currently and things like that.

Upvotes: 2

Related Questions