awacke1's picture
Create backup.app.py
ae46e18 verified
raw
history blame
3.82 kB
import streamlit as st
import random
import numpy as np
# Initialize or reset the game state with all features
def initialize_state():
st.session_state.update({
'character': None,
'knowledge': 0,
'inventory': [],
'map_position': (2, 2), # Center position in a 5x5 grid
'current_location': 'forest edge',
'initialized': True
})
if 'initialized' not in st.session_state:
initialize_state()
# Characters and descriptions
characters = {
"Wizard": {"emoji": "πŸ§™β€β™‚οΈ", "knowledge": 5, "description": "Wise and powerful, connected to magical forces."},
"Witch": {"emoji": "πŸ§™β€β™€οΈ", "knowledge": 5, "description": "Cunning, skilled in potions and spells."}
}
# Locations and emoji grid maps
locations = {
'forest edge': np.array([["🌲", "🌳", "πŸƒ", "🌲", "🌿"], ["πŸ„", "πŸƒ", "🌳", "πŸƒ", "πŸ„"], ["🌲", "🚢", "🌿", "πŸƒ", "🌳"], ["🌳", "πŸƒ", "πŸ„", "🌿", "🌲"], ["🌲", "πŸƒ", "🌳", "πŸƒ", "🌿"]]),
'deep forest': np.array([["🌲", "🌿", "πŸƒ", "🌳", "πŸ„"], ["🌿", "🌳", "πŸƒ", "🌲", "🌿"], ["πŸ„", "πŸƒ", "🚢", "πŸƒ", "🌳"], ["🌳", "🌲", "πŸƒ", "πŸ„", "🌿"], ["πŸƒ", "πŸƒ", "🌳", "🌲", "🌿"]]),
# Add other locations with corresponding maps
}
# Actions and movement
directions = {"North": (-1, 0), "South": (1, 0), "West": (0, -1), "East": (0, 1)}
# Main Streamlit application
def main():
st.title("The Magic Workshop In The Great Tree 🌳✨")
if st.session_state.character is None:
choose_character()
else:
display_character_info()
navigate_world()
def choose_character():
st.header("Choose your character πŸ§™β€β™‚οΈπŸ§™β€β™€οΈ")
character = st.selectbox("Select your character", options=list(characters.keys()), format_func=lambda x: f"{x} {characters[x]['emoji']}")
if st.button("Choose"):
st.session_state.character = characters[character]
st.experimental_rerun()
def display_character_info():
char = st.session_state.character
st.subheader(f"Character: {char['description']}")
st.write(f"Knowledge: {char['knowledge']}")
if st.session_state.inventory:
st.write(f"Inventory: {', '.join(st.session_state.inventory)}")
else:
st.write("Inventory: Empty")
def navigate_world():
st.header("Explore the World")
location = st.session_state.current_location
st.write(f"You are in the {location}.")
display_map(location)
move_direction = st.selectbox("Which direction would you like to go?", options=list(directions.keys()))
if st.button("Move"):
move_player(move_direction)
handle_location_change()
def display_map(location):
map_with_player = locations[location]
map_display = "\n".join(["".join(row) for row in map_with_player])
st.text(map_display)
def move_player(direction):
dx, dy = directions[direction]
x, y = st.session_state.map_position
nx, ny = x + dx, y + dy
if 0 <= nx < 5 and 0 <= ny < 5: # Ensure new position is within bounds
# Update map position
st.session_state.map_position = (nx, ny)
# Update the character's position on the map
update_map_position(st.session_state.current_location, st.session_state.map_position)
def update_map_position(location, new_position):
# Remove old position
locations[location][st.session_state.map_position] = "πŸƒ" # Replace with default terrain
# Set new position
st.session_state.map_position = new_position
locations[location][new_position] = "🚢"
def handle_location_change():
# This function can be expanded to include logic for handling encounters, finding items, etc., based on the new location
pass
if __name__ == "__main__":
main()