0
我認爲我題爲非常糟糕的問題,但我不能真正考慮更好的東西。對不起,(另外,英語不是我的第一語言,所以對我的語法犯罪另一個抱歉)。類是場景。試圖重複現場,如果答案是不考慮
在「Learn Python The Hard Way」的前45頁,我必須製作一些文字遊戲,比如每個房間使用一個課程。我使用ex44中的代碼(幾乎與下面的代碼相同)作爲原型,因爲我真的無法想象三條最終線條是如何工作的並與一切事物互動。對於像我這樣的編程新手來說,我覺得太多了,我甚至試圖在每一步之後逐行寫下來。
此外,我還嘗試使變量current_scene
因此,如果您在if
變量中引入了未考慮的答案,請重複該場景。
from sys import exit
from random import randint
class Engine(object):
def __init__(self, map_scenes):
self.map_scenes = map_scenes
def play(self):
current_scene = self.map_scenes.open_scene()
while True:
print "\n---------"
next_scene_name = current_scene.text()
current_scene = self.map_scenes.next_scene(next_scene_name)
class Scene(object):
def texto(self):
print "Parent class for scenes"
exit(1)
class Death(Scene):
types = [
"You're death.",
"You pass away."
]
def text(self):
print Death.types[randint(0, len(self.types)-1)]
exit(1)
class Again(Scene):
repeat = [
"Can you repeat?",
"Try again.",
"One more time."
]
def text(self):
print Again.repeat[randint(0, len(self.repeat)-1)]
print current_scene # =/
class Intro(Scene):
def text(self):
print "The intro scene"
return 'start'
class Start(Scene):
def text(self):
print "The first scene"
print "Where do you want to go?"
next = raw_input("> ")
if next == "bear":
return 'bear'
elif next == "valley":
return 'valley'
elif next == "death":
return 'death'
else:
return 'again'
class Bear(Scene):
def text(self):
print "The second scene."
print "And so on..."
exit(1)
class Valley(Scene):
def text(self):
print "Alternative second scene."
print "And so on..."
exit(1)
class Map(object):
scenes = {
'intro': Intro(),
'start': Start(),
'bear': Bear(),
'valley': Valley(),
'again': Again(),
'death': Death()
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
return Map.scenes.get(scene_name)
def open_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('intro')
a_game = Engine(a_map)
a_game.play()
我真的希望我已經解釋了自己的好,我會卡在這個練習了幾天,好像我不這樣做毫無進展。