Andrew Hansen Help

The Game Engine Pt 3

Additional commands

Before going on with the game, it is time to add some additional commands that the player may need. The player may need to check their inventory, have another look around or examine an item more closely. We will add those commands now.

Inventory

If the player wishes to check what items they are carrying they can type the single command "inventory". This will list all the items in their possession.

The code is very simple:

case 'inventory': if len(player.items) == 0: print('You are not carrying anything.') else: for item in player.items: print(f'You have a {item.name}.')

Testing the code gives:

You are standing in the Quad. From here you can go: North to the admin office. East to the Cleaners office. South to the Canteen. West to the Teachers office. There is a coin here. What would you like to do? inventory You are not carrying anything. What would you like to do? take coin You take the coin. What would you like to do? inventory You have a coin. What would you like to do?

Look

This could be one of two options. "Look" on its own should replay the location description. "Look item" should give the description of the item.

case 'look': if target == None: print(world.grid[player.row][player.col].get_desc()) else: described = False # use this to see if the player was given a description for item in world.grid[player.row][player.col].items: if item.name == target: print(item.get_desc()) described = True for item in player.items: if item.name == target: print(item.get_desc()) described = True if not described: print(f'The {target} is not here to look at.')

Testing the code gives:

You are standing in the Quad. From here you can go: North to the admin office. East to the Cleaners office. South to the Canteen. West to the Teachers office. There is a coin here. What would you like to do? look coin The coin is gold with $1 stamped on it. What would you like to do? look You are standing in the Quad. From here you can go: North to the admin office. East to the Cleaners office. South to the Canteen. West to the Teachers office. There is a coin here. What would you like to do? take coin You take the coin. What would you like to do? look You are standing in the Quad. From here you can go: North to the admin office. East to the Cleaners office. South to the Canteen. West to the Teachers office. What would you like to do? look coin The coin is gold with $1 stamped on it. What would you like to do? look book The book is not here to look at. What would you like to do?

The output here is long but if you look you can see that the player looks at the coin while it is in the quad and gets the description. They then do a plain look and see the quad and the coin. They then take the coin and do another plain look. This time the coin is gone. They look again at the coin and again get the description. Finally, they try to look at the book which is not in the quad or the player's possession and get a negative response, so the code works.

Meet Joe

The first thing the player is going to want to do is talk to Joe. The "talk" command is coded like this:

case 'talk': if target == 'Joe' and world.grid[player.row][player.col].name == 'cleaner': # The player can only talk to Joe in the cleaners office. print(joe.get_response()) else: print(f'{target} is not here to talk to.')

Give

Having now met Joe and established that he is the reason why the player is carrying a picnic we now need to use the picnic to trigger Joe into the satisfied state, so he gives the key.

case 'give': if target == 'picnic' and world.grid[player.row][player.col].name == 'cleaner': # The player can only give the picnic to Joe in the cleaner's office. player.items.remove(picnic) joe.satisfied = True print(joe.get_response()) player.items.append(key) print('You take the key.')

At face value this appears to work well. Testing the code with expected input gives:

You are standing in the cleaners room. There are shelves of chemicals. Joe is in here filling a bucket with water. From here you can go west to the quad. There is a Joe here. What would you like to do? inventory You have a picnic. What would you like to do? talk Joe You will need to get me a picnic bar if you want to borrow my key. What would you like to do? give picnic Thank you for the picnic bar. Here is my key. You take the key. What would you like to do? inventory You have a key. What would you like to do?

But, there is actually a bug in here. It can be seen by talking to Joe again now that he has handed over the key. Here is the output from the "give picnic" command:

What would you like to do? give picnic Thank you for the picnic bar. Here is my key. You take the key. What would you like to do? inventory You have a key. What would you like to do? talk Joe Thank you for the picnic bar. Here is my key. What would you like to do? inventory You have a key. What would you like to do? talk Joe Thank you for the picnic bar. Here is my key. What would you like to do?

Joe keeps offering his key which makes the game look sloppy. Here are two options that you the reader can programme on your own.

The first is to append a new response to Joe's vocab attribute stating "I've given you my key now clear off!" or you could print a final response from Joe directly in the "give" command after the "joe.get_response()" which will be giving the key, do another print literal saying "Now I have to get about my work." and then remove joe from the cleaner location. That way he cannot be spoken to again.

Use

This command is going to be used when accessing the admin office and the teacher's office. The code will be simple since if the player has the key in their possession the command can simply be used to change the state of the location. If the player does not have the key the game can simply tell them that.

case 'use': location = world.grid[player.row][player.col].name # Just to make the next bit easier. if target == 'key' and key in player.items: if location == 'admin': admin.open = True print(world.grid[player.row][player.col].get_desc()) elif location == 'office': office.open = True print(world.grid[player.row][player.col].get_desc()) else: print('You cannot use the key here.') else: print('You cannot unlock anything without the key.')
You are standing outside the Admin office but it is locked. From here you can go south to the quad. What would you like to do? use key You are standing in the Admin office. There is a small safe on the shelves behind the desk. There is a photo of Kurt Cobain on the wall with the letters RIP under it. From here you can go south to the quad. What would you like to do?

Open

This will be used for the safe and the book. Here is the code for the open command.

case 'open': match target: case 'safe': location = world.grid[player.row][player.col] if location.name == 'admin': # have to be where the safe is. combo = input('What is the combination (x x x)? ') if combo == safe.trigger: safe.open = True print(safe.get_desc()) location.items.append(book) else: print('That is not the combination. The safe is still locked.') case 'book': location = world.grid[player.row][player.col] if book in location.items or book in player.items: book.open = True print(book.get_desc())

Testing gives us:

You are standing outside the Admin office but it is locked. From here you can go south to the quad. What would you like to do? use key You are standing in the Admin office. There is a small safe on the shelves behind the desk. There is a photo of Kurt Cobain on the wall with the letters RIP under it. From here you can go south to the quad. There is a safe here. What would you like to do? open safe What is the combination (x x x)? 5 4 94 The safe is open. There is a book here. What would you like to do? look book The book has Teacher passcodes written on the cover. What would you like to do? open book Your teachers passcode is Trieste1960. It is empty. What would you like to do?

The output is a little clunky. Reporting that the book is empty after giving the teacher's passcode is unfortunate, but we can live with it at this point.

Type

We will use this command for the final puzzle which is the computer. The player is expected to enter "type Trieste1960".

case 'type': location = world.grid[player.row][player.col] if location.name == 'office': if target == computer.trigger: computer.open = True print(computer.get_desc()) else: print('Passcode incorrect.') else: print('There is nothing to type on here.')
You are standing outside your teachers office but it is locked. From here you can go east to the quad. What would you like to do? use key You are standing in your teachers office. Surprisingly there is nothing on the desk except his computer. From here you can go east to the quad. There is a computer here. What would you like to do? type fred Passcode incorrect. What would you like to do? type Trieste1960 Your teacher left a message for you. It says "Too late, I already phoned your parents!" GAME OVER. It is empty. What would you like to do?

The end

And that is that. You have written a complete text based adventure game.

Last modified: 07 April 2024