- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个 Python 文本冒险游戏。它基于《艰难学习 Python》中的 ex41,因此有些相似。然而,我的问题与该练习无关。我正在尝试制作 list ,以便可以拾取和使用元素(即 key 或风风)。
起初我的计划是使用一个 boolean 变量,这样当该项目被“拾取”时,它会将一个值设置为 True,但它似乎不起作用。我认为问题在于,一旦我离开房间,该值就会重置。
现在我正在尝试一个列表,当“拾取”该项目时,该项目将被附加到库存列表中。
我如何创建库存,或者至少“拿起”一个元素,然后“使用”它?
我认为重要的代码行是 18-20(在 def __init__(self, start)
下)、77 行(在 def cell(self) 下)
),161-162(在def janitor(self)
下)。
from sys import exit
from random import randint
import time
prompt = '> '
class Game(object):
def __init__(self, start):
self.quips = [
"Way to go, you died."
"Now you're dead. Sweet.",
"Well isn't this just peachy? You're dead. (It's not peachy.)"
]
self.start = start
#self.smell = 0
#self.Febreeze = False
#self.key = False
self.inventory = []
#if self.smell >=2:
# return 'death'
def play(self):
next = self.start
while True:
print "\n-------"
room = getattr(self, next)
next = room()
def death(self):
print self.quips[randint(0, len(self.quips)-1)]
exit(1)
def intro(self):
print "You wake up."
print "You're in a dark cell."
print "You have no idea who you are or where you are."
print "The door is slightly open."
print "You stagger through the door. The light is blinding."
print "You have just escaped imprisonment and you're on the run."
return 'central_corridor'
def central_corridor(self):
print "In front of you is a long corridor with no doors on the side, but you think you can make out a door at the very end."
print "Behind you is the cell that you have just escaped."
next = raw_input(prompt)
if "cell" in next:
print "You decide to go back into your cell."
return 'cell'
elif "forward" in next:
print "You travel down the corridor towards the door."
return 'front_corridor'
else:
print "That command doesn't exist."
return 'central_corridor'
def cell(self):
print "You're standing in the middle of a musty cell."
print "There is a bed in the corner with a rotting mattress."
print "Under the bed the bricks are loose."
print "In the opposite corner there is a dirty toilet that implies that prison food is even worse than Taco Bell."
print "There are some scratches on the wall next to the toilet."
print "Behind you is an exit into the corridor."
next = raw_input(prompt)
if "toilet" in next:
print "It'd probably be best if this toilet wasn't described."
#self.smell = self.smell + 1
return 'cell'
elif "febreeze" in next and "Febreeze" in self.inventory #and self.Febreeze = True:
print "You use the Febreeze on the toilet to get rid of the odor."
print "Now you can go behind the toilet to read the rest of the scratches."
elif "scratches" in next:
print "The scratches on the wall seem to be tally marks. It goes up to 123. I wonder what it means."
print "You see more scratches behind the toilet, but the stench is too much for you to handle."
print "If only you could get rid of the smell..."
#self.smell = self.smell + 1
return 'cell'
elif "bed" in next:
print "There are various stains on the mattress. Some of the springs are poking up into the mattress. Ouch."
print "Buried between the wall and the mattress is a stuffed animal."
return 'cell'
elif "bricks" in next:
print "You pull the bricks out of the floor and find a few pieces of toilet paper."
print "There is a note written on them in what you hope is dried blood."
print "The note reads:\n -------------------------\nthe closet!! the closet in the walls i'm not \nsure\twhich one it is but its defin-\nly in the hall. i hear it in the bricks!\n---------------------------"
print "Hmm, maybe it's a hint or something."
elif "corridor" in next:
return 'central_corridor'
else:
print "I do not understand how to %s" % next
return 'cell'
def front_corridor(self):
print "You are standing in front of a door."
print "On the side of the door is a keypad."
next = raw_input(prompt)
if "keypad" in next:
return 'keypad'
elif "wall" in next:
return 'wall'
elif "back" in next:
return 'central_corridor'
else:
print "I don't understand %s" % next
return 'front_corridor'
def wall(self):
print "Which wall do you want to check?"
next = raw_input(prompt)
if "left" in next:
return 'left'
elif "right" in next:
return 'right'
elif "back" in next:
return 'front_corridor'
def right(self):
print "You examine the wall carefully, running your fingers across each of the bricks."
print "Unfortunately it doesn't look like anything of value is in this wall."
print "Well, you just wasted some time."
return 'wall'
def left(self):
print "You examine the wall carefully, running your fingers across each of the bricks."
print "One of the bricks seems to be protruding from the wall."
print "Do you push it?"
next = raw_input(prompt)
if next[0] == "y":
print "A few of the bricks shift, revealing a secret door way."
return 'janitor'
elif next[0] == "n":
print "You decide not to push the brick. Good thinking, it may have been a booby trap."
return 'front_corridor'
else:
print "That doesn't exactly make sense..."
return 'wall'
def janitor(self):
print "You are in a room filled with janitorial tools."
print "On your left you see a few cans of Febreeze, a plunger and a Playboy magazine."
next = raw_input(prompt)
if "febreeze" in next:
print "You pick up some Febreeze and put it in your back pocket."
print "Maybe this will be useful somewhere down the line."
self.inventory.append("Febreeze")
#self.Febreeze = True
return 'janitor'
elif "plunger" in next:
print "You try to pick up the plunger but it appears to be stuck in the ground."
print "After tugging for a few minutes the handle comes out, leaving the rubber suction cup plastered to the floor."
print "Who knows what's keeping it there."
print "You decide not to touch it."
return 'janitor'
elif "playboy" in next:
print "Well isn't that nice."
print "These janitors certainly have good taste."
return 'janitor'
elif "back" in next:
return 'front_corridor'
else:
print "Do WHAT with WHAT?"
return 'janitor'
def keypad(self):
print "Above the keypad there is a sign that reads:\n-----------\nInput a 3 digit code.\nWarning: If code is incorrect 3 times, keypad will self-destruct.\n-----------"
code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
guess = raw_input("Password: ")
guesses = 0
while guess != code and guesses < 2:
print "BZZZZZEDD!"
guesses += 1
guess = raw_input("Password: ")
if guess == code:
print "The keypad beeps in acceptance. Wow, that was a good guess"
print "The door swings open."
print "Behind the door is a long bridge suspended over a lake of lava."
return 'bridge'
elif "key" in guess and key == True:
print "You flash your card key across the keypad."
print "There is a beep as the door swings open."
print "Behind the door is a long bridge suspended over a lake of lava."
return 'bridge'
else:
print "The keypad buzzes one last time and then you hear a sickening melting"
print "sound as the lock mechanism fuses together."
print "There is a small clicking while the keypad countsdown."
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print "There is a large explosion and you are caught right in the middle of it."
print "The fiery blast tears your skin from your body as you scream in agony."
return 'death'
def bridge(self):
print "You carefully walk onto the bridge."
print "One false move and you could be dead."
print "Across the bridge is a door leading to the outside world."
next = raw_input(prompt)
if "jump" in next:
print "You jump to your death."
print "Probably not the best idea."
elif "run" in next:
print "You run"
a_game = Game("intro")
a_game.play()
最佳答案
您的方法似乎工作正常,但有一些错误。在 cell()
中,您需要在 elif "febreeze"...
语句之后(在注释开始之前)添加 :
。此外,在 elif
block 的末尾,您需要返回“cell”
。通过这些更改,我可以拿起 Febreeze 并使用它。
关于python - 我正在制作一个 python 文本冒险游戏。需要帮助制作 'inventory',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273619/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!