Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Object-oriented Programming in Python: Create Your Own Adventure Game. Join the course to learn more.

Skip to 0 minutes and 4 seconds OK, so we’re going to add some code here at the bottom inside our game loop to make it so that the user can add different commands. So here, I’m saying that if the command they’ve given is north, south, east, or west, then that obviously means that they want to move. So we’re going to let them move from the current room to another room if there is a room in that direction. So we’re just using the same code that we had already just there. However, we might want to allow them to do different things other than moving.

Skip to 0 minutes and 41 seconds So for the first challenge, you might want to add extra elif to say what if they want to talk to the enemy or the inhabitant of the room. It might not be an enemy. It may be a character. So what would we get the command to do there if they type in talk? That’s something that you have to figure out. There may be other things that they want to do. So they may also want to fight with the inhabitants of the room as well. So we can add another elif and say, if they’ve typed in to fight, then what are we going to do? What’s the loop going to do?

Skip to 1 minute and 19 seconds So if we have a look inside the character class, and we look at the fight method that we wrote within enemy, we can see that it returned either true or false depending on whether you won the fight, which is quite useful, because we can actually make the game end if we want to if they lose a fight with an enemy. So here, I’m just adding the exact same code that we had before when we were fighting with the enemy in our test file. So I’m asking the user what they want to fight with. And then I’m letting them input whatever the item is which they wish to fight with.

Skip to 1 minute and 51 seconds Then I’m calling the fight method on the inhabitant to fight with the item, which they have described. We can use the fact that the fight method on the inhabitant returns true or false to say, well, if it returned true, i.e. They won the fight, what should we do? And if it returns false, i.e. they lost the fight, then maybe we want to do something else.

Skip to 2 minutes and 19 seconds We can add a Boolean, a variable which can be true or false, to see whether we are dead or not. So if we are dead, then obviously, we can’t continue with the game. So now the game will only continue while we’re not dead.

Skip to 2 minutes and 38 seconds Another thing that we could do is we could add another character so that we don’t just have Dave. So in this case, I’m going to add a new character, which is going to be, rather than an enemy, is going to be Katrina, the friendly skeleton. And she’s going to be a character.

Skip to 3 minutes and 0 seconds Here, you can see that I’m asking her some conversation so that if somebody calls the talk method upon her, then we can talk to her and get a response. You could add lots of other things as well. So I could go and add extra methods if I wanted to, to my enemy class. For example, here, I’m adding a steal method, where I just print out that I steal from the character. But really, you could add something else to this. You could find out whether the character has an item, for example, by adding an attribute self . has item.

Skip to 3 minutes and 33 seconds And then you could decide whether to let that person steal it all the time, or maybe on a random dice roll, or something like that. It’s completely up to you.

Skip to 3 minutes and 47 seconds Another thing I could do is I could add a completely new class. Here, I’m adding a friend class, which extends the character class. I can use the same initialisation as I did for enemy. So I’m just going to copy and paste it in here rather than typing it out again. I don’t want to have the weakness though, because that’s something that which I had for enemies. However, I may add any other attributes that I want a friend to have. In this case, I might want a friend to have a feeling so that I can describe how this person is feeling.

Skip to 4 minutes and 22 seconds Here, I’m going to add another method, just as an example, so that you can hug a friend. So instead of calling a fight method on a friend, you may want to hug a friend. And in this case, it just reports that the person hugs you back. But you could do all manner of different things with this. Again, it’s up to you and your imagination.

More interactions


At the moment our game only allows us to move between rooms using the basic loop we wrote at the end of week 2:

current_room = kitchen				

while True:		
    command = input("> ")		
    current_room = current_room.move(command)	

Now that we have characters, let’s expand this loop so that it performs different actions depending on the command that is given. Here is some code to start you off:

command = input("> ")

# Check whether a direction was typed
if command in ["north", "south", "east", "west"]:
    current_room = current_room.move(command)
elif command == "talk":
    # Add code here

Here are some challenges for you to try. There are helpful hints in the video, and if you would like to check how we solved them, example solutions are shown in the next step.

Challenge 1 challengeicon

Add some functionality to the game loop so that different commands can be recognised. For example, your program should react to the following user inputs:

  • “talk” – talk to the inhabitant of this room (if there is one)
  • “fight” – ask what the player would like to fight with, and then fight the inhabitant of this room (if there is one)

Challenge 2 challengeicon

If you lose a fight with an enemy, the game should end. Hint: the fight method we wrote in the Enemy class returns True if you survive, and False if you do not.

Challenge 3 challengeicon

Add an additional enemy character to inhabit a different room in your game. Perhaps you might want to add more methods in the Enemy class in order to be able to interact with enemies in other ways. For example, can you steal from an enemy, bribe an enemy, or send it to sleep?

Challenge 4 challengeicon

For a bigger challenge, why not extend the Character class in a different way? Perhaps you could create a Friend subclass with some custom methods and attributes. For example, you could hug the friendly character, or offer them a gift. You can use the built in Python function isinstance() to check whether an object is an instance of a particular class.

Share this video:

This video is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation