CatRider / app.py
awacke1's picture
Update app.py
e9122f4 verified
raw
history blame
8.53 kB
import streamlit as st
import pandas as pd
import plotly.express as px
import random
import json
import uuid
from datetime import datetime
from streamlit_flow import streamlit_flow
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
from streamlit_flow.layouts import TreeLayout
# ๐ŸŒ Game World Data
SITUATIONS = [
{
"id": "feline_escape",
"name": "The Great Feline Escape",
"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.",
"emoji": "๐Ÿšช"
},
{
"id": "lost_temple",
"name": "The Treasure of the Lost Temple",
"description": "On a quest to retrieve an ancient artifact, your cat rider must navigate through a labyrinth filled with traps and guardian spirits.",
"emoji": "๐Ÿ›๏ธ"
},
{
"id": "royal_tournament",
"name": "The Royal Tournament",
"description": "Compete in a grand tournament where the finest cat riders showcase their skills and bravery to earn the title of the Royal Rider.",
"emoji": "๐Ÿ‘‘"
}
]
ACTIONS = [
{
"id": "stealth",
"name": "Use Stealth",
"description": "Sneak past obstacles or enemies without being detected.",
"emoji": "๐Ÿคซ"
},
{
"id": "agility",
"name": "Showcase Agility",
"description": "Perform impressive acrobatic maneuvers to overcome challenges.",
"emoji": "๐Ÿƒ"
},
{
"id": "charm",
"name": "Charm Others",
"description": "Use your cat's natural charisma to win over allies or distract foes.",
"emoji": "๐Ÿ˜ป"
},
{
"id": "resourcefulness",
"name": "Be Resourceful",
"description": "Utilize the environment or items in creative ways to solve problems.",
"emoji": "๐Ÿง "
}
]
# ๐ŸŽฒ Game Mechanics
def generate_situation():
"""๐Ÿ”„ Randomly select a new situation for the player"""
return random.choice(SITUATIONS)
def generate_actions():
"""๐ŸŽญ Generate a set of three random actions for the player to choose from"""
return random.sample(ACTIONS, 3)
def evaluate_action(action, gear_strength, rider_skill, history):
"""โš–๏ธ Determine the outcome of a player's action"""
base_success_chance = (gear_strength + rider_skill) / 2
if action['id'] in history:
success_chance = base_success_chance + (history[action['id']] * 2)
else:
success_chance = base_success_chance
outcome = random.randint(1, 100) <= success_chance
return outcome, success_chance
# ๐ŸŒณ Journey Visualization
def create_graph_from_history(history_df):
"""๐Ÿ“Š Create a visual representation of the player's journey"""
nodes = []
edges = []
for index, row in history_df.iterrows():
node_id = f"{index}-{row['situation_id']}-{row['action_id']}"
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']}"
new_node = StreamlitFlowNode(node_id, (0, 0), {'content': content}, 'output', 'bottom', 'top')
nodes.append(new_node)
if index > 0:
prev_node_id = f"{index-1}-{history_df.iloc[index-1]['situation_id']}-{history_df.iloc[index-1]['action_id']}"
new_edge = StreamlitFlowEdge(f"{prev_node_id}-{node_id}", prev_node_id, node_id, animated=True, dashed=True)
edges.append(new_edge)
return nodes, edges
# ๐Ÿ”„ Game State Management
def update_game_state(game_state, situation, action, outcome, timestamp):
"""๐Ÿ”„ Update the game state with the latest action and its outcome"""
new_record = pd.DataFrame({
'user_id': [game_state['user_id']],
'timestamp': [timestamp],
'situation_id': [situation['id']],
'situation_name': [situation['name']],
'situation_emoji': [situation['emoji']],
'action_id': [action['id']],
'action_name': [action['name']],
'action_emoji': [action['emoji']],
'outcome': [outcome],
'score': [game_state['score']]
})
game_state['history_df'] = pd.concat([game_state['history_df'], new_record], ignore_index=True)
if action['id'] in game_state['history']:
game_state['history'][action['id']] += 1 if outcome else -1
else:
game_state['history'][action['id']] = 1 if outcome else -1
return game_state
# ๐ŸŽฎ Main Game Application
def main():
"""๐ŸŽฎ Main game loop and Streamlit interface"""
st.title("๐Ÿฑ Cat Rider ๐Ÿ‡")
st.markdown("""
## Welcome to Cat Rider!
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.
""")
# ๐Ÿ“œ Game Rules
st.markdown("""
### ๐Ÿ“œ Game Rules
| ๐Ÿ›ค๏ธ Step | ๐Ÿ“ Description |
|---------|----------------|
| 1๏ธโƒฃ | Choose your Cat Rider |
| 2๏ธโƒฃ | Select the Riding Gear |
| 3๏ธโƒฃ | Set off on an Adventure |
| 4๏ธโƒฃ | Encounter Challenges and Make Decisions |
| 5๏ธโƒฃ | Complete the Quest |
""")
# ๐Ÿ Initialize game state
if 'game_state' not in st.session_state:
st.session_state.game_state = {
'user_id': str(uuid.uuid4()),
'score': 0,
'history': {},
'gear_strength': 5,
'rider_skill': 5,
'history_df': pd.DataFrame(columns=['user_id', 'timestamp', 'situation_id', 'situation_name', 'situation_emoji', 'action_id', 'action_name', 'action_emoji', 'outcome', 'score'])
}
# ๐Ÿ“Š Game Stats
st.sidebar.markdown("## ๐Ÿ“Š Game Stats")
st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
# ๐Ÿฆธ Character Stats
gear_strength = st.sidebar.slider('Gear Strength ๐Ÿ›ก๏ธ', 1, 10, st.session_state.game_state['gear_strength'])
rider_skill = st.sidebar.slider('Rider Skill ๐Ÿ‡', 1, 10, st.session_state.game_state['rider_skill'])
# ๐ŸŽญ Game Loop
situation = generate_situation()
actions = generate_actions()
st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']}")
st.markdown(situation['description'])
st.markdown("### ๐ŸŽญ Choose your action:")
cols = st.columns(3)
for i, action in enumerate(actions):
if cols[i].button(f"{action['emoji']} {action['name']}"):
outcome, success_chance = evaluate_action(action, gear_strength, rider_skill, st.session_state.game_state['history'])
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
st.markdown(f"You decided to: **{action['name']}**")
st.markdown(action['description'])
st.markdown(f"**Outcome:** {'โœ… Success!' if outcome else 'โŒ Failure.'}")
st.markdown(f"**Success Chance:** {success_chance:.2f}%")
if outcome:
st.session_state.game_state['score'] += 1
# ๐Ÿ”„ Update game state
st.session_state.game_state = update_game_state(
st.session_state.game_state,
situation,
action,
outcome,
timestamp
)
# ๐ŸŒณ Display Journey Graph
if not st.session_state.game_state['history_df'].empty:
st.markdown("## ๐ŸŒณ Your Journey")
nodes, edges = create_graph_from_history(st.session_state.game_state['history_df'])
streamlit_flow('cat_rider_flow',
nodes,
edges,
layout=TreeLayout(direction='down'),
fit_view=True,
height=600)
# ๐Ÿ“Š Character Stats Visualization
data = {"Stat": ["Gear Strength ๐Ÿ›ก๏ธ", "Rider Skill ๐Ÿ‡"],
"Value": [gear_strength, rider_skill]}
df = pd.DataFrame(data)
fig = px.bar(df, x='Stat', y='Value', title="Cat Rider Stats ๐Ÿ“Š")
st.plotly_chart(fig)
# Example of Data Table
st.markdown("### ๐Ÿ› ๏ธ Available Gear")
gear_data = {
'Gear': ['Helmet', 'Armor', 'Boots', 'Gloves'],
'Protection Level': [8, 7, 5, 4]
}
gear_df = pd.DataFrame(gear_data)
st.dataframe(gear_df)
if __name__ == "__main__":
main()