medusaOS commited on
Commit
2312915
·
verified ·
1 Parent(s): d2d134c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -41
app.py CHANGED
@@ -39,70 +39,66 @@ def game_logic(action):
39
  global current_room, inventory, game_started
40
  room = rooms.get(current_room)
41
 
42
- # --- Main Menu ---
43
  if not game_started:
44
  if action == "Start Game":
45
  game_started = True
46
  current_room = "dark_room"
47
  inventory.clear()
48
- return f"🎮 Game started!\n{rooms[current_room]['description']}", update_buttons()
49
  elif action == "Help":
50
- return help_text, ["Start Game", "Help", "Exit"]
51
  elif action == "Exit":
52
- return "Goodbye! Thanks for playing.", []
53
- return "Select an option to start.", ["Start Game", "Help", "Exit"]
54
 
55
- # --- Help Menu ---
56
  if action == "Help":
57
- return help_text, update_buttons()
58
 
59
- # --- Room Actions ---
60
  if action.startswith("Look"):
61
  if current_room == "dark_room" and not room["puzzles"]["candle_lit"]:
62
- return "It’s too dark to see much. You might need to light the candle first.", update_buttons()
63
  items = ", ".join(room["items"]) if room["items"] else "nothing"
64
- return f"{room['description']} You see: {items}.", update_buttons()
65
 
66
  if action.startswith("Pick up "):
67
  item = action.replace("Pick up ", "")
68
  if item in room["items"]:
69
  inventory.append(item)
70
  room["items"].remove(item)
71
- return f"You picked up the {item}.", update_buttons()
72
- return f"No {item} here to pick up.", update_buttons()
73
 
74
  if action.startswith("Use "):
75
  item = action.replace("Use ", "")
76
  if item in inventory:
77
  if item == "candle":
78
  room["puzzles"]["candle_lit"] = True
79
- return "You light the candle. Now you can see everything clearly!", update_buttons()
80
- return f"You used the {item}, but nothing happened.", update_buttons()
81
- return f"You don't have a {item}.", update_buttons()
82
 
83
  if action in room["exits"]:
84
  if room["door_locked"] and action == "door":
85
  if "key" in inventory:
86
  room["door_locked"] = False
87
  current_room = "hallway"
88
- return f"You unlock the door with the key and enter the hallway.", update_buttons()
89
- return "The door is locked. You need a key.", update_buttons()
90
  current_room = action
91
- return f"You move into {current_room}. {rooms[current_room]['description']}", update_buttons()
92
 
93
  if action == "Inventory":
94
- return f"You have: {', '.join(inventory) if inventory else 'nothing'}", update_buttons()
95
 
96
  if action == "Exit":
97
- return "Thanks for playing! Goodbye!", []
98
 
99
- return "I don't understand that action.", update_buttons()
100
 
101
- # --- Update Buttons Based on Room ---
102
- def update_buttons():
103
  if not game_started:
104
  return ["Start Game", "Help", "Exit"]
105
-
106
  room = rooms[current_room]
107
  buttons = ["Look", "Inventory", "Help", "Exit"]
108
  buttons += [f"Pick up {item}" for item in room["items"]]
@@ -111,35 +107,42 @@ def update_buttons():
111
 
112
  # --- Gradio UI ---
113
  with gr.Blocks() as demo:
114
- gr.Markdown("# 🏰 Escape Room Adventure (Clickable Buttons)")
115
 
116
  chatbot = gr.Chatbot(type="messages")
117
  state = gr.State()
118
- buttons_container = gr.Row()
119
 
120
- # --- Function to handle button clicks ---
 
 
 
 
 
 
 
 
 
 
 
121
  def respond(action, history):
122
  history = history or []
123
- bot_reply, buttons = game_logic(action)
124
- # Append messages in 'messages' format
125
  history.append({"role": "user", "content": action})
126
  history.append({"role": "assistant", "content": bot_reply})
127
- update_buttons_ui(buttons)
128
- return history, history
129
 
130
- # --- Update clickable buttons dynamically ---
131
- def update_buttons_ui(button_labels):
132
- buttons_container.children.clear()
133
- for label in button_labels:
134
- btn = gr.Button(label)
135
- btn.click(respond, inputs=[btn, state], outputs=[chatbot, state])
136
- return
137
 
138
- # Initial buttons
139
- update_buttons_ui(update_buttons())
 
 
 
140
 
141
  # Manual input
