I created a basic game as part of a learning exercise and I'd like to expand it as I learn more Python. The game is a pretty basic, text-based adventure game and some rooms let users pick up items.
I want to write those items to a text file as the user plays and then give the user an option to check his/her 'inventory' during the game. I can't get the syntax right that will enable the script to do the following:
- Create a new text file when the user starts a game;
- Write defined items to the text file as the user reaches that part of the game;
- Create an option to view the inventory (I have an idea how to do this).
Here is an example of part of the script with my attempt at the code for this commented out:
def room1_creep():
print "You slowly enter the room, and look around. In the opposite corner of the room, you see a ferocious looking bear. It doesn't seem to have seen you yet."
print "You make your way to the chest at the end of the room, checking to see whether the bear has seen you yet. So far, so good."
print "Just as you reach the treasure chest, you hear a roar from the bear that seems to have seen you. What do you do? Rush 'back' to the door or go for the 'treasure'?"
creep_choice = raw_input("You have two choices: 'back' or 'treasure'. > ")
if creep_choice == "back":
print "You make a run for it. You feel the bear's hot breath on the back of your neck but you reach the door before it catches you."
print "You slam the door closed behind you and you find yourself back in the passage."
return entrance()
elif creep_choice == "treasure":
print "You manage to grab a few handfuls of gold coins before the bear stabs its claws into you and sprint for the exit."
# inv = open("ex36_game_txt.txt", 'w')
# line3 = raw_input("10 gold coins")
# inv.write(line3)
# inv.write("\n")
# inv.close()
# I also want to add "gold coins" to a text file inventory that the script will add to.
print "You manage to slam the door closed just as the bear reaches it. It howls in frustration and hunger."
return middle()
else:
room1_indecision()
My script is on GitHub if the full script would be useful. I ran a couple searches here and the question that comes closest to what I think I need is this one. I can't work out how to implement this effectively
One of my main challenges is working out how to get the script to create a new text file dynamically and then populate that text file with the items from the inventory.