Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,134 @@
|
|
1 |
import streamlit as st
|
2 |
-
import gradio as gr
|
3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def main():
|
6 |
-
|
7 |
st.title("๐ฑ Cat Rider ๐")
|
8 |
st.markdown("""
|
9 |
## Welcome to Cat Rider!
|
10 |
In this immersive adventure, you will explore the thrilling world of feline riders. This game sets the stage for dramatic situations and guided storytelling with engaging interactive elements.
|
11 |
""")
|
12 |
|
13 |
-
# Rules
|
14 |
st.markdown("""
|
15 |
### ๐ Game Rules
|
16 |
| ๐ค๏ธ Step | ๐ Description |
|
@@ -22,46 +140,82 @@ def main():
|
|
22 |
| 5๏ธโฃ | Complete the Quest |
|
23 |
""")
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
''
|
39 |
-
|
40 |
-
#
|
41 |
-
st.
|
42 |
-
st.slider('
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Example of Data Table
|
46 |
-
|
|
|
47 |
'Gear': ['Helmet', 'Armor', 'Boots', 'Gloves'],
|
48 |
'Protection Level': [8, 7, 5, 4]
|
49 |
}
|
50 |
-
|
51 |
-
|
52 |
-
st.dataframe(df)
|
53 |
-
|
54 |
-
# Gradio Interactive Elements
|
55 |
-
def cat_rider_gear(gear_strength):
|
56 |
-
return f'{gear_strength} - The perfect balance of agility and protection!'
|
57 |
-
|
58 |
-
interface = gr.Interface(
|
59 |
-
fn=cat_rider_gear,
|
60 |
-
inputs=gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Gear Strength"),
|
61 |
-
outputs="text"
|
62 |
-
)
|
63 |
-
|
64 |
-
interface.launch(share=True)
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
main()
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import random
|
5 |
+
import json
|
6 |
+
import uuid
|
7 |
+
from datetime import datetime
|
8 |
+
from streamlit_flow import streamlit_flow
|
9 |
+
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
|
10 |
+
from streamlit_flow.layouts import TreeLayout
|
11 |
|
12 |
+
# ๐ Game World Data
|
13 |
+
SITUATIONS = [
|
14 |
+
{
|
15 |
+
"id": "feline_escape",
|
16 |
+
"name": "The Great Feline Escape",
|
17 |
+
"description": "Your cat rider is trapped in an old mansion, which is about to be demolished. Using agility, wit, and bravery, orchestrate the perfect escape.",
|
18 |
+
"emoji": "๐ช"
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"id": "lost_temple",
|
22 |
+
"name": "The Treasure of the Lost Temple",
|
23 |
+
"description": "On a quest to retrieve an ancient artifact, your cat rider must navigate through a labyrinth filled with traps and guardian spirits.",
|
24 |
+
"emoji": "๐๏ธ"
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"id": "royal_tournament",
|
28 |
+
"name": "The Royal Tournament",
|
29 |
+
"description": "Compete in a grand tournament where the finest cat riders showcase their skills and bravery to earn the title of the Royal Rider.",
|
30 |
+
"emoji": "๐"
|
31 |
+
}
|
32 |
+
]
|
33 |
+
|
34 |
+
ACTIONS = [
|
35 |
+
{
|
36 |
+
"id": "stealth",
|
37 |
+
"name": "Use Stealth",
|
38 |
+
"description": "Sneak past obstacles or enemies without being detected.",
|
39 |
+
"emoji": "๐คซ"
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"id": "agility",
|
43 |
+
"name": "Showcase Agility",
|
44 |
+
"description": "Perform impressive acrobatic maneuvers to overcome challenges.",
|
45 |
+
"emoji": "๐"
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"id": "charm",
|
49 |
+
"name": "Charm Others",
|
50 |
+
"description": "Use your cat's natural charisma to win over allies or distract foes.",
|
51 |
+
"emoji": "๐ป"
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"id": "resourcefulness",
|
55 |
+
"name": "Be Resourceful",
|
56 |
+
"description": "Utilize the environment or items in creative ways to solve problems.",
|
57 |
+
"emoji": "๐ง "
|
58 |
+
}
|
59 |
+
]
|
60 |
+
|
61 |
+
# ๐ฒ Game Mechanics
|
62 |
+
def generate_situation():
|
63 |
+
"""๐ Randomly select a new situation for the player"""
|
64 |
+
return random.choice(SITUATIONS)
|
65 |
+
|
66 |
+
def generate_actions():
|
67 |
+
"""๐ญ Generate a set of three random actions for the player to choose from"""
|
68 |
+
return random.sample(ACTIONS, 3)
|
69 |
+
|
70 |
+
def evaluate_action(action, gear_strength, rider_skill, history):
|
71 |
+
"""โ๏ธ Determine the outcome of a player's action"""
|
72 |
+
base_success_chance = (gear_strength + rider_skill) / 2
|
73 |
+
if action['id'] in history:
|
74 |
+
success_chance = base_success_chance + (history[action['id']] * 2)
|
75 |
+
else:
|
76 |
+
success_chance = base_success_chance
|
77 |
+
outcome = random.randint(1, 100) <= success_chance
|
78 |
+
return outcome, success_chance
|
79 |
+
|
80 |
+
# ๐ณ Journey Visualization
|
81 |
+
def create_graph_from_history(history_df):
|
82 |
+
"""๐ Create a visual representation of the player's journey"""
|
83 |
+
nodes = []
|
84 |
+
edges = []
|
85 |
+
for index, row in history_df.iterrows():
|
86 |
+
node_id = f"{index}-{row['situation_id']}-{row['action_id']}"
|
87 |
+
content = f"{row['situation_emoji']} {row['situation_name']}\n{row['action_emoji']} {row['action_name']}\nOutcome: {'โ
Success' if row['outcome'] else 'โ Failure'}\n๐ {row['timestamp']}"
|
88 |
+
new_node = StreamlitFlowNode(node_id, (0, 0), {'content': content}, 'output', 'bottom', 'top')
|
89 |
+
nodes.append(new_node)
|
90 |
+
|
91 |
+
if index > 0:
|
92 |
+
prev_node_id = f"{index-1}-{history_df.iloc[index-1]['situation_id']}-{history_df.iloc[index-1]['action_id']}"
|
93 |
+
new_edge = StreamlitFlowEdge(f"{prev_node_id}-{node_id}", prev_node_id, node_id, animated=True, dashed=True)
|
94 |
+
edges.append(new_edge)
|
95 |
+
|
96 |
+
return nodes, edges
|
97 |
+
|
98 |
+
# ๐ Game State Management
|
99 |
+
def update_game_state(game_state, situation, action, outcome, timestamp):
|
100 |
+
"""๐ Update the game state with the latest action and its outcome"""
|
101 |
+
new_record = pd.DataFrame({
|
102 |
+
'user_id': [game_state['user_id']],
|
103 |
+
'timestamp': [timestamp],
|
104 |
+
'situation_id': [situation['id']],
|
105 |
+
'situation_name': [situation['name']],
|
106 |
+
'situation_emoji': [situation['emoji']],
|
107 |
+
'action_id': [action['id']],
|
108 |
+
'action_name': [action['name']],
|
109 |
+
'action_emoji': [action['emoji']],
|
110 |
+
'outcome': [outcome],
|
111 |
+
'score': [game_state['score']]
|
112 |
+
})
|
113 |
+
game_state['history_df'] = pd.concat([game_state['history_df'], new_record], ignore_index=True)
|
114 |
+
|
115 |
+
if action['id'] in game_state['history']:
|
116 |
+
game_state['history'][action['id']] += 1 if outcome else -1
|
117 |
+
else:
|
118 |
+
game_state['history'][action['id']] = 1 if outcome else -1
|
119 |
+
|
120 |
+
return game_state
|
121 |
+
|
122 |
+
# ๐ฎ Main Game Application
|
123 |
def main():
|
124 |
+
"""๐ฎ Main game loop and Streamlit interface"""
|
125 |
st.title("๐ฑ Cat Rider ๐")
|
126 |
st.markdown("""
|
127 |
## Welcome to Cat Rider!
|
128 |
In this immersive adventure, you will explore the thrilling world of feline riders. This game sets the stage for dramatic situations and guided storytelling with engaging interactive elements.
|
129 |
""")
|
130 |
|
131 |
+
# ๐ Game Rules
|
132 |
st.markdown("""
|
133 |
### ๐ Game Rules
|
134 |
| ๐ค๏ธ Step | ๐ Description |
|
|
|
140 |
| 5๏ธโฃ | Complete the Quest |
|
141 |
""")
|
142 |
|
143 |
+
# ๐ Initialize game state
|
144 |
+
if 'game_state' not in st.session_state:
|
145 |
+
st.session_state.game_state = {
|
146 |
+
'user_id': str(uuid.uuid4()),
|
147 |
+
'score': 0,
|
148 |
+
'history': {},
|
149 |
+
'gear_strength': 5,
|
150 |
+
'rider_skill': 5,
|
151 |
+
'history_df': pd.DataFrame(columns=['user_id', 'timestamp', 'situation_id', 'situation_name', 'situation_emoji', 'action_id', 'action_name', 'action_emoji', 'outcome', 'score'])
|
152 |
+
}
|
153 |
+
|
154 |
+
# ๐ Game Stats
|
155 |
+
st.sidebar.markdown("## ๐ Game Stats")
|
156 |
+
st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
|
157 |
+
|
158 |
+
# ๐ฆธ Character Stats
|
159 |
+
gear_strength = st.sidebar.slider('Gear Strength ๐ก๏ธ', 1, 10, st.session_state.game_state['gear_strength'])
|
160 |
+
rider_skill = st.sidebar.slider('Rider Skill ๐', 1, 10, st.session_state.game_state['rider_skill'])
|
161 |
+
|
162 |
+
# ๐ญ Game Loop
|
163 |
+
situation = generate_situation()
|
164 |
+
actions = generate_actions()
|
165 |
+
|
166 |
+
st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']}")
|
167 |
+
st.markdown(situation['description'])
|
168 |
+
st.markdown("### ๐ญ Choose your action:")
|
169 |
+
|
170 |
+
cols = st.columns(3)
|
171 |
+
for i, action in enumerate(actions):
|
172 |
+
if cols[i].button(f"{action['emoji']} {action['name']}"):
|
173 |
+
outcome, success_chance = evaluate_action(action, gear_strength, rider_skill, st.session_state.game_state['history'])
|
174 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
175 |
+
|
176 |
+
st.markdown(f"You decided to: **{action['name']}**")
|
177 |
+
st.markdown(action['description'])
|
178 |
+
st.markdown(f"**Outcome:** {'โ
Success!' if outcome else 'โ Failure.'}")
|
179 |
+
st.markdown(f"**Success Chance:** {success_chance:.2f}%")
|
180 |
+
|
181 |
+
if outcome:
|
182 |
+
st.session_state.game_state['score'] += 1
|
183 |
+
|
184 |
+
# ๐ Update game state
|
185 |
+
st.session_state.game_state = update_game_state(
|
186 |
+
st.session_state.game_state,
|
187 |
+
situation,
|
188 |
+
action,
|
189 |
+
outcome,
|
190 |
+
timestamp
|
191 |
+
)
|
192 |
+
|
193 |
+
# ๐ณ Display Journey Graph
|
194 |
+
if not st.session_state.game_state['history_df'].empty:
|
195 |
+
st.markdown("## ๐ณ Your Journey")
|
196 |
+
nodes, edges = create_graph_from_history(st.session_state.game_state['history_df'])
|
197 |
+
streamlit_flow('cat_rider_flow',
|
198 |
+
nodes,
|
199 |
+
edges,
|
200 |
+
layout=TreeLayout(direction='down'),
|
201 |
+
fit_view=True,
|
202 |
+
height=600)
|
203 |
+
|
204 |
+
# ๐ Character Stats Visualization
|
205 |
+
data = {"Stat": ["Gear Strength ๐ก๏ธ", "Rider Skill ๐"],
|
206 |
+
"Value": [gear_strength, rider_skill]}
|
207 |
+
df = pd.DataFrame(data)
|
208 |
+
fig = px.bar(df, x='Stat', y='Value', title="Cat Rider Stats ๐")
|
209 |
+
st.plotly_chart(fig)
|
210 |
|
211 |
# Example of Data Table
|
212 |
+
st.markdown("### ๐ ๏ธ Available Gear")
|
213 |
+
gear_data = {
|
214 |
'Gear': ['Helmet', 'Armor', 'Boots', 'Gloves'],
|
215 |
'Protection Level': [8, 7, 5, 4]
|
216 |
}
|
217 |
+
gear_df = pd.DataFrame(gear_data)
|
218 |
+
st.dataframe(gear_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
if __name__ == "__main__":
|
221 |
main()
|