How can I get a different message for individual rooms in my game?

Hello again,
I’m writing because I have a class called Death in my game that is giving me the same message everytime I die in the game. I want individualized messages to appear depending on which room you are in. Can someone please help me with this? Thank you in advance! I appologize in advance for the length of code, but I thought it would give an idea of what I’m trying to do.
Here’s the code:

from random import randint
from textwrap import dedent

class Scene(object):
    
    def enter(self):
        print("This scene is not yet configured.")
        print("Subclass it and implement enter().")
        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()
        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)

        current_scene.enter()

class Death(Engine):
    
    def enter(self):
        if  'outside_house' or 'dark_woods' or "outside_house_again":
            print("Day becomes night and you're devoured by wolves.")
            print("You're dead!  Better luck next time!")
            exit(1)
        elif 'foyer' or 'living_room':
            print("The dog chews your leg off and you bleed to death.")
            print("You're dead!  Better luck next time!") 
            exit(1)
        elif 'cellar':
            print("The dragon burns you to a crisp.")
            print("You're dead!  Better luck next time!")       
            exit(1)
        else:
            print("You are turned into a rodent and eaten by an owl.")
            print("You're dead!  Better luck next time!")
            exit(1)

class Start(Scene):
      
    def enter(self):
        print(dedent("""
                    Welcome to Arcamadius, where magic and creatures exist.
                    You come to a house in the dark woods.
                    There is a window on the east wall and a door on the
                    South wall.  The house is surrounded by woods. What
                    would you like to do?
                     """))
        action = input("> ")

        if action == 'open window':
            print(dedent("""
                        You open the window.  It makes a squeaking noise.
                        What do you wnat to do now?
                        """ ))
            action = input("> ")

            if action == 'climb into window':
                print("You enter the window into the kitchen.")
                return 'kitchen'
            else:
                print("You just stand there!")
                return 'death'
            
        elif action == 'open door':
            print("You enter the door into the Foyer")
            return 'foyer'
        else:
            return 'death'

class OutsideHouseWithoutBone(Scene):

    def enter(self):
        have_bone = False
        print(dedent("""
                    You are at a house in the dark woods.
                    There is a window on the east wall and a door on the
                    South wall.  The house is surrounded by woods. What
                    would you like to do?
                     """)) 
        action = input("> ")

        if action == 'open window':
            print(dedent("""
                        You open the window.  It makes a squeaking noise.
                        What do you wnat to do now?
                        """ ))
            action = input("> ")

            if action == 'climb into window':
                print("You enter the window into the kitchen.")
                return 'kitchen'
            else:
                print("You just stand there!")
                return 'death'
        elif action == 'open door':
            print("You enter the door into the Foyer")
            return 'foyer'
        else:
            return 'death'
        
class OutsideHouseWithBone(Scene):
    
    def enter(self):
        have_bone = True
        print(dedent("""
                    You are at a house in the dark woods.
                    There is an open window on the east wall and a door on the
                    South wall.  The house is surrounded by woods. You are 
                    carrying a bone.  What would you like to do?
                    """)) 
        action = input("> ")

        if action == 'climb into window':
                print("You enter the window into the kitchen.")
                return 'kitchen_with_bone'
        elif action == 'open door':
            print("You enter the door into the Foyer")
            return 'foyer_with_bone'
        else:
            return 'death'

        
class Kitchen (Scene):

    def enter(self):
        have_bone = False
        print(dedent("""
                     There is a doorway ahead.  There is also a table
                     with a bone on it.  What do you wnat to do?
                     """))
        action = input("> ")

        if action == "go through doorway" and have_bone == True:
            return 'living_room'
        elif action == "grab bone" and have_bone == False:
            print("You grab the bone")
            return 'kitchen_with_bone'
        elif action == "climb out window":
            return 'outside_house_without_bone'
        else:
            print("I'm not sure what you are saying.")
            self.enter(self) 
            
class KitchenWithBone(Scene):

    def enter(self):
        have_bone = True
        print(dedent("""
                     There is a doorway ahead.  There is also an empty
                     table.  What do you wnat to do?
                     """))
        action = input("> ")

        if action == "go through doorway" and have_bone == True:
            return 'living_room'
        elif action == "climb out window":
            return 'outside_house_with_bone'
        else:
            print("I'm not sure what you are saying.")
            self.enter(self)    

class Foyer(Scene):
    
    def enter(self):
        have_bone = False
        print(dedent("""
                     A huge dog is staring and growling at you
                     What do you do?
                     """))
        action = input("> ")

        if action == "flee":
            print(dedent("""
                         You managed to get away from the dog chasing you and the
                         dog goes back into the house.
                         """))
            return 'outside_house_without_bone'
        elif action == "give dog the bone" and have_bone == False:
            print("you have no bone.")
            return 'death'
        else:
            print("I have no idea what that means.")
            return 'foyer'

class FoyerWithBone(Scene):

    def enter(self):
        have_bone = True
        print(dedent("""
                     A huge dog is staring and growling at you and you are
                     carrying a bone.  What do you do?
                     """))
        action = input("> ")

        if action == "flee":
            print(dedent("""
                         You managed to get away from the dog chasing you and the
                         dog goes back into the house.
                         """))
            return 'outside_house_with_bone'
        elif action == "give dog the bone" and have_bone == True:
            print(dedent("""
                        You give the dog the bone.
                        The dog wants to play.  You play with the dog for awhile.
                        Now back to business. The entrance to the living room
                        is ahead.
                        """))
            return 'living_room'
        else:
            print("I have no idea what that means.")
            return 'foyer_with_bone'

