Create the Game
Create a file named adventure_game.py
and add the following code:
pythonCopy codeclass AdventureGame:
def __init__(self):
self.is_running = True
self.current_room = 'start'
self.inventory = []
def start(self):
print("Welcome to the Adventure Game!")
print("Type 'help' for a list of commands.")
while self.is_running:
self.command_handler()
def command_handler(self):
command = input(f"\nYou are in {self.current_room}. What do you want to do? ").lower()
if command == 'help':
self.show_help()
elif command == 'look':
self.look_around()
elif command.startswith('go '):
self.move(command[3:])
elif command.startswith('take '):
self.take_item(command[5:])
elif command == 'inventory':
self.show_inventory()
elif command == 'quit':
self.is_running = False
print("Thank you for playing!")
else:
print("Unknown command. Type 'help' for a list of commands.")
def show_help(self):
print("\nAvailable commands:")
print(" - look: Look around your current room.")
print(" - go [direction]: Move to a different room (north, south, east, west).")
print(" - take [item]: Take an item from the room.")
print(" - inventory: Show your inventory.")
print(" - quit: Exit the game.")
def look_around(self):
if self.current_room == 'start':
print("You are in a dimly lit room. There is a door to the north.")
print("There is a shiny key on the table.")
elif self.current_room == 'hallway':
print("You are in a long hallway. There are doors to the east and west.")
elif self.current_room == 'treasure_room':
print("You are in a room filled with gold and jewels! There is a door to the south.")
def move(self, direction):
if self.current_room == 'start' and direction == 'north':
self.current_room = 'hallway'
print("You move north into the hallway.")
elif self.current_room == 'hallway' and direction == 'east':
self.current_room = 'treasure_room'
print("You move east into the treasure room.")
elif self.current_room == 'hallway' and direction == 'west':
self.current_room = 'start'
print("You move west back to the start room.")
else:
print("You can't go that way.")
def take_item(self, item):
if self.current_room == 'start' and item == 'key':
self.inventory.append('key')
print("You take the shiny key.")
else:
print("There's nothing to take here.")
def show_inventory(self):
if not self.inventory:
print("Your inventory is empty.")
else:
print("You have: " + ', '.join(self.inventory))
if __name__ == "__main__":
game = AdventureGame()
game.start()
Step 2: Running the Game
- Open your terminal (or command prompt).
- Navigate to the directory where you saved
adventure_game.py
. - Run the script using the command:bashCopy code
python adventure_game.py
How the Game Works
- Rooms: The game features three rooms:
start
,hallway
, andtreasure_room
. You can move between them and interact with items. - Commands: Players can use commands such as
look
,go
,take
,inventory
, andquit
. - Inventory System: Players can pick up items (like a key) and view their inventory.
Example Commands
- Look around: Type
look
to see what’s in your current room. - Move: Type
go north
to move to the hallway. - Take an item: Type
take key
to pick up the key in the start room. - Check inventory: Type
inventory
to see what items you have.
Leave a Reply