gpt4 book ai didi

python - 被 Gothon 运动游戏 Python 迷惑

转载 作者:行者123 更新时间:2023-11-28 22:44:51 27 4
gpt4 key购买 nike

从 learningpythonthehardway 开始,我已经进入了文字冒险游戏,我有一些代码,我理解它们执行的内容,但我不明白如何执行。下面是我缩小到单个 .py 文件中的整个代码:

from sys import exit 
from random import randint

class Scene(object):
def enter(self):
print "This isn't configured yet. Subclass and implement enter()"
exit(1)

class CentralCorridor(Scene):

def enter(self):
print "Something."
action = raw_input("> ")

if action == "shoot!":
print "Something something."

return 'death'


class Death(Scene):

dead = [
"You died. That sucks ...",
"You shouldn't have died here...that was dumb.",
"You L00se.",
"Even a cow's moo sounds smarter than you."
]

def enter(self):
print Death.dead[randint(0, len(self.dead)-1)]
exit(1)

class Engine(object):

def __init__(self, scene_map):
self.scene_map = scene_map #sets scene_map from the a_map variable

def play(self):
current_scene = self.scene_map.opening_scene()#sets aMap.opening_scene() and initiates CentralCorridor
last_scene = self.scene_map.next_scene('finished') #for the last win scene Finished()
while current_scene != last_scene:
next_scene_name = current_scene.enter() #if scene's not last, call enter() of current scene to next scene name
current_scene = self.scene_map.next_scene(next_scene_name)#Set current_scene to instance of scene_map with self, next_scene_name as parameters

current_scene.enter()

class Map(object): #initially central_corridor

scenes = {
'central_corridor':CentralCorridor(),
'death':Death(),
}

def __init__(self, start_scene):
self.start_scenes = start_scene #central_corridor


def next_scene(self, scene_name):
val = Map.scenes.get(scene_name) #sets CentralCorridor() to val
return val

def opening_scene(self):
return self.next_scene(self.start_scenes) #returns val from
#next_scene which is
#CentralCorridor()


a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

在这段代码中,我有一些我没有得到的东西(请耐心等待。)

  1. CentralCorridor类下的“return 'death'”如何在Map类的场景下引用字典,返回Death类?当我打字时!它似乎通过 Engine 类并执行 current_scene.enter() 但我没有在 CentralCorridor 类的任何地方调用 Engine 类的播放函数。

  2. 在Map类的opening_scene函数中,究竟是如何返回 self.next_scene(self.start_scenes) 工作???我知道它的作用,但 self.next_scene(self.start_scenes) 位让我有点困惑。

感谢任何人解释这一点!

最佳答案

我也是前几天刚刚在书上接触到这个练习,花了一些时间寻找答案。这个网站真的很不错。让我试着解释一下,因为它加强了我的理解。如果我有任何错误,请随时纠正我。

回答你的问题,我不妨从以下开始解释整个事情:

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

a_map是-一个以'中央走廊'为参数的 map 。

在 Map 类中(阅读 # 注释)

def __init__(self, start_scene):
self.start_scenes = start_scene #start_scene is set to 'central corridor'

def next_scene(self, scene_name):
val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
return val #return and pass to next_scene as a method.
def opening_scene(self):
return self.next_scene(self.start_scenes) #return and pass to opening_scene

由于self.start_scene被设置为参数'central corridor',opening_scene将通过next_scene中说明的方法在类Map中的场景库中搜索'central corridor',这是get()函数用于定位事物图书馆。继续...

a_game = Engine(a_map)

a_game是一个以a_map为参数的Engine,而a_map以'central corridor'为参数。因此 a_game 也将“中央走廊”作为参数。逻辑 A=B 和 B=C 因此 A=C,并且 a_game 可以作为继承访问类 Map 和 Engine。你可以在本章之后的 learningpythonthehardway 的下一章中看到它。

在 Engine 类中(阅读 # 注释)

def __init__(self, scene_map):
self.scene_map = scene_map #self.scene_map has set to 'central corridor'
def play(self):
current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explained in class Map, accessible by a_game
last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
#I didn't see any 'finish' scene in map. I think you should have added it in scenes.

注意这里!您将从 enter 函数获得返回值。

    while current_scene != last_scene: #enters the while loop!! This is where the game starts.
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)

current_scene.enter()

这里是理解整个游戏过程的关键点:

next_scene_name = current_scene.enter()

现在您在 CentralCorridor() 函数中,您可以做出选择。因为你的代码不完整,所以在这种情况下我们假设 action == 'shoot!'这让你“死亡”作为返回值并将其传递给 next_scene_name。现在 next_scene_name 已设置为“死亡”。如果您在这里做出其他选择(如果您对它们进行编码),则可能需要设置其他场景。

然后

current_scene = self.scene_map.next_scene(next_scene_name)

通过使用前面解释过的 next_scene 函数在类 Map 中搜索场景来设置 current_scene。在这种情况下,搜索 CentralCorridor() 的返回值“death”,将在类 Map 中的 scenes 库中找到 death() 函数。

只要 current_scene != last_scene,游戏就会继续 while 循环。这就是为什么您需要场景库中的“完成”场景。

关于python - 被 Gothon 运动游戏 Python 迷惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129219/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com