Text Based Games

1 Adventure Game Part II

Learn It

  • An integral part of most adventure games is the ability to interact with your environment.
  • You'll notice that on the original map, there were some objects drawn.

map.jpg

  • We're going to need a couple of new functions so that we can view and interact with these objects

Code It

  • Let's start by creating a data structure to hold the items that are in each room.
  • Again, you can adapt this to suit your own game.
roomItems = {'corridor':[],'room1':['torch'],'room2':['knife','pen']}
  • You'll notice that even if the room is empty, we still link it to a list, that is also empty.

Code It

  • Now we'll create a function to show the Items.
def showItems(location):
    items = roomItems[location]
  • The single line of code in the function looks up the room it the dictionary we've created and stores the contents of the room in a new list called items

Badge It - Silver

  • Now we want to print the contents of the room so the player knows what's visible.
  • Remember, you can find the length of the items list by using len(items)
  • Use conditional selection if, elif, else statements so that:
    1. If the length of item is 0
      • print out that there are no items in the room
    2. If the length of the list is 1
      • print out that there is the item in the room using string formatting.
    3. If the length of the list is 2
      • print out that there are two items in the room using string formatting.
    4. Else
      • Leave this option blank for now. We'll code that in the next section

Code It

  • The code for handling multiple items in the room in a grammatically correct way is a little tricky.
  • We want the print statement to read something like
'You see a knife, a pen and a torch here'
  • The code below can be inserted into your else statement from above.
print('You see a ',end=' ')
for i, item in enumerate(items):
    if i == len(items) -1:
	print('and a '+item)
    else:
	print(item + ',',end=' ')
  • It iterates over the list of items, adding a comma after each item is printed.
  • If the item is the final item though, it adds an ='and'= instead.

Code It

  • Now call the showItems() function in each of your room functions.
  • Make sure to pass it the room's name as a string.

Run It

  • Run your game and have a play around.
  • Make sure that all the items are being displayed correctly.

Learn It

  • The chances are we're going to want the player to be able to do more than just move around.
  • We might want the player to be able to:
    1. Take an item
    2. Use an item
    3. Move
    4. Fight
    5. Look around
    6. Anything else you can think of.

Code It

  • Let's make a new function to handle player actions.
def action(location):
    print('What would you like to do')
    print('1. Take an item')
    print('2. Use an item')
    print('3. Leave')
    print('4. Interact')
    print('5. Look again')
    choice = input()
    if choice == '1':
	takeItems(location)
    elif choice == '2':
	useItem(location)
    elif choice == '3':
	commands[Moving(location)]()
    elif choice == '4':
	fight(location)
    elif choice == '5':
	command[location]()
    else:
	print('You can not do that')
	commands[location]()
  • Feel free to add your own choices as well.
  • Add the action() function call into each of your room functions, in place of the Moving() function call.

Badge It - Gold

  • Let's try and implement the takeItems(location) function.
  • You'll need to declare a list, near the top of your code, to act as the players inventory.
playerItems = []
  • Now use the algorithm below, to create the function
def takeItems(location):
  • Follow the following algorithm to code the function.
    1. Set a new variable called items to be the contents of the room
    2. If the length of the items list is 0, then print that the room is empty, and call the action(location) function.
    3. Ask the player which item they want to take.
    4. If the item they want to take is not in the list, then keep asking the player what item they want to take.
    5. If the item is in the list then remove it from the roomItems dictionary.
      • n.b you can use .remove() to remove items from lists.
    6. Add the item to a global list that represents the players inventory.
    7. Tell the player they have taken the item.
    8. Run the action(location) function again.

Badge It - Platinum

  • The rest is up to you now.
  • You can create any functions you like, to perform various actions.
  • You could create a function for using items, fighting, running away. The choices are up to you.