Python Forum
Adding an inventory and a combat system to a text based adventure game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Adding an inventory and a combat system to a text based adventure game (/thread-23214.html)



Adding an inventory and a combat system to a text based adventure game - detkitten - Dec-17-2019

Hello,

I am relatively new to Python and have started making a text-based adventure game. I have made the basic movement part of the game using a script similar to this:
rooms = {
    'kitchen': {
        'name': 'your kitchen', 
        'east': 'bedroom', 
        'north': 'bathroom',
        'text': 'You see your familliar kitchen bench.'},
    'bathroom': {
        'name': 'your bathroom', 
        'east': 'lounge', 
        'south': 'kitchen',
        'text': 'You see your toilet and basin, and Bob the rubber duck.'},
    'lounge': {
        'name': 'your lounge room', 
        'west': 'bathroom', 
        'south': 'bedroom',
        'text': 'You see your couch, which is facing your tv.'},
    'bedroom': {
        'name': 'your bedroom', 
        'north': 'lounge', 
        'west': 'kitchen',
        'text': 'You see your cozy bed piled with teddies.'}
    }
directions = ['north', 'south', 'east', 'west']
current_room = rooms['kitchen']

while True:
    print()
    print('You are in {}.'.format(current_room['name']))
    print(current_room['text'])
    command = input('\nWhat do you do? ').strip()
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            print("You can't go that way.")
    elif command.lower() in ('q', 'quit', 'exit'):
        break
    else:
        print("I don't understand that command.")
I want to add an inventory system and a combat system to the game, but am not sure how to do that.

Thanks,
detkitten


RE: Adding an inventory and a combat system to a text based adventure game - ichabod801 - Dec-17-2019

That looks familiar, it's basically the second code bit in my text adventure tutorial. If you want an inventory system, there's one in the third piece of code in that same tutorial. It adds a 'contents' key to each room dictionary, with a list of words for the things in the room. Then there is an overall 'carrying' list to show what the player is carrying. It's pretty rudimentary, but it's a start.

If you want to add a combat system, you will probably want to start distinguishing things that can fight from things that can't. At that point, you would probably want to switch the items from just being strings to be dictionaries themselves, so they could contain different attributes (key/value pairs). What I would do is have the item dictionary be something like this:

items = {'bone': {'description': 'A long leg bone, good for hitting things with',
        'contents': None,
        'hp': None,
        'attack': []},
    'box': {'description': 'A cardboard box.',
        'contents': [],
        'hp': None,
        'attack': []}
    'Bat Boy': {'description': 'A teenager with huge ears and fangs.',
        'contents': None,
        'hp': 18,
        'attack': [1, 1, 2, 3]}}
Now, the box and the bone both have 'hp' set to None and 'attack' set to an empty list. These could be used to determine that those items can't get into combat. 'Bat Boy', OTOH, has both, and they can be used for a combat system (the idea I'm going with here is that random.choice(items['Bat Boy']['attack']) would give you the damage of Bat Boy's attack).

You will note that I also gave each item a 'contents' key. If item['contents'] is None, it is not a container, and you can't put other things in it. Otherwise, you can put other items (like the bone) into the box. This is a common thing in interactive fiction.

This is just an off-the-top-of-my-head sketch of how things might work. But hopefully it gives you some ideas on how to set up your own systems.


RE: Adding an inventory and a combat system to a text based adventure game - detkitten - Dec-17-2019

Thanks for the advice! This is exactly what I was looking for.