Python Forum
Adding an inventory and a combat system to a text based adventure game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding an inventory and a combat system to a text based adventure game
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for the advice! This is exactly what I was looking for.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] adding mouse control to game flash77 2 205 Apr-08-2024, 06:52 PM
Last Post: flash77
  Text-Based RPG - After selecting correct decision it also prints others code KimJiwoo1 1 988 Apr-26-2023, 08:30 PM
Last Post: deanhystad
  Complete beginnner making a text based adventure Mishmaccles 2 2,672 Jul-07-2021, 05:00 PM
Last Post: BashBedlam
  Text Adventure Module Leroyrobenson 1 2,177 Feb-23-2020, 09:57 PM
Last Post: Larz60+
  Adding persistent multiple levels to game michael1789 2 2,407 Nov-16-2019, 01:15 AM
Last Post: michael1789
  Using classes for room movement (text game) Lawr3y 3 6,571 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  [pygame] Inventory items not working SheeppOSU 14 6,737 May-27-2019, 09:44 PM
Last Post: metulburr
  Simple Game - Choice Based theor 3 2,923 May-10-2019, 08:41 AM
Last Post: beLIEve
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 4,012 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  Adding a single player mode to my wxOthello game keames 29 11,973 May-01-2019, 02:56 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020