0

I am building a text adventure in python and I would like to have my items affect things in the game, for example, a passageway is too dark to walk down, unless the player holds a lamp in their inventory.

What would be the best way to code this based off my code already?

---This is my rooms and directions to connecting rooms---

rooms = ["hallEnt", "hallMid", "snowRoom", "giantNature", "strangeWall", "riverBank"]
roomDirections = {
    "hallEnt":{"e":"hallMid"},
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
    "snowRoom":{"n":"hallMid"},
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"},
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"},
    "riverBank":{"e":"lilyOne", "w":"giantNature"},
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"},
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"},
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"},
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"},
    "treasureRoom":{"w":"hallEnt"},

---and here are my items and their room locations.---

roomItems = {
    "hallEnt":["snowboots"],
    "snowRoom":["lamp"],
    "treasureRoom":["treasure"],
    }

Another example of my query, I dont want the player to be able to get from "hallMid" to "giantNature" by going (e)ast, unless they hold the "lamp" in their invItems.

kebab
  • 17
  • 5
  • Maybe you can create another dictionary for room requirements. In your 'change_room' function you can check for the inventory and room requirements. – Rik Verbeek Nov 18 '14 at 07:12
  • I understand what you mean, unfortunately Im not a good enough coder to know how to code it, could you give me an example ? – kebab Nov 18 '14 at 07:21

2 Answers2

1

The following example returns an empty list when you are allowed to enter a room. Or creates a list of missing items.

roomRequirements = { "giantNature" : ["lamp"], "snowRoom" : ["snowboots"] }

inventory = [ "lamp" ]

def changeroom (room):
    missing = []
    if room in roomRequirements.keys():
        for item in roomRequirements[room]:
            if item not in inventory:
                missing.append(item)

    print (missing)

changeroom("hallEnt")
changeroom("giantNature")
changeroom("snowRoom")
Rik Verbeek
  • 502
  • 3
  • 8
0

You might want to look into using nested if statements that run each time you enter a room, especially if the effects are passive. Assuming you are storing the value of items with a 1 or 0, you could do this:

lamp = 0
rooms = ["hallEnt", "hallMid", "snowRoom", "giantNature", "strangeWall", "riverBank"]
room = rooms[0]

if lamp == 1:
        connected_room = ["hallMid", "snowRoom"]
        print "You are currently inside the " + room + "."
        print "You can see " + connected_room[0] + " and " + connected_room[1] + " in the distance."
else:
        connected_room = ["snowRoom"]
        print "You are currently inside the " + room + "."
        print "You can see the " + connected_room[0] +  " in the distance."
Matthew
  • 768
  • 1
  • 11
  • 25
  • im a little bit confused, do i put that in my main game loop? – kebab Nov 18 '14 at 08:32
  • I edited the code to include your room lists. If it's a text game, then you will probably want to list the rooms in a special way. I recommend making **lists** inside **if** statements for each room. For example, If hallEnt can only reach the snowRoom, then that should be the only available option to avoid confusing the user. Same goes with how items affect the rooms. – Matthew Nov 18 '14 at 08:47
  • could you show me what that would look like, i have about ten rooms so would it take long? – kebab Nov 18 '14 at 08:50
  • rooms = { 1 : { "name" : "hallEnt" , "west" : 2 } , 2 : { "name" : "hallMid" , "west" : 4 , "south" : 3 } , 3 : { "name" : "snowRoom" , "west" : 3 , "south" : 4 } , 4 : { "name" : "giantNature" , "west" : 3 , "south" : 4 } , } – kebab Nov 18 '14 at 09:07
  • is that a better way to layout the rooms – kebab Nov 18 '14 at 09:07
  • I edited it again to maybe give you a better idea. You can use the if statements to make checks on your lists. You want to design everything so it links up. You can take my if lamp == statement and put it inside another statement that checks what room you are in, etc. When you make a room selection, you could update the room variable. Then you can read that variable to see which list to show for visible rooms. – Matthew Nov 18 '14 at 09:14
  • 1
    yea but can i use this method without having to re write all my code. time is limited – kebab Nov 19 '14 at 01:03