Stage 1

Introduction

In this tutorial, you’ll be getting used to the development environment, and we’ll be finding out how comfortable you are using Python so we can give the right level of help. We’ll be starting by implementing the first version of our MUD, which will be very limited in functionality, but hopefully simple to implement.

By the end of this tutorial you should:

  • be comfortable writing and running Python code
  • implemented a text adventure game where a single player can move around a predefined world

Aim

Let’s have a look at a map of the world you’ll be implementing:

digraph {
    node [shape=box]

    entrance_courtyard [label="Entrance Courtyard"]
    throne_room [label="Throne Room\n(win)"]
    main_hall [label="Main Hall", color="blue"]
    dungeon [label="Dungeon\n(lose)"]

    main_hall -> entrance_courtyard [label="north"]
    entrance_courtyard -> main_hall [label="south"]

    {rank=same;
    throne_room -> main_hall [label="east", dir="back"]
    main_hall -> dungeon [label="west"]
    }
}

The blue border around the Main Hall room indicates it’s the room which the player starts in. From there, they could type north to go to the Entrance Courtyard, from which they could then type south to come back to the Main Hall. If they type west to go to the Dungeon, they’ll lose the game, but if they type east, they’ll enter the Throne Room and win the game.

So what does a typical run of the game look like? Here’s two possible runs of the game, first one where I lose by going into the dungeon:

You are standing in the main hall, a room with many pillars.
On the sides of the room, you see many sculptures of animals,
people and (strangely enough) boats.
There are exits west, east and north.

> west
You move west into the dungeon
This room is filled with manacles with rotting corpses hanging
from them. Behind you, you hear the jailer's maniacal laugh as
he shuts the door behind you.

You lose.

And now one where I explore the entrance courtyard, and then win:

You are standing in the main hall, a room with many pillars.
On the sides of the room, you see many sculptures of animals,
people and (strangely enough) boats.
There are exits west, east and north.

> north
You move north into the entrance courtyard
You are standing in the entrance courtyard, a place with a
crossroads of paved pathways, lined by miniature pine trees
with tulips surrounding them at the base.
There are exits south.

> north
What?

> south
You move south into the main hall
You are standing in the main hall, a room with many pillars.
On the sides of the room, you see many sculptures of animals,
people and (strangely enough) boats.
There are exits west, east and north.

> east
You move east into the throne room
You sneak into the throne room, and steal the crown!

Congratulations, you've won the game!

Go for it!

At this point, you’re probably itching to get started: if so, go for it! Implement this however you like, and put your hand up to let your tutor know when you’re done.

If this already looks big and scary, and you need some help, that’s perfectly fine too: just put your hand up and your tutor will come over and help you.

Extension

Unless you were particularly careful when you implemented this, your Python code will have lots of descriptions and room names in it. In this extension, we want to move everything about the world out into another file, so that we can make changes to the world and the code independently.

Start by saving the following file as world.json, and then modify your code so it reads the JSON file and uses the room names, descriptions and exits from it.

{
    "start": "main hall",
    "rooms": {
        "throne room": {
            "description": [
                "You sneak into the throne room, and steal the crown!"
            ],
            "exits": {},
            "special": ["win"]
        },
        "main hall": {
            "description": [
                "You are standing in the main hall, a room with many pillars.",
                "On the sides of the room, you see many sculptures of animals,",
                "people and (strangely enough) boats."
            ],
            "exits": {
                "north": "entrance courtyard",
                "west": "dungeon",
                "east": "throne room"
            },
            "special": []
        },
        "dungeon": {
            "description": [
                "This room is filled with manacles with rotting corpses hanging",
                "from them. Behind you, you hear the jailer's maniacal laugh as",
                "he shuts the door behind you."
            ],
            "exits": {},
            "special": ["lose"]
        },
        "entrance courtyard": {
            "description": [
                "You are standing in the entrance courtyard, a place with a",
                "crossroads of paved pathways, lined by miniature pine trees",
                "with tulips surrounding them at the base."
            ],
            "exits": {
                "south": "main hall"
            },
            "special": []
        }
    }
}

How do I read this file?

This file is a JSON file: JSON is just a way of storing complicated objects (specifically: integers, floating point numbers, strings, booleans, null, lists and dictionaries). Python’s amazing standard library comes with the json module for reading JSON files, which you can use like this:

import json

with open('world.json', 'rb') as f:
    world = json.load(f)

print(world)

See also Doug Hellmann’s entry for JSON in Python Module of the Week.

Table Of Contents

Previous topic

Reboot MUD Workshop

Next topic

Stage 2

This Page