【发布时间】:2017-06-19 20:48:50
【问题描述】:
我创建了一个基本游戏作为学习练习的一部分,我想在学习更多 Python 时扩展它。该游戏是一款非常基本的基于文本的冒险游戏,有些房间允许用户拾取物品。
我想在用户玩游戏时将这些项目写入文本文件,然后让用户选择在游戏期间检查他/她的“库存”。我无法获得使脚本能够执行以下操作的正确语法:
- 在用户开始游戏时创建一个新的文本文件;
- 当用户到达游戏的那个部分时,将定义的项目写入文本文件;
- 创建一个选项来查看库存(我知道如何执行此操作)。
这里是部分脚本的示例,我尝试的代码已被注释掉:
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 如果完整的脚本有用的话。我在这里进行了几次搜索,最接近我认为我需要的问题是this one。我不知道如何有效地实现这一点
我的主要挑战之一是弄清楚如何让脚本动态创建一个新的文本文件,然后用库存中的物品填充该文本文件。
【问题讨论】:
标签: python python-2.7