< Prev  |  Home  |  Next >

Python RPG Game

Adding Rooms

This next challenge follows on from our use of more complex data structures in the battleships game. You will create an RPG game, using the help provided in this section.

The aim of the first version of the game is just to ‘walk’ around rooms. Lets create a very simple map for how our world will look:

RPG room plan

Remember that dictionaries link some data (the ‘key’) to another value. We can store information about each room in our game by using a dictionary:

To get the name of the room, we can just type:

Dictionaries can also map keys to other dictionaries, which is handy for giving each room a room number, and associating the room information with that room number key:

We now have a big dictionary containing 2 room keys, with each key linking to another dictionary containing more information (at the moment just the room name).

We can now type:

To see all of the rooms, or:

To see all the information about one room, or:

To find the name of a particular room.

Adding Doors

Let’s add the four rooms to our game:

Notice that our map contains doors, to allow us to move between rooms? We can add them to our room by linking one room to another via a compass direction:

This means that room 1 is linked to room 2 (to the east) and room 3 (to the south). If we’re in room 2, we can go back into room 1 to the west. So, if we are in room 1 we can use:

To get to another adjoining room. Simply type a valid compass direction to move to another room.

Here’s a first version of the RPG game, containing just the rooms:

Notice how the input (stored in the variable “move”) is the word “go” and then a direction, so move[0] is the word “go”, and move[1] is the direction. Making the user type “go”, means that we can add other commands, such as “get”.

Adding Items

Adding an item to a room is easy; we just add it to the dictionary of a particular room. So to add a sword to room 2, we just add it like this:

If we get to a room containing an item, we can display it to the user:

Now if we are in a room with an item, we should be able to type “get sword” to add the sword to our inventory. If we type “get sword” in a room without a sword, we should be told there’s nothing to pick up. Here’s the final version of the game, which you can expand upon to create your RPG:

For your game, you can add any of the following:

  • Add rooms / doors;
  • Add items to rooms;
  • Add new directions (e.g. “up”);
  • Add other things to rooms (e.g. enemies);
  • Add commands (e.g. “drop”, “use”, “fight”);
  • Add game logic (e.g. you die if you enter a room containing an enemy without a sword);
  • Add variables (e.g. “health”, “strength”, “money”);
  • Add battles (e.g. roll dice to decide your fate).
< Prev  |  Home  |  Next >