【发布时间】:2016-10-09 19:27:02
【问题描述】:
我对 Python 编程有点陌生,我正在尝试创建一个文本冒险游戏。我假设我使用的是 2.7.3 版?
2.7.3(默认,2015 年 6 月 22 日,19:33:41) [GCC 4.6.3]
我想制作一个具有最大尺寸的库存系统。我目前正在使用我的库存清单。
我也想让一些功能成为一次性的东西;您只能选择一项而不能选择另一项。
或者也许删除一个函数在它被执行之后?
我认为这些与课程有关,但我在这方面并不是很先进。这是我的代码/游戏的示例。我希望玩家只选择一种武器种类。
由于我有很多while True 循环,可以肯定地说玩家可以返回并选择另一个weaponKind。
inventory = ['pen','apple']
import time
while True:
a2 = raw_input("You begin to download it. The progress bar seems very small, so you decide you'd better do something as time passes. What will you do?" + '\n' + "==> Decide strife specibus" + '\n' + "==> Examine items in (on?) shelf." + '\n' + "==> Doodle on arm" + '\n' + "==> Check inventory" + '\n'+ "==> End game.").upper()
if a2 == 'DOODLE ON ARM':
print("You're a natural tattoo artist. Those stick figured dinosaurs rock.")
if a2 == 'END':
quit()
if a2 == 'CHECK INVENTORY':
print(inventory)
continue
if a2 == 'STRFE':
strife = raw_input("You assume you have to fight monsters in this game. You've heard this is a real-life thing, no VR junk and whatnot. Where will you scour for a weapon? " + '\n' + "==> Scour in the kitchen." + '\n' + "==> Scour in your room. " + '\n' + "==> Scour in the yard.").upper()
if strife == 'KITCHEN':
while True:
a2 = raw_input("You begin to download it. The progress bar seems very small, so you decide you'd better do something as time passes. What will you do?" + '\n' + "==> Decide strife specibus" + '\n' + "==> Examine items in (on?) shelf." + '\n' + "==> Doodle on arm" + '\n' + "==> Check inventory" + '\n'+ "==> End game.").upper()
if a2 == 'DOODLE ON ARM':
print("You're a natural tattoo artist. Those stick figured dinosaurs rock.")
if a2 == 'END':
quit()
if a2 == 'CHECK INVENTORY':
print(inventory)
continue
if a2 == 'STRFE':
strife = raw_input("You assume you have to fight monsters in this game. You've heard this is a real-life thing, no VR junk and whatnot. Where will you scour for a weapon? " + '\n' + "==> Scour in the kitchen." + '\n' + "==> Scour in your room. " + '\n' + "==> Scour in the yard." + '\n').upper()
if strife == 'KITCHEN':
while True:
kitchen = raw_input("You lazily go downstairs to the kitchen. There's a plate of *shudder* Betty Crocker cookies on the island. There's the cupboard full of kitchen supplies where your weapon will be. There's also the fridge. The window suggests it's a bit before noon. What will you do?" + '\n' + "==> Eat cookies" + '\n' + "==> Examine cupboard" + '\n' + "==> Open fridge." + '\n' + "==> Exit Kitchen" + '\n').upper()
if kitchen == 'EAT COOKIES':
print("You hesitate for a moment before grabbing a cookie. Curse that Crocker Corp and its manipulating ways!")
def cookie(food):
cookie = food(name = 'BC cookie')
inventory.append('BC cookie')
print(inventory)
time.sleep(3)
if kitchen == 'EXAMINE CUPBOARD':
while True:
specibi = raw_input("There's a mixer, cheese grater, and knife." + '\n' + "> Choose mixer." '\n' + "> Choose knife." + '\n' + "> Choose grater." + '\n').upper()
if specibi == 'MIXER':
def mixerKind(strife):
mixerkind = strife(name = "BETTY CRACKER")
inventory.append("BETTY CRACKER")
print(inventory)
time.sleep(3)
if specibi == 'KNIFE':
def knifeKind(strife):
knifeKind = strife(name = "BETTY CRACKER")
inventory.append("Kitchen Knife")
print(inventory)
time.sleep(3)
break
if kitchen == 'EXIT':
break
if strife == 'ROOM':
pers = raw_input("You find an item related to your interest in archery which is a ")
if pers == pers:
def originalKind(strife):
ogkind = strife(name = pers)
inventory.append(str(pers))
print(inventory)
【问题讨论】:
-
不是“删除”一个函数,而是使用
if语句来检查该函数之前是否被调用过,并在if正文中相应地调用它。 -
您使用“函数”一词,但不要在代码中使用任何您自己的词。您应该阅读在 Python 中创建函数,因为它会减少代码中的大量重复
-
听起来你真的需要看看类。您可以拥有一个包含清单列表的玩家类和一个指向清单中的对象之一的 current_item。您还可以在此类中存储一些状态布尔值,这将允许您稍后使用标准
if语句禁止某些操作,这将具有“删除函数”的效果。 -
使用一些布尔变量来控制代码 - 即。
can_choose_weapon = True。其他方法:在开始时设置weapon = None和后来的If weapon is not None: print("you can't get another weapon') else weapon = "knife"
标签: python python-2.7 adventure