Confused by Gothon exercise game Python












1














From learningpythonthehardway, I've reached the text adventure game and I had a few codes that I understand what they execute, but I don't understand how. Below is the whole code I made smaller into a single .py file:



from sys import exit 
from random import randint

class Scene(object):
def enter(self):
print "This isn't configured yet. Subclass and implement enter()"
exit(1)

class CentralCorridor(Scene):

def enter(self):
print "Something."
action = raw_input("> ")

if action == "shoot!":
print "Something something."

return 'death'


class Death(Scene):

dead = [
"You died. That sucks ...",
"You shouldn't have died here...that was dumb.",
"You L00se.",
"Even a cow's moo sounds smarter than you."
]

def enter(self):
print Death.dead[randint(0, len(self.dead)-1)]
exit(1)

class Engine(object):

def __init__(self, scene_map):
self.scene_map = scene_map #sets scene_map from the a_map variable

def play(self):
current_scene = self.scene_map.opening_scene()#sets aMap.opening_scene() and initiates CentralCorridor
last_scene = self.scene_map.next_scene('finished') #for the last win scene Finished()
while current_scene != last_scene:
next_scene_name = current_scene.enter() #if scene's not last, call enter() of current scene to next scene name
current_scene = self.scene_map.next_scene(next_scene_name)#Set current_scene to instance of scene_map with self, next_scene_name as parameters

current_scene.enter()

class Map(object): #initially central_corridor

scenes = {
'central_corridor':CentralCorridor(),
'death':Death(),
}

def __init__(self, start_scene):
self.start_scenes = start_scene #central_corridor


def next_scene(self, scene_name):
val = Map.scenes.get(scene_name) #sets CentralCorridor() to val
return val

def opening_scene(self):
return self.next_scene(self.start_scenes) #returns val from
#next_scene which is
#CentralCorridor()


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


In this code, I have a few things I don't get (bear with me.)




  1. How does " return 'death' " under the CentralCorridor class refer to the dictionary down in Map class's scenes, and return Death class? When I type shoot! it seems to go through the Engine class and execute the current_scene.enter() but I haven't called the Engine class's play function anywhere in the CentralCorridor class.


  2. In the Map class's opening_scene function, how exactly is
    return self.next_scene(self.start_scenes) working??? I know what it does, but the self.next_scene(self.start_scenes) bit is slightly confusing me.



Thank you for anyone explaining this!










