5 Replies - 278 Views - Last Post: 02 January 2016 - 01:32 PM Rate Topic: -----

#1 Kernel0010  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 05-January 15

Confused how next scene is called

Posted 01 January 2016 - 05:28 AM

I just learned some simple object-oriented programming. Such as Encapsulation, Data-abstraction, Polymorphism and Inheritance. Yet, my head hurts from trying to figure out how this code works.

What confuses me the most is line 37, where you send in an object of Map to an Engine object. Does the law of inheritance apply to this method ? Will the engine object inherit methods from Map ?



class Scene(object):
	print "Not configured yet."
	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()
		
		while True:
			print "\n--------"
			next_scene_name = current_scene.enter()
			current_scene = self.scene_map.next_scene(next_scene_name)

class Map(object):
	scenes = {
		'central_corridor': CentralCorridor(),
		'laser_weapon_armory': LaserWeaponArmory(),
		'the_bridge': TheBridge(),
		'escape_pod': EscapePod(),
		'death': Death()
	}
	
	def __init__(self, start_scene):
		self.start_scene = start_scene
		
	def next_scene(self, scene_name):
		return Map.scenes.get(scene_name)
		
	def opening_scene(self):
		return self.next_scene(self.start_scene)
		
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()


Is This A Good Question/Topic? 0
  • +

Replies To: Confused how next scene is called

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6510
  • View blog
  • Posts: 13,803
  • Joined: 16-October 07

Re: Confused how next scene is called

Posted 01 January 2016 - 06:39 AM

Right, for those playing along at home, this comes from here: http://learnpythonth.../book/ex43.html

I've noted elsewhere, for the other guy struggling with the questionable book, this code framework is horrid. The author's original version that didn't use any objects at all at least made sense: https://lpthw.readth...en/latest/ex41/

So, with what you have:
class Scene(object):
    # you skipped this in your post
    def enter(self):
        # print "Not configured yet."
        # if you have to call this, your flow sucks
        # exit(1)
        raise NotImplementedError("Not configured yet.")

class Engine(object):
    def __init__(self, scene_map):
        self.scene_map = scene_map

    def play(self):
        # load starting scene
        # this should be a Scene object
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n--------"
            # enter should return the name of another valid scene
            next_scene_name = current_scene.enter()
            # the scene_map sould be able to get another Scene object based on
            # name
            current_scene = self.scene_map.next_scene(next_scene_name)

class Map(object):
    # create object intances and associate them with names
    # this should belong to the object instance,
    # not the object as it does here
    scenes = {
            'central_corridor': CentralCorridor(),
            'laser_weapon_armory': LaserWeaponArmory(),
            'the_bridge': TheBridge(),
            'escape_pod': EscapePod(),
            'death': Death()
            }

    def __init__(self, start_scene):
        # store starting name
        # this is the only used one time
        # perhaps should be reconsidered
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        # calling a class method, bad idea
        return Map.scenes.get(scene_name)

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

# init with starting room...
# which belongs to the class you're calling?!?
a_map = Map('central_corridor')
# init Engine, which is theoretically generic, with map instance
a_game = Engine(a_map)
# call the play method on the object
a_game.play()



Good luck with that book. Also, look at other material on OOP and Python. Because, as an example, the author's code is fail.
Was This Post Helpful? 2
  • +
  • -

#3 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,379
  • Joined: 13-June 14

Re: Confused how next scene is called

Posted 01 January 2016 - 09:14 AM

I'm not sure why you'd think there's an inheritance relationship between Engine and Map. Inheritance means "is a" (e.g. a car is a vehicle, a square is a shape ...). The only inheritance in that code is that your classes inherit from object (see, e.g. line 26).
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,379
  • Joined: 13-June 14

Re: Confused how next scene is called

Posted 01 January 2016 - 09:58 AM

Oops, typing on a phone got me confused. I meant line 26 in baavgai's version of the code.
Was This Post Helpful? 0
  • +
  • -

#5 albert003  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 15-December 14

Re: Confused how next scene is called

Posted 02 January 2016 - 12:23 PM

I also had a lot of difficulties with that lesson and I've learned that you don't need to make it exactly like Zed Shaw made his. Each programmer will have their own style and way of doing things and to me as long as you have the same results and the program works thats all that matters.
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,379
  • Joined: 13-June 14

Re: Confused how next scene is called

Posted 02 January 2016 - 01:32 PM

While just getting the program to work is fine initially, just be aware that at some point you'll want to learn about designing your programs well. If you always just write programs that work and never learn about good design, you'll find it quite painful when you want to make changes to your software.. or even when you just want to go and see how it works some time long after you've finished it. I imagine everyone starts out that way (I certainly did!), but as others will tell you, writing software is a continual learning experience (well, it should be, anyway).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1