Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class/object
#1
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 Engine.play"
		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'
			
		else:
			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"
a_game.play()
print "after a_game.play()"
Hi,

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
Output:
<__main__.Map object at 0x1028504d0>
as result, can explain what it means?

Thanks.
Reply
#2
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.
Reply
#3
Noted, thanks :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 444 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,801 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  AttributeError class object has no attribute list object scttfnch 5 3,342 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Python Class and Object Parameters JoeDainton123 5 2,828 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  Multiprocessing, class, run and a Queue Object SeanInColo 0 1,491 Jul-12-2020, 05:36 PM
Last Post: SeanInColo
  Hi, need help with class, object Houston222 1 1,804 Apr-04-2020, 01:55 PM
Last Post: buran
  class returns NoneType Object istemihan 0 2,215 Aug-12-2019, 11:47 AM
Last Post: istemihan
  Object and type class Uchikago 2 2,200 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  Return a value when I equal an object from a class ihouses 4 2,919 Jul-10-2019, 02:44 AM
Last Post: SheeppOSU
  How to save a class object to a file? SheeppOSU 2 3,698 Jun-22-2019, 11:54 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020