r_metal r_metal - 24 days ago 6
Python Question

Why module is not seeing a class inside the engine code?

I'm writing a little game here. I have described game scenes in .py files to import them as modules in order to make the code cleaner.
I've also tried to put the Scene definition into a separate .py file and import it as module inside those modules and even with import it with the modules in engine code. The error is the same:

Traceback (most recent call last):
File "engine_chapter_1.py", line 4, i
import module_intro
File "C:\Users\r.hrytskiv\Documents\P
class Intro(Scene):
NameError: name 'Scene' is not defined


Here's the engine code itself:

from sys import exit
from random import randint

import module_intro
import module_house_one
import module_package_zoom
import module_house_two
import module_unboxing
import module_key_letter
import module_letter_key
import module_house_three

class Scene(object):

def enter(self):
print "Subclass and implement"
exit(1)

class EndGame(Scene):

cast = [
"Roman Hrytskiv"
"Dmytro Litvyn"
]

def enter(self):
print EndGame.cast
exit(1)

class Engine(object):

def __init__(self, scene_map):
self.scene_map = scene_map

def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')

while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)

current_scene.enter()

class Map(object):

scenes = {
'intro': Intro(),
'house_one': HouseOne(),
'package_zoom': PackageZoom(),
'house_two': HouseTwo(),
'unboxing': Unboxing(),
'key_letter': KeyLetter(),
'letter_key': LetterKey(),
'house_three': HouseThree(),
'end_game': EndGame()
}

def __init__(self, start_scene):
self.start_scene = start_scene

def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val

def opening_scene(self):
return self.next_scene(self.start_scene)

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


And one of the modules:

class Intro(Scene):

def enter(self):
print "City of Bend, OR. Morning. Nowadays."
print "City panorama, people on the streets, around noon."
print "Roland is the hero of this story. He is 27 years old guy."
print "He has recently left University. He's always looking for"
print "something interesting and fun. Doesn't like to sit in one place."
print "For now he's unemployed and struggling to pay for rent so"
print "he decided to sell some stuff from his house. He went to the"
print "attic to check if there is something he might sell."
print "He found some antique stuff and went to pawn shop..."
return 'house_one'


UPDATE 1

new engine code:

from sys import exit


from random import randint

import module_intro
import module_house_one
import module_package_zoom
import module_house_two
import module_unboxing
import module_key_letter
import module_letter_key
import module_house_three
import module_end_game

class Engine(object):

def __init__(self, scene_map):
self.scene_map = scene_map

def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')

while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)

current_scene.enter()


class Map(object):

scenes = {
'intro': Intro(),
'house_one': HouseOne(),
'package_zoom': PackageZoom(),
'house_two': HouseTwo(),
'unboxing': Unboxing(),
'key_letter': KeyLetter(),
'letter_key': LetterKey(),
'house_three': HouseThree(),
'end_game': EndGame()
}

def __init__(self, start_scene):
self.start_scene = start_scene

def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val

def opening_scene(self):
return self.next_scene(self.start_scene)

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


new modules code:

import module_scene


class Intro(module_scene.Scene):

def enter(self):
print "City of Bend, OR. Morning. Nowadays."
print "City panorama, people on the streets, around noon."
print "Roland is the hero of this story. He is 27 years old guy."
print "He has recently left University. He's always looking for"
print "something interesting and fun. Doesn't like to sit in one place."
print "For now he's unemployed and struggling to pay for rent so"
print "he decided to sell some stuff from his house. He went to the"
print "attic to check if there is something he might sell."
print "He found some antique stuff and went to pawn shop..."
return 'house_one'


SOULTION:
just had to reference them throught package_name.

scenes = {
'intro': module_intro.Intro(),
'house_one': module_house_one.HouseOne(),
'package_zoom': module_package_zoom.PackageZoom(),
'house_two': module_house_two.HouseTwo(),
'unboxing': module_unboxing.Unboxing(),
'key_letter': module_key_letter.KeyLetter(),
'letter_key': module_letter_key.LetterKey(),
'house_three': module_house_three.HouseThree(),
'end_game': module_end_game.EndGame()
}

Answer

First, you should move the Scene class to some other file/package.

To use it in your module_intro you have to import it there by:

 from package_name import Scene

or:

import package_name

(but then use Scene as package_name.Scene)

Keeping Scene implementation in main project file will leed to create a circular reference (two modules importing each other)

Comments