Andrew Hansen Help

The Game Engine Pt 2

The player input parser

The player input is mostly going to be in the form of "action target". For example, "go north", "take coin", "talk Joe". However, there will be a couple of single action inputs. For example, "help", "inv", "look". The first function we need to write is one the takes the user input and determines the action and, if required, the target.

The code for the input parser is:

def parse_input(input_str): str_parts = input_str.split(' ') if len(str_parts) == 1: action = str_parts[0] target = None else: action = str_parts[0] target = str_parts[1] return action, target

To be able to use these they need to be returned into variables:

user_input = input('What would you like to do? ') action, target = parse_input(user_input) print('Action=', action, 'Target=', target) # TODO Remove this line when finished

Note that we have a TODO item to remove the diagnostic print once we are confident it is working properly.

Moving

In olden days we may have used a series of IF statements to handle the input. In this case we will use the CASE-MATCH syntax. The first task to write and test is player movement around the world.

while True: print(world.grid[player.row][player.col].get_desc()) # print the details user_input = input('What would you like to do? ') action, target = parse_input(user_input) print('Action=', action, 'Target=', target) # TODO Remove this line when finished match action: case 'go': if target in world.grid[player.row][player.col].exits: match target: case 'north': print('go north') # TODO Diagnostic. Remove when working player.do_move('north') case 'south': print('go south') player.do_move('south') case 'east': print('go east') player.do_move('east') case 'west': print('go west') player.do_move('west') else: print('You cannot go that way.')

The following output is a bit long but it demonstrates testing movement from the quad to all four locations and back again. Finally, there is an attempt to go in an illegal direction that is caught by the IF-ELSE statement.

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? go north Action= go Target= north go north 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? go south Action= go Target= south go south 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? go south Action= go Target= south go south You are standing in the canteen. There is a vending machine here. For $1 you can buy a choc-chip cookie, a picnic or chips. From here you can go north to the quad.There is a vending machine here. What would you like to do? go north Action= go Target= north go north 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? go east Action= go Target= east go east 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 quadThere is a Joe the cleaner here. What would you like to do? go west Action= go Target= west go west 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? go west Action= go Target= west go west 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? go west Action= go Target= west You cannot go that way. 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?

Two things this output show us. The first is need to check the newline characters as there are some lines that require a line feed. The second is the imperfect grammar of some of the statements. We will see how many we can fix later.

It is also clear that the ACTION - TARGET parsing is working, so we will remove the diagnostic lines.

Taking

Since the coin is in the quad we should code the take command next. This case section is in line with the case 'go' section.

case 'take': taken = False # Assume the item is not there or cannot be taken for item in world.grid[player.row][player.col].items: if item.name == target and item.collectable == True: taken = True # The player can take that item print(f'You take the {target}.') world.grid[player.row][player.col].items.remove(item) player.items.append(item) if not taken: print(f'You cannot take the {target}.')

When we run the game and test it we find that the code works properly but there is an unfortunate behaviour in our code.

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. 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?

Note that after we take the coin we get the complete recap of the location. This is not needed again and is probably only needed after the player moves, so we will make a couple of small modifications to prevent this.

Tweaking the move

Modify the code at the top of the game loop to look like this:

relocated = True # We are in a new location while True: if relocated: # If we are in a new location ... print(world.grid[player.row][player.col].get_desc()) # print the details relocated = False # Now that we are here we don't need the details again until we move user_input = input('What would you like to do? ') action, target = parse_input(user_input)

The code in the case 'go' will need to be modified as well.

case 'go': if target in world.grid[player.row][player.col()].exits: match target: case 'north': player.do_move('north') case 'south': player.do_move('south') case 'east': player.do_move('east') case 'west': player.do_move('west') relocated = True # We have moved to a new location. else: print('You cannot go that way.')

Testing the game shows the behaviour is now fixed:

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? go south You are standing in the canteen. There is a vending machine here. For $1 you can buy a choc-chip cookie, a picnic or chips. From here you can go north to the quad.There is a vending machine here. What would you like to do?

Now that we have the coin it's time to get the picnic

Buying

The game requires the player to buy a picnic from the vending machine. If they buy anything else they will not be able to swap it for the key when they visit Joe so they have to get a picnic regardless of what they try to buy.

Here's the code for the case 'buy' section:

case 'buy': if world.grid[player.row][player.col].name == 'canteen': # If the player is in the canteen if coin in player.items: match target: case 'picnic': player.items.append(picnic) player.items.remove(coin) vending.open = False # This swaps the desc to the one without the picnic print('You take the Picnic.') case 'cookie' | 'chips': player.items.append(picnic) player.items.remove(coin) vending.open = False print(f'The machine gave you a Picnic instead. Must be faulty.') case _: # This is a wildcard and will match all other inputs print('There are no {target}\'s available.') else: print('You do not have any coins.') else: print('You cannot buy anything here.')

Here is the test output:

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? go south You are standing in the canteen. There is a vending machine here. For $1 you can buy a choc-chip cookie, a picnic or chips. From here you can go north to the quad.There is a vending machine here. What would you like to do? buy chips You do not have any coins. What would you like to do? go north 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? go south You are standing in the canteen. There is a vending machine here. For $1 you can buy a choc-chip cookie, a picnic or chips. From here you can go north to the quad.There is a vending machine here. What would you like to do? buy chips The machine gave you a Picnic instead. Must be faulty. What would you like to do?
Last modified: 07 April 2024