def play_again():
play = input("Do you want to play again? (yes/no): ").lower()
if play == "yes":
main()
elif play == "no":
print("Thanks for playing!")
exit()
else:
print("Invalid input. Please type 'yes' or 'no'.")
play_again()
Main game loop
def main():
print("Welcome to the Adventure Game!")
print("You are a brave explorer who has stumbled upon a mysterious cave.")
print("Your goal is to find the legendary treasure hidden deep within.")
print("Be careful, the cave is filled with traps and puzzles.")
print("Good luck!\n")
# Game loop
while True:
# Display current room
current_room = rooms[player["current_room"]]
current_room.display()
# Check if player has won
if current_room.is_exit():
print("Congratulations! You've found the legendary treasure!")
play_again()
# Get player's action
action = input("What do you want to do? ").lower()
# Process player's action
if action == "go north":
if current_room.has_exit("north"):
player["current_room"] = current_room.get_exit("north")
else:
print("There is no exit to the north.")
elif action == "go south":
if current_room.has_exit("south"):
player["current_room"] = current_room.get_exit("south")
else:
print("There is no exit to the south.")
elif action == "go east":
if current_room.has_exit("east"):
player["current_room"] = current_room.get_exit("east")
else:
print("There is no exit to the east.")
elif action == "go west":
if current_room.has_exit("west"):
player["current_room"] = current_room.get_exit("west")
else:
print("There is no exit to the west.")
elif action == "take key":
if current_room.has_item("key"):
current_room.take_item("key")
print("You've taken the key.")
else:
print("There is no key here.")
elif action == "use key":
if "key" in player["inventory"]:
if current_room.has_locked_exit():
current_room.use_key()
print("You've unlocked the exit.")
else:
print("There is nothing to unlock here.")
else:
print("You don't have a key.")
else:
print("Invalid action. Please try again.")
Start the game
if name == "main":
main()
This is a simple text-based adventure game in Python. The player navigates through a cave system, solving puzzles and avoiding traps to find a legendary treasure. The game includes basic inventory management and a simple combat system.
To run the game, save the code in a file called "adventure_game.py" and execute it using Python 3:
python adventure_game.py
You can then play the game by typing commands to interact with the game world. Enjoy your adventure!