python - Classes are scenes. Trying to repeat the scene if the answer is not consider -
i think titled question bad, can't think better. sorry (also, english not first language, sorry grammatical crimes).
in ex45 of 'learn python hard way', have make text game conditions, use 1 class per room. use code in ex44 (almost identical code below) prototype, cause can't undestand how 3 final lines works , interact everything. think novice in programming me, try write down, line line, following every step.
plus that, try make variable current_scene
if introduce answer not contemplate in if
variable, repeat scene.
from sys import exit 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 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 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 want 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 on..." exit(1) class valley(scene): def text(self): print "alternative second scene." print "and 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()
i hope i've explained myself well, stuck in exercise days , seems i'm not doing no progress @ all.
you have modify engine.play
handle that:
next_scene_name = current_scene.text() if next_scene_name in self.map_scenes.scenes: current_scene = self.map_scenes.next_scene(next_scene_name)
Comments
Post a Comment