Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class Engine(object):
	def __init__(self, scene_map):
		print "Inside Engine.scene_map"
		print "scene_map: %r" %scene_map
		self.scene_map = scene_map
	def play(self):
		print "Inside"
		current_scene = self.scene_map.opening_scene()
		print "current_scene: %r" %current_scene
		while True:
			print "\n------------"
			print "Inside Engine.while_loop"
			next_scene_name = current_scene.enter()
			print "After current_scene.enter()"
			current_scene = self.scene_map.next_scene(next_scene_name)
			print "After scene_map.next_scene()"
class CentralCorridor(Scene):
	def enter(self):
		action = raw_input("> ")
		if action == "shoot!":
			print "Quick on the draw you yank out your blaster and fire it at the Gothon."
			print "You are dead. Then he eats you."
			return 'death'
		elif action == "dodge!":
			print "He eats you."
			return 'death'
		elif action == "tell a joke":
			print "You jump through the Weapon Armory door while he laughing."
			return 'laser_weapon_armory'
			print "DOES NOT COMPUTE!"
			return 'central_corridor'
class Map(object):
	scenes = {
		'central_corridor': CentralCorridor(),
		'laser_weapon_armory': LaserWeaponArmory(),
		'the_bridge': TheBridge(),
		'escape_pod': EscapePod(),
		'death': Death()
	def __init__(self, start_scene):
		print "Inside Map.start_scene"
		print "start_scene: %r" %start_scene
		self.start_scene = start_scene
	def next_scene(self, scene_name):
		print "Inside Map.next_scene"
		print "scene_name: ", scene_name
		return Map.scenes.get(scene_name)

	def opening_scene(self):
		print "Inside Map.opening_scene"
		print "start_scene: %r" %self.start_scene
		return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
print "after a_map"
a_game = Engine(a_map)
print "after a_game"
print "after"

I am a beginner. Having some doubts on a code i saw in a book.
Can anyone explain to me why next_scene() is under scene_map as follows
current_scene = self.scene_map.next_scene(next_scene_name)
instead of Map.next_scene?

when i print scene_map as per
print "scene_map: %r" %scene_map
, i get
<__main__.Map object at 0x1028504d0>
as result, can explain what it means?

self.scene_map is an instance Map. When you create the engine, you pass it a Map instance, which you then assign as self.scene_map.

Calling Map.next_scene() doesn't make sense, as it's not a class method. Going from one spot on a map to another spot on the same map only makes sense if you have a map first.
Noted, thanks :)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  class returns NoneType Object istemihan 0 274 Aug-12-2019, 11:47 AM
Last Post: istemihan
  Object and type class Uchikago 2 259 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  Return a value when I equal an object from a class ihouses 4 381 Jul-10-2019, 02:44 AM
Last Post: SheeppOSU
  How to save a class object to a file? SheeppOSU 2 354 Jun-22-2019, 11:54 PM
Last Post: metulburr
  saving (in text or binary) an object under a defined class cai0824 3 493 May-12-2019, 08:55 AM
Last Post: snippsat
  Why Can't I call base class function for my derived class object? AmirAB 2 469 Feb-13-2019, 03:04 PM
Last Post: snippsat
  General query regarding conversion of dict to class object saisankalpj 0 405 Jan-16-2019, 01:58 PM
Last Post: saisankalpj
  Class Object hellp Dneal620 1 386 Dec-18-2018, 12:19 AM
Last Post: micseydel
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 809 Dec-03-2018, 11:16 PM
Last Post: Windspar
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 2,091 Jun-19-2018, 02:44 PM
Last Post: jdrp

Forum Jump:

Users browsing this thread: 1 Guest(s)