share|improve this question





























    1














    From learningpythonthehardway, I've reached the text adventure game and I had a few codes that I understand what they execute, but I don't understand how. Below is the whole code I made smaller into a single .py file:



    from sys import exit 
    from random import randint

    class Scene(object):
    def enter(self):
    print "This isn't configured yet. Subclass and implement enter()"
    exit(1)

    class CentralCorridor(Scene):

    def enter(self):
    print "Something."
    action = raw_input("> ")

    if action == "shoot!":
    print "Something something."

    return 'death'


    class Death(Scene):

    dead = [
    "You died. That sucks ...",
    "You shouldn't have died here...that was dumb.",
    "You L00se.",
    "Even a cow's moo sounds smarter than you."
    ]

    def enter(self):
    print Death.dead[randint(0, len(self.dead)-1)]
    exit(1)

    class Engine(object):

    def __init__(self, scene_map):
    self.scene_map = scene_map #sets scene_map from the a_map variable

    def play(self):
    current_scene = self.scene_map.opening_scene()#sets aMap.opening_scene() and initiates CentralCorridor
    last_scene = self.scene_map.next_scene('finished') #for the last win scene Finished()
    while current_scene != last_scene:
    next_scene_name = current_scene.enter() #if scene's not last, call enter() of current scene to next scene name
    current_scene = self.scene_map.next_scene(next_scene_name)#Set current_scene to instance of scene_map with self, next_scene_name as parameters

    current_scene.enter()

    class Map(object): #initially central_corridor

    scenes = {
    'central_corridor':CentralCorridor(),
    'death':Death(),
    }

    def __init__(self, start_scene):
    self.start_scenes = start_scene #central_corridor


    def next_scene(self, scene_name):
    val = Map.scenes.get(scene_name) #sets CentralCorridor() to val
    return val

    def opening_scene(self):
    return self.next_scene(self.start_scenes) #returns val from
    #next_scene which is
    #CentralCorridor()


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


    In this code, I have a few things I don't get (bear with me.)




    1. How does " return 'death' " under the CentralCorridor class refer to the dictionary down in Map class's scenes, and return Death class? When I type shoot! it seems to go through the Engine class and execute the current_scene.enter() but I haven't called the Engine class's play function anywhere in the CentralCorridor class.


    2. In the Map class's opening_scene function, how exactly is
      return self.next_scene(self.start_scenes) working??? I know what it does, but the self.next_scene(self.start_scenes) bit is slightly confusing me.



    Thank you for anyone explaining this!










    share|improve this question



























      1












      1








      1







      From learningpythonthehardway, I've reached the text adventure game and I had a few codes that I understand what they execute, but I don't understand how. Below is the whole code I made smaller into a single .py file:



      from sys import exit 
      from random import randint

      class Scene(object):
      def enter(self):
      print "This isn't configured yet. Subclass and implement enter()"
      exit(1)

      class CentralCorridor(Scene):

      def enter(self):
      print "Something."
      action = raw_input("> ")

      if action == "shoot!":
      print "Something something."

      return 'death'


      class Death(Scene):

      dead = [
      "You died. That sucks ...",
      "You shouldn't have died here...that was dumb.",
      "You L00se.",
      "Even a cow's moo sounds smarter than you."
      ]

      def enter(self):
      print Death.dead[randint(0, len(self.dead)-1)]
      exit(1)

      class Engine(object):

      def __init__(self, scene_map):
      self.scene_map = scene_map #sets scene_map from the a_map variable

      def play(self):
      current_scene = self.scene_map.opening_scene()#sets aMap.opening_scene() and initiates CentralCorridor
      last_scene = self.scene_map.next_scene('finished') #for the last win scene Finished()
      while current_scene != last_scene:
      next_scene_name = current_scene.enter() #if scene's not last, call enter() of current scene to next scene name
      current_scene = self.scene_map.next_scene(next_scene_name)#Set current_scene to instance of scene_map with self, next_scene_name as parameters

      current_scene.enter()

      class Map(object): #initially central_corridor

      scenes = {
      'central_corridor':CentralCorridor(),
      'death':Death(),
      }

      def __init__(self, start_scene):
      self.start_scenes = start_scene #central_corridor


      def next_scene(self, scene_name):
      val = Map.scenes.get(scene_name) #sets CentralCorridor() to val
      return val

      def opening_scene(self):
      return self.next_scene(self.start_scenes) #returns val from
      #next_scene which is
      #CentralCorridor()


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


      In this code, I have a few things I don't get (bear with me.)




      1. How does " return 'death' " under the CentralCorridor class refer to the dictionary down in Map class's scenes, and return Death class? When I type shoot! it seems to go through the Engine class and execute the current_scene.enter() but I haven't called the Engine class's play function anywhere in the CentralCorridor class.


      2. In the Map class's opening_scene function, how exactly is
        return self.next_scene(self.start_scenes) working??? I know what it does, but the self.next_scene(self.start_scenes) bit is slightly confusing me.



      Thank you for anyone explaining this!










      share|improve this question















      From learningpythonthehardway, I've reached the text adventure game and I had a few codes that I understand what they execute, but I don't understand how. Below is the whole code I made smaller into a single .py file:



      from sys import exit 
      from random import randint

      class Scene(object):
      def enter(self):
      print "This isn't configured yet. Subclass and implement enter()"
      exit(1)

      class CentralCorridor(Scene):

      def enter(self):
      print "Something."
      action = raw_input("> ")

      if action == "shoot!":
      print "Something something."

      return 'death'


      class Death(Scene):

      dead = [
      "You died. That sucks ...",
      "You shouldn't have died here...that was dumb.",
      "You L00se.",
      "Even a cow's moo sounds smarter than you."
      ]

      def enter(self):
      print Death.dead[randint(0, len(self.dead)-1)]
      exit(1)

      class Engine(object):

      def __init__(self, scene_map):
      self.scene_map = scene_map #sets scene_map from the a_map variable

      def play(self):
      current_scene = self.scene_map.opening_scene()#sets aMap.opening_scene() and initiates CentralCorridor
      last_scene = self.scene_map.next_scene('finished') #for the last win scene Finished()
      while current_scene != last_scene:
      next_scene_name = current_scene.enter() #if scene's not last, call enter() of current scene to next scene name
      current_scene = self.scene_map.next_scene(next_scene_name)#Set current_scene to instance of scene_map with self, next_scene_name as parameters

      current_scene.enter()

      class Map(object): #initially central_corridor

      scenes = {
      'central_corridor':CentralCorridor(),
      'death':Death(),
      }

      def __init__(self, start_scene):
      self.start_scenes = start_scene #central_corridor


      def next_scene(self, scene_name):
      val = Map.scenes.get(scene_name) #sets CentralCorridor() to val
      return val

      def opening_scene(self):
      return self.next_scene(self.start_scenes) #returns val from
      #next_scene which is
      #CentralCorridor()


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


      In this code, I have a few things I don't get (bear with me.)




      1. How does " return 'death' " under the CentralCorridor class refer to the dictionary down in Map class's scenes, and return Death class? When I type shoot! it seems to go through the Engine class and execute the current_scene.enter() but I haven't called the Engine class's play function anywhere in the CentralCorridor class.


      2. In the Map class's opening_scene function, how exactly is
        return self.next_scene(self.start_scenes) working??? I know what it does, but the self.next_scene(self.start_scenes) bit is slightly confusing me.



      Thank you for anyone explaining this!







      python python-2.7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 18 '15 at 17:55

























      asked Mar 18 '15 at 17:50









      OrangeLime

      497




      497
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You call the play() function in the very last line of your code: a_game.play()



          def opening_scene(self):
          # This call the next_scene method with the parameter start_scenes.
          # which is initialize to 'central_corridor' when you create the object
          # with Map('central_corridor') a few lines below
          return self.next_scene(self.start_scenes)


          def next_scene(self, scene_name):
          # This get the value for key 'central_corridor' from
          # the scenes dictionary and assign it to variable val.
          val = Map.scenes.get(scene_name)
          return val





          share|improve this answer





















          • I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
            – OrangeLime
            Mar 18 '15 at 18:33










          • There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
            – Dikei
            Mar 18 '15 at 18:40










          • Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
            – OrangeLime
            Mar 18 '15 at 18:43



















          1














          I have also just reach this exercise few days ago in the book, spend some time looking for answer. This website is really nice tho. Let me try to explain as it strengthen my understanding. Please feel free to correct me if I'm wrong at any point.



          Answering your question, I might as well explaining the whole thing start with:



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


          a_map is-a Map that takes 'central corridor' as parameter.



          In class Map (read the # comment)



          def __init__(self, start_scene):
          self.start_scenes = start_scene #start_scene is set to 'central corridor'

          def next_scene(self, scene_name):
          val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
          return val #return and pass to next_scene as a method.
          def opening_scene(self):
          return self.next_scene(self.start_scenes) #return and pass to opening_scene


          as self.start_scene was set as parameter 'central corridor' thus, opening_scene will search 'central corridor' in scenes library located in class Map by method stated in next_scene, which is get() a function used to locate things in library. Continue on...



          a_game = Engine(a_map)


          a_game is-a Engine that takes a_map as parameter, while a_map takes 'central corridor' as parameter thus a_game takes 'central corridor' as parameters as well. Logic A=B and B=C thus A=C. and also a_game has access to both class Map and Engine as inheritance. You can read about it in the next chapter in the book learningpythonthehardway after this chapter.



          In class Engine (read the # comment)



          def __init__(self, scene_map):
          self.scene_map = scene_map #self.scene_map has set to 'central corridor'
          def play(self):
          current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explain in class Map, accessible by a_game
          last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
          #I didn't see any 'finish' scene in map, I think you should have added it in scenes.


          Note Here! you will get a return from the enter function.



              while current_scene != last_scene: #enters the while loop!! This is where the game starts.
          next_scene_name = current_scene.enter()
          current_scene = self.scene_map.next_scene(next_scene_name)

          current_scene.enter()


          Well here is the critical point to understand the whole game process.



          next_scene_name = current_scene.enter()


          Now you are inside the function CentralCorridor(), and you make your choices. Because your codes are not complete, so in this case we assume action == 'shoot!' which gets you 'death' as return and pass this to next_scene_name. Now next_scene_name has set to 'death'. You may have other scenes to be set if you make other choices here (if you code them).



          Then



          current_scene = self.scene_map.next_scene(next_scene_name)


          Set current_scene by searching the scene in class Map using next_scene function which was explained earlier. In this case, searching the return from CentralCorridor() which is 'death', will get you the function death() in library scenes located in class Map.



          The game continues as while loops current_scene != last_scene. That's why u need a 'finished' scene in scenes library.






          share|improve this answer































            0














            To understand the code, you need to understand what the last three lines of the code and what they actually do:



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


            The object a_map is first created which is-a class Map. Map is called with the argument central_corridor, which is a key to a dictionary that holds the value CentrolCorridor(). CentralCorridor() is a class which inherits from the class Scene.



            With this information, lets understand what the object a_map now actually holds. The object a_map can access the everything with the class Map. The object also directed to the class CentralCorridor and can access every method and variable there. Remember that cCentralCorridor inherits from the class Scene, meaning it can too access variables and methods in Scene.



            Now lets understand the object a_game. This object is an Engine class object. The Engine class, on the other hand, has the argument a_map. The a_map, as we have seen is an object that can access various methods and variables in a number of classes as discussed above. It is now possible to access the play method in Engine, ie a_game.play(). Keenly this can help you understand and respond to your first questions. Just take time to understand how everything merges out and connects to each other.
            On the second question, in fact a proper understanding of the above can help you understand how everything couples up in the Engine class. return self.next_scene(self.start_scenes)holds the initial argument central_corridor. After executing the next value that is picked by the code val = Map.scenes.get(scene_name) Therefore, the code return val takes the code to the new scene, which is the class Death.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29129219%2fconfused-by-gothon-exercise-game-python%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You call the play() function in the very last line of your code: a_game.play()



              def opening_scene(self):
              # This call the next_scene method with the parameter start_scenes.
              # which is initialize to 'central_corridor' when you create the object
              # with Map('central_corridor') a few lines below
              return self.next_scene(self.start_scenes)


              def next_scene(self, scene_name):
              # This get the value for key 'central_corridor' from
              # the scenes dictionary and assign it to variable val.
              val = Map.scenes.get(scene_name)
              return val





              share|improve this answer





















              • I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
                – OrangeLime
                Mar 18 '15 at 18:33










              • There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
                – Dikei
                Mar 18 '15 at 18:40










              • Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
                – OrangeLime
                Mar 18 '15 at 18:43
















              1














              You call the play() function in the very last line of your code: a_game.play()



              def opening_scene(self):
              # This call the next_scene method with the parameter start_scenes.
              # which is initialize to 'central_corridor' when you create the object
              # with Map('central_corridor') a few lines below
              return self.next_scene(self.start_scenes)


              def next_scene(self, scene_name):
              # This get the value for key 'central_corridor' from
              # the scenes dictionary and assign it to variable val.
              val = Map.scenes.get(scene_name)
              return val





              share|improve this answer





















              • I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
                – OrangeLime
                Mar 18 '15 at 18:33










              • There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
                – Dikei
                Mar 18 '15 at 18:40










              • Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
                – OrangeLime
                Mar 18 '15 at 18:43














              1












              1








              1






              You call the play() function in the very last line of your code: a_game.play()



              def opening_scene(self):
              # This call the next_scene method with the parameter start_scenes.
              # which is initialize to 'central_corridor' when you create the object
              # with Map('central_corridor') a few lines below
              return self.next_scene(self.start_scenes)


              def next_scene(self, scene_name):
              # This get the value for key 'central_corridor' from
              # the scenes dictionary and assign it to variable val.
              val = Map.scenes.get(scene_name)
              return val





              share|improve this answer












              You call the play() function in the very last line of your code: a_game.play()



              def opening_scene(self):
              # This call the next_scene method with the parameter start_scenes.
              # which is initialize to 'central_corridor' when you create the object
              # with Map('central_corridor') a few lines below
              return self.next_scene(self.start_scenes)


              def next_scene(self, scene_name):
              # This get the value for key 'central_corridor' from
              # the scenes dictionary and assign it to variable val.
              val = Map.scenes.get(scene_name)
              return val






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 18 '15 at 18:04









              Dikei

              9,50122130




              9,50122130












              • I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
                – OrangeLime
                Mar 18 '15 at 18:33










              • There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
                – Dikei
                Mar 18 '15 at 18:40










              • Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
                – OrangeLime
                Mar 18 '15 at 18:43


















              • I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
                – OrangeLime
                Mar 18 '15 at 18:33










              • There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
                – Dikei
                Mar 18 '15 at 18:40










              • Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
                – OrangeLime
                Mar 18 '15 at 18:43
















              I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
              – OrangeLime
              Mar 18 '15 at 18:33




              I understand next_scene gets the value of the key. But how is "return 'death'" going into next_scene of Map class?
              – OrangeLime
              Mar 18 '15 at 18:33












              There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
              – Dikei
              Mar 18 '15 at 18:40




              There's a loop inside play method, the get the return value of enter method and pass it into the map to get the next scene.
              – Dikei
              Mar 18 '15 at 18:40












              Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
              – OrangeLime
              Mar 18 '15 at 18:43




              Ooh!! Yes! Now I understand. I totally overlooked that while loop there. Thank you!
              – OrangeLime
              Mar 18 '15 at 18:43













              1














              I have also just reach this exercise few days ago in the book, spend some time looking for answer. This website is really nice tho. Let me try to explain as it strengthen my understanding. Please feel free to correct me if I'm wrong at any point.



              Answering your question, I might as well explaining the whole thing start with:



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


              a_map is-a Map that takes 'central corridor' as parameter.



              In class Map (read the # comment)



              def __init__(self, start_scene):
              self.start_scenes = start_scene #start_scene is set to 'central corridor'

              def next_scene(self, scene_name):
              val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
              return val #return and pass to next_scene as a method.
              def opening_scene(self):
              return self.next_scene(self.start_scenes) #return and pass to opening_scene


              as self.start_scene was set as parameter 'central corridor' thus, opening_scene will search 'central corridor' in scenes library located in class Map by method stated in next_scene, which is get() a function used to locate things in library. Continue on...



              a_game = Engine(a_map)


              a_game is-a Engine that takes a_map as parameter, while a_map takes 'central corridor' as parameter thus a_game takes 'central corridor' as parameters as well. Logic A=B and B=C thus A=C. and also a_game has access to both class Map and Engine as inheritance. You can read about it in the next chapter in the book learningpythonthehardway after this chapter.



              In class Engine (read the # comment)



              def __init__(self, scene_map):
              self.scene_map = scene_map #self.scene_map has set to 'central corridor'
              def play(self):
              current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explain in class Map, accessible by a_game
              last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
              #I didn't see any 'finish' scene in map, I think you should have added it in scenes.


              Note Here! you will get a return from the enter function.



                  while current_scene != last_scene: #enters the while loop!! This is where the game starts.
              next_scene_name = current_scene.enter()
              current_scene = self.scene_map.next_scene(next_scene_name)

              current_scene.enter()


              Well here is the critical point to understand the whole game process.



              next_scene_name = current_scene.enter()


              Now you are inside the function CentralCorridor(), and you make your choices. Because your codes are not complete, so in this case we assume action == 'shoot!' which gets you 'death' as return and pass this to next_scene_name. Now next_scene_name has set to 'death'. You may have other scenes to be set if you make other choices here (if you code them).



              Then



              current_scene = self.scene_map.next_scene(next_scene_name)


              Set current_scene by searching the scene in class Map using next_scene function which was explained earlier. In this case, searching the return from CentralCorridor() which is 'death', will get you the function death() in library scenes located in class Map.



              The game continues as while loops current_scene != last_scene. That's why u need a 'finished' scene in scenes library.






              share|improve this answer




























                1














                I have also just reach this exercise few days ago in the book, spend some time looking for answer. This website is really nice tho. Let me try to explain as it strengthen my understanding. Please feel free to correct me if I'm wrong at any point.



                Answering your question, I might as well explaining the whole thing start with:



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


                a_map is-a Map that takes 'central corridor' as parameter.



                In class Map (read the # comment)



                def __init__(self, start_scene):
                self.start_scenes = start_scene #start_scene is set to 'central corridor'

                def next_scene(self, scene_name):
                val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
                return val #return and pass to next_scene as a method.
                def opening_scene(self):
                return self.next_scene(self.start_scenes) #return and pass to opening_scene


                as self.start_scene was set as parameter 'central corridor' thus, opening_scene will search 'central corridor' in scenes library located in class Map by method stated in next_scene, which is get() a function used to locate things in library. Continue on...



                a_game = Engine(a_map)


                a_game is-a Engine that takes a_map as parameter, while a_map takes 'central corridor' as parameter thus a_game takes 'central corridor' as parameters as well. Logic A=B and B=C thus A=C. and also a_game has access to both class Map and Engine as inheritance. You can read about it in the next chapter in the book learningpythonthehardway after this chapter.



                In class Engine (read the # comment)



                def __init__(self, scene_map):
                self.scene_map = scene_map #self.scene_map has set to 'central corridor'
                def play(self):
                current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explain in class Map, accessible by a_game
                last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
                #I didn't see any 'finish' scene in map, I think you should have added it in scenes.


                Note Here! you will get a return from the enter function.



                    while current_scene != last_scene: #enters the while loop!! This is where the game starts.
                next_scene_name = current_scene.enter()
                current_scene = self.scene_map.next_scene(next_scene_name)

                current_scene.enter()


                Well here is the critical point to understand the whole game process.



                next_scene_name = current_scene.enter()


                Now you are inside the function CentralCorridor(), and you make your choices. Because your codes are not complete, so in this case we assume action == 'shoot!' which gets you 'death' as return and pass this to next_scene_name. Now next_scene_name has set to 'death'. You may have other scenes to be set if you make other choices here (if you code them).



                Then



                current_scene = self.scene_map.next_scene(next_scene_name)


                Set current_scene by searching the scene in class Map using next_scene function which was explained earlier. In this case, searching the return from CentralCorridor() which is 'death', will get you the function death() in library scenes located in class Map.



                The game continues as while loops current_scene != last_scene. That's why u need a 'finished' scene in scenes library.






                share|improve this answer


























                  1












                  1








                  1






                  I have also just reach this exercise few days ago in the book, spend some time looking for answer. This website is really nice tho. Let me try to explain as it strengthen my understanding. Please feel free to correct me if I'm wrong at any point.



                  Answering your question, I might as well explaining the whole thing start with:



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


                  a_map is-a Map that takes 'central corridor' as parameter.



                  In class Map (read the # comment)



                  def __init__(self, start_scene):
                  self.start_scenes = start_scene #start_scene is set to 'central corridor'

                  def next_scene(self, scene_name):
                  val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
                  return val #return and pass to next_scene as a method.
                  def opening_scene(self):
                  return self.next_scene(self.start_scenes) #return and pass to opening_scene


                  as self.start_scene was set as parameter 'central corridor' thus, opening_scene will search 'central corridor' in scenes library located in class Map by method stated in next_scene, which is get() a function used to locate things in library. Continue on...



                  a_game = Engine(a_map)


                  a_game is-a Engine that takes a_map as parameter, while a_map takes 'central corridor' as parameter thus a_game takes 'central corridor' as parameters as well. Logic A=B and B=C thus A=C. and also a_game has access to both class Map and Engine as inheritance. You can read about it in the next chapter in the book learningpythonthehardway after this chapter.



                  In class Engine (read the # comment)



                  def __init__(self, scene_map):
                  self.scene_map = scene_map #self.scene_map has set to 'central corridor'
                  def play(self):
                  current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explain in class Map, accessible by a_game
                  last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
                  #I didn't see any 'finish' scene in map, I think you should have added it in scenes.


                  Note Here! you will get a return from the enter function.



                      while current_scene != last_scene: #enters the while loop!! This is where the game starts.
                  next_scene_name = current_scene.enter()
                  current_scene = self.scene_map.next_scene(next_scene_name)

                  current_scene.enter()


                  Well here is the critical point to understand the whole game process.



                  next_scene_name = current_scene.enter()


                  Now you are inside the function CentralCorridor(), and you make your choices. Because your codes are not complete, so in this case we assume action == 'shoot!' which gets you 'death' as return and pass this to next_scene_name. Now next_scene_name has set to 'death'. You may have other scenes to be set if you make other choices here (if you code them).



                  Then



                  current_scene = self.scene_map.next_scene(next_scene_name)


                  Set current_scene by searching the scene in class Map using next_scene function which was explained earlier. In this case, searching the return from CentralCorridor() which is 'death', will get you the function death() in library scenes located in class Map.



                  The game continues as while loops current_scene != last_scene. That's why u need a 'finished' scene in scenes library.






                  share|improve this answer














                  I have also just reach this exercise few days ago in the book, spend some time looking for answer. This website is really nice tho. Let me try to explain as it strengthen my understanding. Please feel free to correct me if I'm wrong at any point.



                  Answering your question, I might as well explaining the whole thing start with:



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


                  a_map is-a Map that takes 'central corridor' as parameter.



                  In class Map (read the # comment)



                  def __init__(self, start_scene):
                  self.start_scenes = start_scene #start_scene is set to 'central corridor'

                  def next_scene(self, scene_name):
                  val = Map.scenes.get(scene_name) #get() narrows the search in the library scenes.
                  return val #return and pass to next_scene as a method.
                  def opening_scene(self):
                  return self.next_scene(self.start_scenes) #return and pass to opening_scene


                  as self.start_scene was set as parameter 'central corridor' thus, opening_scene will search 'central corridor' in scenes library located in class Map by method stated in next_scene, which is get() a function used to locate things in library. Continue on...



                  a_game = Engine(a_map)


                  a_game is-a Engine that takes a_map as parameter, while a_map takes 'central corridor' as parameter thus a_game takes 'central corridor' as parameters as well. Logic A=B and B=C thus A=C. and also a_game has access to both class Map and Engine as inheritance. You can read about it in the next chapter in the book learningpythonthehardway after this chapter.



                  In class Engine (read the # comment)



                  def __init__(self, scene_map):
                  self.scene_map = scene_map #self.scene_map has set to 'central corridor'
                  def play(self):
                  current_scene = self.scene_map.opening_scene() #set current_scene by calling the function opening_scene which was explain in class Map, accessible by a_game
                  last_scene = self.scene_map.next_scene('finished') #set last_scene by searching scene in class Map.
                  #I didn't see any 'finish' scene in map, I think you should have added it in scenes.


                  Note Here! you will get a return from the enter function.



                      while current_scene != last_scene: #enters the while loop!! This is where the game starts.
                  next_scene_name = current_scene.enter()
                  current_scene = self.scene_map.next_scene(next_scene_name)

                  current_scene.enter()


                  Well here is the critical point to understand the whole game process.



                  next_scene_name = current_scene.enter()


                  Now you are inside the function CentralCorridor(), and you make your choices. Because your codes are not complete, so in this case we assume action == 'shoot!' which gets you 'death' as return and pass this to next_scene_name. Now next_scene_name has set to 'death'. You may have other scenes to be set if you make other choices here (if you code them).



                  Then



                  current_scene = self.scene_map.next_scene(next_scene_name)


                  Set current_scene by searching the scene in class Map using next_scene function which was explained earlier. In this case, searching the return from CentralCorridor() which is 'death', will get you the function death() in library scenes located in class Map.



                  The game continues as while loops current_scene != last_scene. That's why u need a 'finished' scene in scenes library.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 4:41

























                  answered Nov 13 '18 at 3:19









                  Will MeetYou

                  237




                  237























                      0














                      To understand the code, you need to understand what the last three lines of the code and what they actually do:



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


                      The object a_map is first created which is-a class Map. Map is called with the argument central_corridor, which is a key to a dictionary that holds the value CentrolCorridor(). CentralCorridor() is a class which inherits from the class Scene.



                      With this information, lets understand what the object a_map now actually holds. The object a_map can access the everything with the class Map. The object also directed to the class CentralCorridor and can access every method and variable there. Remember that cCentralCorridor inherits from the class Scene, meaning it can too access variables and methods in Scene.



                      Now lets understand the object a_game. This object is an Engine class object. The Engine class, on the other hand, has the argument a_map. The a_map, as we have seen is an object that can access various methods and variables in a number of classes as discussed above. It is now possible to access the play method in Engine, ie a_game.play(). Keenly this can help you understand and respond to your first questions. Just take time to understand how everything merges out and connects to each other.
                      On the second question, in fact a proper understanding of the above can help you understand how everything couples up in the Engine class. return self.next_scene(self.start_scenes)holds the initial argument central_corridor. After executing the next value that is picked by the code val = Map.scenes.get(scene_name) Therefore, the code return val takes the code to the new scene, which is the class Death.






                      share|improve this answer


























                        0














                        To understand the code, you need to understand what the last three lines of the code and what they actually do:



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


                        The object a_map is first created which is-a class Map. Map is called with the argument central_corridor, which is a key to a dictionary that holds the value CentrolCorridor(). CentralCorridor() is a class which inherits from the class Scene.



                        With this information, lets understand what the object a_map now actually holds. The object a_map can access the everything with the class Map. The object also directed to the class CentralCorridor and can access every method and variable there. Remember that cCentralCorridor inherits from the class Scene, meaning it can too access variables and methods in Scene.



                        Now lets understand the object a_game. This object is an Engine class object. The Engine class, on the other hand, has the argument a_map. The a_map, as we have seen is an object that can access various methods and variables in a number of classes as discussed above. It is now possible to access the play method in Engine, ie a_game.play(). Keenly this can help you understand and respond to your first questions. Just take time to understand how everything merges out and connects to each other.
                        On the second question, in fact a proper understanding of the above can help you understand how everything couples up in the Engine class. return self.next_scene(self.start_scenes)holds the initial argument central_corridor. After executing the next value that is picked by the code val = Map.scenes.get(scene_name) Therefore, the code return val takes the code to the new scene, which is the class Death.






                        share|improve this answer
























                          0












                          0








                          0






                          To understand the code, you need to understand what the last three lines of the code and what they actually do:



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


                          The object a_map is first created which is-a class Map. Map is called with the argument central_corridor, which is a key to a dictionary that holds the value CentrolCorridor(). CentralCorridor() is a class which inherits from the class Scene.



                          With this information, lets understand what the object a_map now actually holds. The object a_map can access the everything with the class Map. The object also directed to the class CentralCorridor and can access every method and variable there. Remember that cCentralCorridor inherits from the class Scene, meaning it can too access variables and methods in Scene.



                          Now lets understand the object a_game. This object is an Engine class object. The Engine class, on the other hand, has the argument a_map. The a_map, as we have seen is an object that can access various methods and variables in a number of classes as discussed above. It is now possible to access the play method in Engine, ie a_game.play(). Keenly this can help you understand and respond to your first questions. Just take time to understand how everything merges out and connects to each other.
                          On the second question, in fact a proper understanding of the above can help you understand how everything couples up in the Engine class. return self.next_scene(self.start_scenes)holds the initial argument central_corridor. After executing the next value that is picked by the code val = Map.scenes.get(scene_name) Therefore, the code return val takes the code to the new scene, which is the class Death.






                          share|improve this answer












                          To understand the code, you need to understand what the last three lines of the code and what they actually do:



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


                          The object a_map is first created which is-a class Map. Map is called with the argument central_corridor, which is a key to a dictionary that holds the value CentrolCorridor(). CentralCorridor() is a class which inherits from the class Scene.



                          With this information, lets understand what the object a_map now actually holds. The object a_map can access the everything with the class Map. The object also directed to the class CentralCorridor and can access every method and variable there. Remember that cCentralCorridor inherits from the class Scene, meaning it can too access variables and methods in Scene.



                          Now lets understand the object a_game. This object is an Engine class object. The Engine class, on the other hand, has the argument a_map. The a_map, as we have seen is an object that can access various methods and variables in a number of classes as discussed above. It is now possible to access the play method in Engine, ie a_game.play(). Keenly this can help you understand and respond to your first questions. Just take time to understand how everything merges out and connects to each other.
                          On the second question, in fact a proper understanding of the above can help you understand how everything couples up in the Engine class. return self.next_scene(self.start_scenes)holds the initial argument central_corridor. After executing the next value that is picked by the code val = Map.scenes.get(scene_name) Therefore, the code return val takes the code to the new scene, which is the class Death.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 2 '17 at 19:21









                          chibole

                          451413




                          451413






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29129219%2fconfused-by-gothon-exercise-game-python%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python