Home > Python > Designing a Text Adventure/RPG Game

Designing a Text Adventure/RPG Game

PoCTagFor the past month, I’ve been coding a text adventure game engine in the python programming language. Unfortunately, I have realize that there are way too many components to the engine that the original Core engine cannot handle it.

The original proof of concept game that I designed is very simple. There are object classes for each of the game element, like a room, a player, and the game itself.

When creating instances of these objects, we pass certain parameters that defines the properties of the objects.

Under the Player object class, there are methods of what the player can do, in this case, there are the go and the look command.  These commands are called by the processing function via the getattr property.

Since the current engine is far more advanced than the original version, I have decided that I’m going to release the original source code. Not that it’s really really good, it’s just that people might be interested for those who are starting with python, as a text adventure game is a cool beginner project.

There are a lot of problems and limitations with this version. Therefore feel free to give suggestions or make modification to use for your own program.

### TEXT ADVENTURE GAME ###
### KKSNETWORK ###
### thekks.net ###

end = False

class Room(object):
    def __init__(self, name, description, north=None, east=None, south=None, west=None, up=None, down=None):
        self.name = name
        self.description = description
        self.north = north
        self.east = east
        self.south = south
        self.west = west

    def __str__(self):
        return self.description

class Player(object):
    def __init__(self, name, currentRoom=None):
        self.name = name
        self.currentRoom = currentRoom

    def look(self):
        print self.currentRoom

    def go(self, direction):
        direction = direction.lower()
        directiontxt = "self.currentRoom."+direction
        try:
            if eval(directiontxt) == None:
                print "You cannot go " + direction + "."
            else:
                self.currentRoom = eval(directiontxt)
                print self.currentRoom

        except AttributeError:
            print "The direction you are going does not exist. Try harder next time!"

class Game(object):
    def __init__(self, name, player):
        self.name = name
        self.player = player
        print "Welcome to "+self.name
        print player.currentRoom
        while end == False:
            cmd = raw_input(">> ")
            self.process(cmd)

    def process(self, cmd):
        global end
        cmdlist = cmd.split()
        if len(cmdlist) == 1:
            cmd = getattr(self.player, cmdlist[0])
            cmd()
        elif len(cmdlist) == 2:
            cmd = getattr(self.player, cmdlist[0])
            cmd(cmdlist[1])
        else:
            print "You can only have a maximum 2 words for your command. Type 'help' for detail."

def main():
    # This is a map of the proof of concept.
    # +-----+-----+-----+
    # |     |     |     |
    # |room1|room2|room3|
    # |     |    |     |
    # +-----+-----+-----+
    # |     |     |     |
    # |room4|room5|room6|
    # |     |     |     |
    # +-----+-----+-----+
    # |     |     |     |
    # |room7|room8|room9|
    # |     |     |     |
    # +-----+-----+-----+
    room1 = Room(name="room1", description="You're in room1")
    room2 = Room(name="room2", description="You're in room2")
    room3 = Room(name="room3", description="You're in room3")
    room4 = Room(name="room4", description="You're in room4")
    room5 = Room(name="room5", description="You're in room5")
    room6 = Room(name="room6", description="You're in room6")
    room7 = Room(name="room8", description="You're in room7")
    room8 = Room(name="room7", description="You're in room8")
    room9 = Room(name="room9", description="You're in room9")
    room10 = Room(name="room10", description="You're in room10")

    room1.south = room4
    room1.east = room2

    room2.south = room5
    room2.west = room1
    room2.east = room3

    room3.south = room6
    room3.west = room2

    room4.north = room1
    room4.east = room5
    room4.south = room7

    room5.north = room2
    room5.east = room6
    room5.south = room8
    room5.west = room4
    room5.down = room10

    room6.north = room3
    room6.south = room9
    room6.west = room5

    room7.north = room4
    room7.east = room8

    room8.north = room5
    room8.east = room9
    room8.west = room7

    room10.up = room5

    room9.north = room6
    room9.west = room8

    adventurer=Player(name="Adventurer", currentRoom=room5)

    game = Game(name="Proof of Concept", player=adventurer)

if __name__ == "__main__":
    main()

For those who prefer a download instead of the entire source code, you can download it, here:

  Simplified Text Adventure Game Engine (3.6 KiB, 138 hits)

Like it? Share it:
  • Digg
  • Reddit
  • StumbleUpon
  • Yahoo! Buzz
  • Technorati
  • Twitter
  • Tumblr
  • Facebook
  • del.icio.us
  • Slashdot
  • Google Bookmarks
  • Ping.fm
  • email
  • Print

 

Your Ad Here
Line Break

Author: Ultimatebuster (89 Articles)

Administrator. Usually found surfing the tubes and hiding in an IRC Chatroom. Occasionally developing in Python or doing web design. Blogging comes 3rd. Oh, follow him on twitter so he can finally settle on more than 100 followers.

  1. May 9th, 2010 at 04:35 | #1

    One possible way to develop the engine further is to separate room, item description from the parser logic. You could do this using YAMLish syntax. Example: http://gist.github.com/395024 .

    I think this would make it quite easy to come up with simplified worlds. Of course the description needs to be bolstered with some ways to define triggers, interactions between items and whatnot but at least it’s one way to look at it.

    I would not be surprised if there were some mature solutions for this problem already as the history of this kind of games goes way back in time.

  2. May 9th, 2010 at 20:12 | #2

    @Juho Vepsäläinen
    I’m currently working on another post, outlining game engine development.
    This article mainly focuses on developing individual games.

  1. May 20th, 2010 at 23:01 | #1