|
| 1 | +# Import the tkinter library |
| 2 | +import tkinter as tk |
| 3 | + |
| 4 | +# Define the NPC class |
| 5 | +class NPC: |
| 6 | + def __init__(self, name, greeting, dialogue): |
| 7 | + self.name = name |
| 8 | + self.greeting = greeting |
| 9 | + self.dialogue = dialogue |
| 10 | + |
| 11 | + def interact(self): |
| 12 | + # Print the NPC's greeting |
| 13 | + print(self.greeting) |
| 14 | + |
| 15 | + # Create the GUI window |
| 16 | + window = tk.Tk() |
| 17 | + window.title(self.name) |
| 18 | + |
| 19 | + # Create a label for the greeting |
| 20 | + greeting_label = tk.Label(window, text=self.greeting) |
| 21 | + greeting_label.pack() |
| 22 | + |
| 23 | + # Create a button for each available option |
| 24 | + for option in self.dialogue: |
| 25 | + button = tk.Button(window, text=option, command=lambda: self.select_option(option)) |
| 26 | + button.pack() |
| 27 | + |
| 28 | + # Start the GUI loop |
| 29 | + window.mainloop() |
| 30 | + |
| 31 | + def select_option(self, option): |
| 32 | + # Print the selected option |
| 33 | + print(self.dialogue[option]) |
| 34 | + |
| 35 | +# Define the rooms and their descriptions |
| 36 | +rooms = { |
| 37 | + "entryway": "You are in the entryway of a house. There is a door to the north and a staircase leading upstairs to the west.", |
| 38 | + "kitchen": "You are in the kitchen. There is a table with chairs and a door to the east.", |
| 39 | + "living room": "You are in the living room. There is a couch and a fireplace. The door to the south leads back to the entryway.", |
| 40 | + "upstairs hallway": "You are in the upstairs hallway. There is a door to the east leading to the attic and a door to the west leading to a bedroom.", |
| 41 | + "attic": "You are in the attic. There is a mysterious locked door. A sign on the door reads: 'Solve the riddle to unlock the door: I am not alive, but I grow; I don't have lungs, but I need air; I don't have a mouth, but water kills me. What am I?'", |
| 42 | + "hidden room": "You are in a hidden room. There is a chest in the corner. It is locked, but it looks like it could be opened with the right key.", |
| 43 | + "bedroom": "You are in a bedroom. There is a bed and a dresser.", |
| 44 | + "basement": "You are in the basement. It is dark and musty down here." |
| 45 | +} |
| 46 | + |
| 47 | +# Define the available actions |
| 48 | +actions = ["move", "map", "q"] |
| 49 | + |
| 50 | +# Define the map of the game world |
| 51 | +map = { |
| 52 | + "entryway": ["north", "west"], |
| 53 | + "kitchen": ["east"], |
| 54 | + "living room": ["south"], |
| 55 | + "upstairs hallway": ["east", "west"], |
| 56 | + "attic": ["east"], |
| 57 | + "hidden room": ["west"], |
| 58 | + "bedroom": ["east"], |
| 59 | + "basement": [] |
| 60 | +} |
| 61 | + |
| 62 | +# Define the player's inventory |
| 63 | +inventory = [] |
| 64 | + |
| 65 | +# Set the starting location |
| 66 | +location = "entryway" |
| 67 | + |
| 68 | +# Print the initial room description |
| 69 | +print(rooms[location]) |
| 70 | + |
| 71 | +# Create the NPCs |
| 72 | +shopshopkeeper = NPC("Shopkeeper", "Welcome to my shop! Can I interest you in any items?", { |
| 73 | + "Yes, I'd like to browse your wares": "Here are my items for sale: a key for 10 gold, a map for 5 gold, and a lantern for 15 gold. Which would you like?", |
| 74 | + "No, I'm just looking": "Alright, let me know if you change your mind." |
| 75 | +}) |
| 76 | + |
| 77 | +riddlemaster = NPC("Riddlemaster", "Hello, adventurer. I see you're trying to unlock the door to the hidden room. Perhaps you'd like to try solving a riddle to unlock it?", { |
| 78 | + "Sure, I love riddles": "Very well. Here is the riddle: I am not alive, but I grow; I don't have lungs, but I need air; I don't have a mouth, but water kills me. What am I?", |
| 79 | + "No, I'd rather not": "Very well. Good luck on your journey." |
| 80 | +}) |
| 81 | + |
| 82 | +# Define the available actions |
| 83 | +actions = ["move", "map", "q", "interact"] |
| 84 | + |
| 85 | +while True: |
| 86 | + # Print the available actions |
| 87 | + print("Actions:", ", ".join(actions)) |
| 88 | + |
| 89 | + # Get the player's input |
| 90 | + action = input("What would you like to do? ") |
| 91 | + |
| 92 | + # Check if the player wants to move |
| 93 | + if action == "move": |
| 94 | + # Print the available directions |
| 95 | + print("Available directions:", ", ".join(map[location])) |
| 96 | + |
| 97 | + # Get the player's input |
| 98 | + direction = input("Which direction would you like to go? ") |
| 99 | + |
| 100 | + # Check if the direction is available |
| 101 | + if direction in map[location]: |
| 102 | + # Update the location |
| 103 | + location = direction |
| 104 | + |
| 105 | + # Print the new room description |
| 106 | + print(rooms[location]) |
| 107 | + else: |
| 108 | + print("You can't go that way.") |
| 109 | + |
| 110 | + # Check if the player wants to view the map |
| 111 | + elif action == "map": |
| 112 | + # Print the map |
| 113 | + for room, directions in map.items(): |
| 114 | + print(room, ":", ", ".join(directions)) |
| 115 | + |
| 116 | + # Check if the player wants to interact with an NPC |
| 117 | + elif action == "interact": |
| 118 | + # Print the available NPCs |
| 119 | + print("Available NPCs:", ", ".join([shopkeeper.name, riddlemaster.name])) |
| 120 | + |
| 121 | + # Get the player's input |
| 122 | + npc = input("Which NPC would you like to interact with? ") |
| 123 | + |
| 124 | + # Check if the NPC exists |
| 125 | + if npc == shopkeeper.name: |
| 126 | + shopkeeper.interact() |
| 127 | + elif npc == riddlemaster.name: |
| 128 | + riddlemaster.interact() |
| 129 | + else: |
| 130 | + print("There is no NPC with that name.") |
| 131 | + |
| 132 | + # Check if the player wants to quit the game |
| 133 | + elif action == "q": |
| 134 | + break |
| 135 | + |
| 136 | + # If the player's input is invalid |
| 137 | + else: |
| 138 | + print("Invalid action.") |
0 commit comments