mardi 3 novembre 2015

LPTHW ex45 Python if / else behaviour - exiting / loop and returning what I want

NB: Have searched here and read several posts related to if/else loop and similar things. Not sure I have the knowledge / context to translate the answers there.

I'm doing LPTHW and built a simple game for myself after Ex45. Heres an extract -

#Here I'm importing the moduels that I need for exit and randint.
from sys import exit
from random import randint

#Define a parent class called Scene
class Scene(object):

    def enter (self):
        print "This scene is not yet configured. Subclass it and"
        print "implement enter()"
        exit(1)

# Create class Engine, theres a while loop here too.
# Basically, it takes the opening scene as an "anchor" of sorts
# And everytime it checks to make sure that the next scene is not 'Finished',
# in which case its the last_scene.
# To understand this code, we need to look at the bottom of the file.
# There is a Map class with scenes dict defined and various methods for this.

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)

       # be sure to print out the last scene
       current_scene.enter()

Heres is the Object that I seem to be having issues with:

class Hallway(Scene):

    def enter(self):
        print "You are now in the long hallway"
        print "unfortunately, it is not very bright and its pretty hard to see"
        print "you hear the sound of footsteps running towards you - its the killer!"
        print "What do you do - use your Knife or Run?"
        decision = raw_input("Knife or Run? > ")

        if decision == "Knife":
            print "You pull your knife from your back pocket and swing it through the air"
            print "You hear a scream and then a thud on the floor"
            print "You find the light and turn it on, the killer is dead"
            return 'finished'

        elif decision == "Run":
            print "You run as fast as you can but you just can't get away"
            return 'death'

        else:
            print "You have two choices here, please try again!"
            return 'hallway'




class Map(object):

    scenes = {
        'respawn' : Respawn(),
        'kitchen' : Kitchen(),
        'hallway' : Hallway(),
        'death' : Death(),
        'finished' : Finished(),
        'won' : Won(),
    }

    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('respawn')
a_game = Engine(a_map)
a_game.play()

When I run the program, it never takes into account the if / else loop. See how it runs again here -

LPTHW$ python ex45_mygame.py
You've respawned, would you like to start from Hallway or Kitchen?
> Hallway
You are now in the long hallway
unfortunately, it is not very bright and its pretty hard to see
you hear the sound of footsteps running towards you - its the killer!
What do you do - use your Knife or Run?
Knife or Run? > Knife
You pull your knife from your back pocket and swing it through the air
You hear a scream and then a thud on the floor
You find the light and turn it on, the killer is dead
You are now in the long hallway
unfortunately, it is not very bright and its pretty hard to see
you hear the sound of footsteps running towards you - its the killer!
What do you do - use your Knife or Run?
Knife or Run? > Knife
You pull your knife from your back pocket and swing it through the air
You hear a scream and then a thud on the floor
You find the light and turn it on, the killer is dead
You won! Good job.





Would you like to play again? Y/N?

Can anyone help me understand what I am doing wrong here. I'm sure its something simple - just need a push. Sorry - I'm new to this!

Aucun commentaire:

Enregistrer un commentaire