Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
def __init__(self):
|
| 6 |
-
self.location = "start"
|
| 7 |
-
self.inventory = []
|
| 8 |
-
self.game_over = False
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
command = command.lower().strip()
|
| 15 |
|
| 16 |
-
if
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return "You're at the entrance of a mysterious cave. The entrance looks dark but inviting."
|
| 28 |
else:
|
| 29 |
-
return "
|
| 30 |
-
|
| 31 |
-
elif
|
| 32 |
-
if
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if "crystal" in state.inventory:
|
| 39 |
-
state.location = "treasure"
|
| 40 |
-
return "The crystal lights your way through the passage. You find a treasure chest!"
|
| 41 |
-
return "It's too dark to go through the passage without a light source."
|
| 42 |
else:
|
| 43 |
-
return "
|
| 44 |
-
|
| 45 |
-
elif
|
| 46 |
-
if
|
| 47 |
-
|
| 48 |
-
return "
|
|
|
|
|
|
|
|
|
|
| 49 |
else:
|
| 50 |
-
return "
|
| 51 |
|
| 52 |
-
def game_interface(command, history):
|
| 53 |
-
if history is None:
|
| 54 |
-
state = GameState()
|
| 55 |
-
response = "Welcome! You find yourself at the entrance of a dark cave. What do you do?"
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return response, state
|
| 61 |
|
| 62 |
-
#
|
| 63 |
iface = gr.Interface(
|
| 64 |
-
fn=
|
| 65 |
-
inputs=
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
outputs=gr.Textbox(label="Game Output"),
|
| 70 |
-
title="Simple Text Adventure",
|
| 71 |
-
description="A simple cave exploration game. Try commands like 'look', 'enter', 'take crystal', etc.",
|
| 72 |
-
examples=[["look"], ["enter"], ["take crystal"], ["passage"], ["open chest"]]
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Initialize game state
|
| 4 |
+
game_state = {"step": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Game logic
|
| 7 |
+
def play_game(user_input):
|
| 8 |
+
step = game_state["step"]
|
| 9 |
+
user_input = user_input.strip().lower()
|
|
|
|
| 10 |
|
| 11 |
+
if step == 0:
|
| 12 |
+
game_state["step"] = 1
|
| 13 |
+
return "You are in a dark forest. Do you go 'left' or 'right'?"
|
| 14 |
+
|
| 15 |
+
elif step == 1:
|
| 16 |
+
if user_input == "left":
|
| 17 |
+
game_state["step"] = 2
|
| 18 |
+
return "You see a river. Do you 'swim' across or 'build a raft'?"
|
| 19 |
+
elif user_input == "right":
|
| 20 |
+
game_state["step"] = 3
|
| 21 |
+
return "You meet a wizard. Do you 'talk' to him or 'run' away?"
|
|
|
|
| 22 |
else:
|
| 23 |
+
return "Please type 'left' or 'right'."
|
| 24 |
+
|
| 25 |
+
elif step == 2:
|
| 26 |
+
if user_input == "swim":
|
| 27 |
+
game_state["step"] = 4
|
| 28 |
+
return "You tried to swim and got swept away! Game Over. Type anything to restart."
|
| 29 |
+
elif user_input == "build a raft":
|
| 30 |
+
game_state["step"] = 4
|
| 31 |
+
return "You safely crossed the river. You win! 🏆 Type anything to restart."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
+
return "Please choose 'swim' or 'build a raft'."
|
| 34 |
+
|
| 35 |
+
elif step == 3:
|
| 36 |
+
if user_input == "talk":
|
| 37 |
+
game_state["step"] = 4
|
| 38 |
+
return "The wizard gives you a treasure. You win! 🧙♂️ Type anything to restart."
|
| 39 |
+
elif user_input == "run":
|
| 40 |
+
game_state["step"] = 4
|
| 41 |
+
return "The wizard curses you! Game Over. Type anything to restart."
|
| 42 |
else:
|
| 43 |
+
return "Please choose 'talk' or 'run'."
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
+
game_state["step"] = 0
|
| 47 |
+
return "The game is restarted. Do you want to go 'left' or 'right'?"
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Gradio interface
|
| 50 |
iface = gr.Interface(
|
| 51 |
+
fn=play_game,
|
| 52 |
+
inputs=gr.Textbox(placeholder="Type your choice here..."),
|
| 53 |
+
outputs="text",
|
| 54 |
+
title="🕹️ Simple Text Adventure Game",
|
| 55 |
+
description="Make your choices and see where the story goes!"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
+
iface.launch()
|
|
|