r/learnpython • u/Attack_On_Tiddys • Jun 27 '23
Another text based game question. Hopefully my last one.
Hello again everyone. So I have yet again another problem I have been trying to fix for the last few hours.
Basically the problem breaks down to this. In my text based game, before you can enter the training grounds, you must give the hungry guard the bread from your inventory. As of now, that works. and the inventory updates to show the bread is no longer in the inventory. Along with removing the bread from the inventory, I remove it from the dictionary called rooms.
everything works except for when you re-enter the training grounds. It then asks you for the bread again, which no longer exists.
My question is how would I write a while, if, elif, etc statement like:
if "Bread" not in rooms["Kitchen"]["Item"]:
continue or something like that, so that you can re=enter the room without it asking for the bread. I'm new so I know my code is probably garbage but I'm using this project to learn.
here is the section of code.
if current_room == "Training Grounds" and "Bread" not in Inventory:
print("A hungry guard blocks your path and shoves you back into the Great Hall.")
current_room = "Great Hall"
continue
if current_room == "Training Grounds" and "Bread" in Inventory:
player_move = input("Would you like to offer the guard bread in exchange for access to the Training Grounds? ")
if player_move == "yes".capitalize():
current_room = "Training Grounds"
print("The guard lets you enter the Training Grounds.")
Inventory.remove("Bread")
del rooms["Kitchen"]["Item"]
continue
elif player_move == "no".capitalize():
print('"Get the hell out of here!"')
print("The guard shoves you back into the Great Hall.")
current_room = "Great Hall"
continueI'm assuming somewhere in here is where I need the line to check for that, and allow you to enter the room. Any help is greatly appreciated.
4
u/gaaasstly Jun 28 '23
I would suggest two things:
Give the Guard an Inventory and place the Bread in it
Give the Training Grounds and Guard flags (e.g. TG.can_enter = True if [you have given Bread to the Guard])
Not only will this allow you to do cool things later, like pickpocket the Guard to get the Bread back, but it's a better design than yet another global variable.
2
u/Attack_On_Tiddys Jun 28 '23
Woah! I hadn’t even considered I could move the bread to a guard_inventory. Thanks so much!
After I get that working, I’ll at some Point try to do your second idea, cause the ability to pickpocket would be super cool too.
2
u/Attack_On_Tiddys Jun 28 '23
Okay so I tried your idea about making a guard inventory and it worked so well! Thanks again! One thing I'm clearly lacking besides structured code, is the programmers mindset, the problem solving brain. Your answer really helped me to start looking at things on a bigger picture.
2
u/K5RTO Jun 28 '23
Thanks for creating this thread. I’m doing a similar project and this made so much sense.
2
u/Attack_On_Tiddys Jun 28 '23
Glad it helped! I ended up doing the guard inventory to fix that problem. However, now I have the issue where the room the bread was originally in is telling me there’s bread again 🤦🏻♂️ one issue after another! Lol
2
u/gaaasstly Jun 28 '23
Just a proof of concept but you can do something like:
player = { "type": "player", "name": "Hero", "inventory": [], } npcs = [ { "type": "npc", "name": "Guard", "inventory": [], }, ] places = [ { "type": "place", "name": "Home", "rooms": [ { "type": "room", "name": "Kitchen", "inventory": [ { "type": "item", "name": "Bread", }, ], }, ], }, ] kitchen = places[0]["rooms"][0] guard = npcs[0] def move_item(obj_src, i, obj_dst): del obj_src[i] obj_dst.append(item) print(player) print(kitchen) print(guard) for i, item in enumerate(kitchen["inventory"].copy()): if item["type"] == "item": answer = input(f"Would you like to take the {item['name']}? ") if answer.lower().startswith("y"): move_item(kitchen["inventory"], i, player["inventory"]) print(player) print(kitchen) print(guard) bread = player["inventory"][0] answer = input(f"Would you like to give the {bread['name']} to the {guard['name']}? ") if answer.lower().startswith("y"): move_item(player["inventory"], 0, guard["inventory"]) print(player) print(kitchen) print(guard)Output
root@13b376ca7340:/home/travis# python test.py {'type': 'player', 'name': 'Hero', 'inventory': []} {'type': 'room', 'name': 'Kitchen', 'inventory': [{'type': 'item', 'name': 'Bread'}]} {'type': 'npc', 'name': 'Guard', 'inventory': []} Would you like to take the Bread? y {'type': 'player', 'name': 'Hero', 'inventory': [{'type': 'item', 'name': 'Bread'}]} {'type': 'room', 'name': 'Kitchen', 'inventory': []} {'type': 'npc', 'name': 'Guard', 'inventory': []} Would you like to give the Bread to the Guard? y {'type': 'player', 'name': 'Hero', 'inventory': []} {'type': 'room', 'name': 'Kitchen', 'inventory': []} {'type': 'npc', 'name': 'Guard', 'inventory': [{'type': 'item', 'name': 'Bread'}]}As you can see, the Bread moves from the Kitchen's inventory to the Player's inventory to the Guard's inventory. Obviously, you'll want to adapt this to your code and not do things like hard-code indices.
11
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jun 27 '23
Have a variable that stores a boolean, defaulting to False, which is used to check if you are allowed to enter that room. When the player gives the bread, set that variable to True.
Seems simple enough.
4
u/ninhaomah Jun 27 '23 edited Jun 28 '23
Re enter ? So you need to keep track of if this is the first time you are entering the training ground.
Have a counter , a boolean.