FYI def has a name it's 'function' or 'method' if part of a class.
This is best done by creating a class named Room. Then when you want a room, you instantiate a new class.
Dumb Example (but gets the point across (I hope):
This is best done by creating a class named Room. Then when you want a room, you instantiate a new class.
Dumb Example (but gets the point across (I hope):
class Room: def __init__(self): self.current = None def dead(self): self.current = 'Dead' def alive(self): self.current = 'Alive' class MyGame: def __init__(self): self.den = Room() self.den.alive() self.kitchen = Room() self.kitchen.alive() def display_rooms(self): print('\nden is {}'.format(self.den.current)) print('kitchen is {}'.format(self.kitchen.current)) def play(self): print('In the beginning') self.display_rooms() self.den.dead() print('A while later') self.display_rooms() self.den.alive() self.kitchen.dead() print('\nIn the end: ') self.display_rooms() if __name__ == '__main__': game = MyGame() game.play()results:
Output:In the beginning
den is Alive
kitchen is Alive
A while later
den is Dead
kitchen is Alive
In the end:
den is Alive
kitchen is Dead