142
- msg = gr.Textbox(label="Type your action")
143
  msg.submit(respond, inputs=[msg, state], outputs=[chatbot, state])
144
 
145
  demo.launch()
 
39
  global current_room, inventory, game_started
40
  room = rooms.get(current_room)
41
 
 
42
  if not game_started:
43
  if action == "Start Game":
44
  game_started = True
45
  current_room = "dark_room"
46
  inventory.clear()
47
+ return f"🎮 Game started!\n{rooms[current_room]['description']}"
48
  elif action == "Help":
49
+ return help_text
50
  elif action == "Exit":
51
+ return "Goodbye! Thanks for playing."
52
+ return "Select an option to start."
53
 
 
54
  if action == "Help":
55
+ return help_text
56
 
 
57
  if action.startswith("Look"):
58
  if current_room == "dark_room" and not room["puzzles"]["candle_lit"]:
59
+ return "It’s too dark to see much. You might need to light the candle first."
60
  items = ", ".join(room["items"]) if room["items"] else "nothing"
61
+ return f"{room['description']} You see: {items}."
62
 
63
  if action.startswith("Pick up "):
64
  item = action.replace("Pick up ", "")
65
  if item in room["items"]:
66
  inventory.append(item)
67
  room["items"].remove(item)
68
+ return f"You picked up the {item}."
69
+ return f"No {item} here to pick up."
70
 
71
  if action.startswith("Use "):
72
  item = action.replace("Use ", "")
73
  if item in inventory:
74
  if item == "candle":
75
  room["puzzles"]["candle_lit"] = True
76
+ return "You light the candle. Now you can see everything clearly!"
77
+ return f"You used the {item}, but nothing happened."
78
+ return f"You don't have a {item}."
79
 
80
  if action in room["exits"]:
81
  if room["door_locked"] and action == "door":
82
  if "key" in inventory:
83
  room["door_locked"] = False
84
  current_room = "hallway"
85
+ return f"You unlock the door with the key and enter the hallway."
86
+ return "The door is locked. You need a key."
87
  current_room = action
88
+ return f"You move into {current_room}. {rooms[current_room]['description']}"
89
 
90
  if action == "Inventory":
91
+ return f"You have: {', '.join(inventory) if inventory else 'nothing'}"
92
 
93
  if action == "Exit":
94
+ return "Thanks for playing! Goodbye!"
95
 
96
+ return "I don't understand that action."
97
 
98
+ # --- Determine Which Buttons to Show ---
99
+ def get_visible_buttons():
100
  if not game_started:
101
  return ["Start Game", "Help", "Exit"]
 
102
  room = rooms[current_room]
103
  buttons = ["Look", "Inventory", "Help", "Exit"]
104
  buttons += [f"Pick up {item}" for item in room["items"]]
 
107
 
108
  # --- Gradio UI ---
109
  with gr.Blocks() as demo:
110
+ gr.Markdown("# 🏰 Escape Room Adventure (Dynamic Buttons)")
111
 
112
  chatbot = gr.Chatbot(type="messages")
113
  state = gr.State()
114
+ msg = gr.Textbox(label="Type your action")
115
 
116
+ # Pre-create all possible buttons
117
+ all_button_labels = [
118
+ "Start Game", "Help", "Exit", "Look", "Inventory",
119
+ "Pick up candle", "Pick up key", "Pick up painting",
120
+ "door", "trapdoor", "stairs", "painting", "Use candle"
121
+ ]
122
+ buttons_dict = {}
123
+ for label in all_button_labels:
124
+ btn = gr.Button(label)
125
+ buttons_dict[label] = btn
126
+
127
+ # Respond function
128
  def respond(action, history):
129
  history = history or []
130
+ bot_reply = game_logic(action)
 
131
  history.append({"role": "user", "content": action})
132
  history.append({"role": "assistant", "content": bot_reply})
 
 
133
 
134
+ # Update button visibility
135
+ visible = get_visible_buttons()
136
+ for lbl, btn in buttons_dict.items():
137
+ btn.update(visible=(lbl in visible))
 
 
 
138
 
139
+ return history, history
140
+
141
+ # Connect buttons to respond
142
+ for btn in buttons_dict.values():
143
+ btn.click(respond, inputs=[btn, state], outputs=[chatbot, state])
144
 
145
  # Manual input
 
146
  msg.submit(respond, inputs=[msg, state], outputs=[chatbot, state])
147
 
148
  demo.launch()