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 1 secondOK, so this is how to have a go at some of the challenges.

Skip to 0 minutes and 9 secondsSo I'm going to go back and find the item class, which I created in Week two. And I'm going to copy and paste it so that I've actually got it accessible in my main game. So I need to create myself a new file called item . py. And then I'm just going to paste it in to say I've got everything there. And don't forget to also import it at the start of your main . py so that the class item is available to you. So now I've got an item. Let's head over to room . py. And we're going to need to add the fact that the room can contain an item. So we're adding an attribute called self. item.

Skip to 0 minutes and 46 secondsAnd then we will need to add some getters and setters to be able to let us put an item in the room. So we will need to be able to get item and set item for this room.

Skip to 0 minutes and 59 secondsI'll let you figure out how to sort those since we've done those lots of times before.

Skip to 1 minute and 8 secondsSo now I'm going to go back to main . py. And just like I added an enemy, and then I put an enemy in a particular room, I'm going to create an item. So this item is going to be cheese. And the item's name is cheese. And I'm also going to give it a description, because that was one of the things which I had in my item class. I'm able to give an item a description. So here it is. It's a large and smelly block of cheese, which is quite handy because Dave the zombie doesn't like cheese. And then I'm going to add this into a room. So I'm going to put the cheese in the ball room.

Skip to 1 minute and 45 secondsAnd we do this, just like when we set the character to be in a particularly room. We're going to set the item to be in a particular room with that getter and setter that we just wrote. Now, when we enter the main game loop, we check whether there is a character in each room. And if there is, then we describe it. So we can do something very, very similar with the item. And I'll let you figure out how to do that. I'm just starting it off for you here. So you need to figure out how to discribe the item if there is one. There may not be an item in every single room.

Skip to 2 minutes and 25 secondsSo now that we've got items, we need somewhere to store them. So I'm going to create backpack, which is just at the moment an empty list. I'm going to need to have an extra command. So there is a command, which we can add to take an item, which I'm going to call take. So if somebody types in take, then we have to figure out how we're going to get them to take the item, and put it in the backpack. So we'll have to append it to the backpack array or list. Don't forget to remove the item from the room because we don't want to be able to collect it twice.

Skip to 2 minutes and 58 secondsBut also, don't forget to check if the item is actually there. So we don't want them to be able to take something that doesn't exist. Otherwise, they can just conjure items out of anywhere.

Skip to 3 minutes and 15 secondsAnd then when we fight with the item, then we're going to check, do we actually have this item? Is this item in our backpack? Otherwise, again, they're going to be cheating and fighting with items they don't have. So perhaps you could write some code to check whether the item is there and they can use it, or isn't there, and you might want to tell them you don't have that item. You can't use that.

Skip to 3 minutes and 41 secondsOK, so I'm going to go back to my enemy class that I've already got. And I'm going to add inside it class variable, so that I can store the number of enemies that were defeated. And this is shared between all the enemies. Every single enemy knows how many enemies have been defeated so far. I might want to use this so that when I have a fight with an enemy, I can say I'm going to add one to the number of enemies, which are defeated. And then I can say in my game, well, maybe I have to defeat five enemies to win, or something like that, rather than just having the one enemy fight.

Skip to 4 minutes and 15 secondsAnd it doesn't matter which order I fight them in. I'm going to need some getters and setters for this enemy's defeated variable. The getter is probably more important here because we're going to be setting the enemy's defeated variable plus one whenever we fight an enemy.

Finishing the game

Now it’s time to finish writing your game and put all the concepts you have learnt about into practice. Here are some ideas for things you might want to do. 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

Add the Item class you wrote in week 2 to your game. Follow the same process as for the Character class – add an attribute to Room so that it can store an Item, and then create the Item and assign it to the Room. Inside the game loop, check whether there is an Item in the room, and if so, describe it.


Challenge 2

Add a “backpack” to allow the player to store items. This can be a list in the main part of the program rather than a separate class:

backpack = []

When a player enters a room which contains an Item, the command “take” should put the name of the current room’s Item into the backpack, and also set the Item attribute of the Room to None.


Challenge 3

Change your game so that when the player chooses an item to use in a fight, the game checks whether the player actually has an item with that name in their backpack.


Challenge 4

If you want your game to last a bit longer, you can use a class variable to allow the player to win the game only after they have defeated a specific number of enemies, rather than winning it the instant they defeat a single enemy.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation