0

I'm studying a tutorial where it creates a text-based game on python to practice using classes and attributes.

The basic structure is that Scene will be the superclass and the Engine will run to get to the next scene.

I'm having trouble understanding how the last three lines of the command work.

This is as far as I understand.

a_map = Map('central_corridor') creates an instance of class Map which runs the __init__ function in Map thus printing start_scene in __init__ central corridor.

Then a_game = Engine(a_map) creates an instance of Engine and runs the __init__ of Engine thus printing Engine __init__ has scene map, scene_map

Now I don't understand the last part which prints

start_scene in opening_scene
start_scene in next_scene
next_scene returns _____

Then it runs a_game.play()

It would be great if you could explain what happens in the between.

from sys import exit
from random import randint
class Scene(object):
 def enter(self):
 pass 
class Engine(object):
 def __init__(self, scene_map):
 print "Engine __init__ has scene map", scene_map
 self.scene_map = scene_map`
 def play(self):
 current_scene = self.scene_map.opening_scene()
 print "Plays first scene", current_scene
 while True:
 print "\n------------"
 next_scene_name = current_scene.enter()
 print "next scene", next_scene_name
 current_scene = self.scene_map.next_scene(next_scene_name)
class CentralCorridor(Scene):
 pass
class LaserWeaponArmory(Scene):
 pass
class Bridge(Scene):
 pass
class EscapePod(Scene):
 pass
class Death(Scene):
 pass
class Map(object): 
 scenes = {
 'central_corridor' : CentralCorridor(),
 'laserweaponarmory' : LaserWeaponArmory(),
 'Bridge' : Bridge(),
 'escape_pod' : EscapePod(),
 'Death' : Death()
 }
 def __init__(self, start_scene):
 self.start_scene = start_scene
 print "start_scene in __init__", self.start_scene
 def next_scene(self, scene_name):
 print "start_scene in next_scene"
 val = Map.scenes.get(scene_name)
 print "next_scene returns", val
 return val
 def opening_scene(self):
 print "start_scene in opening_scene"
 return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Sep 3, 2014 at 16:57

1 Answer 1

1

To clarify:

start_scene in opening_scene
start_scene in next_scene
next_scene returns _____

And are run as a result of

a_game.play()

The Engine and Map look like this:

Engine
- scene_map <- Scene
- start_scene <- String
+ __init__()
+ next_scene()
+ opening_scene()
Map
- start_scene <- String
- scenes <- Dictionary of Scene
+ __init__()
+ play()

play() calls opening_scene() on scene_map:

self.scene_map.opening_scene()

opening_scene() calls next_scene() with "central_corridor"

self.next_scene(self.start_scene)

next_scene() looks up the correlating Scene in the scenes dictionary with the key "central_corridor" which was passed in

Map.scenes.get(scene_name)

The scene is returned up through the chain of method calls and stored in current_scene

current_scene = self.scene_map.opening_scene()
answered Sep 3, 2014 at 17:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.