rokman54
rokman54

Reputation: 159

on classes and attributes python

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()

Upvotes: 0

Views: 161

Answers (1)

user1743173
user1743173

Reputation:

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()

Upvotes: 1

Related Questions