class LivingRoom(Scene):

    def enter(self):
        pass

class DarkWoods(Scene):
    
    def enter(self):
        pass

class Cellar(Scene):

    def enter(self):
        pass

class WitchHouse(Scene):

    def enter(self):
        pass

class Finished(Scene):

    def enter(self):
        pass

class Map(object):
    scenes = {
        'start': Start(),
        'outside_house_without_bone': OutsideHouseWithoutBone(),
        'outside_house_with_bone': OutsideHouseWithBone(),
        'kitchen': Kitchen(),
        'kitchen_with_bone': KitchenWithBone(),
        'foyer': Foyer(),
        "foyer_with_bone": FoyerWithBone(),
        'living_room': LivingRoom(),
        'dark_woods': DarkWoods(),
        'cellar': Cellar(),
        'witch_house': WitchHouse(),
        'death': Death(),
        'finished': Finished(),
    }

    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):
        have_bone = False
        return self.next_scene(self.start_scene)

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

Hello,

The reason that the same message is printed for all “deaths” is because all the conditional statements are always true in your enter method. There is no test variable to test the string values against for a unique outcome. This is what is missing in your script (Death class and enter method).

As an example, I have gone ahead and modified your class method whereby I have added a test variable to test against. Since the method is static, there is no reason to include the self object. Because the test strings that you have in the conditional statements refer to places, I have named the test variable location but you can name it to anything that you prefer. Note that the variable self refers to the object itself (when creating instances of classes) and NOT the variable that you’re passing in as a test variable. Here is a tutorial on the subject:

The way that if location in (tuple of variables here) works is that if the variable location matches any of the values in a list or tuple, then that conditional statement is true and the corresponding print statement will be printed.

You can try modifying it to something like this:

    @staticmethod
    def enter(location):

        if location in ('outside_house', 'dark_woods', "outside_house_again"):
            print("Day becomes night and you're devoured by wolves.")

        elif location in ('foyer', 'living_room'):
            print("The dog chews your leg off and you bleed to death.")

        elif location == 'cellar':
            print("The dragon burns you to a crisp.")

        else:
            print("You are turned into a rodent and eaten by an owl.")

        print("You're dead!  Better luck next time!")
        exit(1)

Note that all four conditional statements have the following two lines in common:

        print("You're dead!  Better luck next time!")
        exit(1)

You can place them at the end since they would be executed for all conditions.

Be sure to pass in the variable to the enter method to be used to test the conditional statements.

1 Like

By the way, since your project is class based, you can create a separate class that is used just for the sake of passing the location variable between all the other classes. In order to do this, the class will have to be inherited by the other classes. Anytime that there is a change to the value of this variable, it will be transparent to the Death class method enter.

# This class used to keep track of 'location' variable
class Location:
    location = ''  # Initial value

# The following three classes simulate your script classes
class First(Location):

    @staticmethod
    def first_def():
        Location.location = 'outside_house'

class Second(Location):

    @staticmethod
    def second_def():
        Location.location = 'foyer'

class Third(Location):

    @staticmethod
    def third_def():
        Location.location = 'cellar'

First.first_def()
print(f'The value of location is: {Location.location}')
Second.second_def()
print(f'The value of location is: {Location.location}')
Third.third_def()
print(f'The value of location is: {Location.location}')

You can then modify your enter method like this:

    @staticmethod
    def enter():

        if Location.location in ('outside_house', 'dark_woods', "outside_house_again"):
            print("Day becomes night and you're devoured by wolves.")

        elif Location.location in ('foyer', 'living_room'):
            print("The dog chews your leg off and you bleed to death.")

        elif Location.location == 'cellar':
            print("The dragon burns you to a crisp.")

        else:
            print("You are turned into a rodent and eaten by an owl.")

        print("You're dead!  Better luck next time!")
        exit(1)

This implies that the Death class has inherited the Location class.

Update:
Actually, since the Location class is global, the other classes aren’t required to inherit it. It is visible within all classes.

Thank you for your response it was greatly appreciated!

I tried the second solution and the output kept going the else block in the Death class. Here’s a copy of what I did:

class Location:

    location = ' '

class First(Location):
    
    def first_def():
        Location.location = 'start'

class Second(Location):

    def second_def():
        Location.location = 'dark_woods'

class Third(Location):

    def third_def():
        Location.location = 'outside_house_without_bone'

class Fourth(Location):

    def fourth_def():
        Location.location = 'outside_house_with_bone'

class Fifth(Location):

    def fifth_def():
        Location.location = 'foyer'

class Sixth(Location):

    def sixth_def():
        Location.location = 'living_room'

class Seventh(Location):

    def seventh_def():
        Location.location = 'cellar'

class Death(Scene):
    
    def enter(self):
        
        if  Location.location in ('start', 'dark_woods', 'outside_house_without_bone', 'outside_house_with_bone'):
            print("Day becomes night and you're devoured by wolves.")
            
        elif Location.location in ('foyer', 'living_room'):
            print("The dog chews your leg off and you bleed to death.")
            
        elif Location.location == 'cellar':
            print("The dragon burns you to a crisp.")
            
        else:
            print("You are turned into a rodent and eaten by an owl.")
            
        print("You're dead!  Better luck next time!") 
        exit(1)        

Nevermind! I figurred it out. Thank you so much!

1 Like