|
import streamlit as st |
|
import random |
|
import numpy as np |
|
|
|
|
|
def initialize_state(): |
|
st.session_state.update({ |
|
'character': None, |
|
'knowledge': 0, |
|
'inventory': [], |
|
'map_position': (2, 2), |
|
'current_location': 'forest edge', |
|
'initialized': True |
|
}) |
|
|
|
if 'initialized' not in st.session_state: |
|
initialize_state() |
|
|
|
|
|
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 = { |
|
'forest edge': np.array([["π²", "π³", "π", "π²", "πΏ"], ["π", "π", "π³", "π", "π"], ["π²", "πΆ", "πΏ", "π", "π³"], ["π³", "π", "π", "πΏ", "π²"], ["π²", "π", "π³", "π", "πΏ"]]), |
|
'deep forest': np.array([["π²", "πΏ", "π", "π³", "π"], ["πΏ", "π³", "π", "π²", "πΏ"], ["π", "π", "πΆ", "π", "π³"], ["π³", "π²", "π", "π", "πΏ"], ["π", "π", "π³", "π²", "πΏ"]]), |
|
|
|
} |
|
|
|
|
|
directions = {"North": (-1, 0), "South": (1, 0), "West": (0, -1), "East": (0, 1)} |
|
|
|
|
|
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: |
|
|
|
st.session_state.map_position = (nx, ny) |
|
|
|
update_map_position(st.session_state.current_location, st.session_state.map_position) |
|
|
|
def update_map_position(location, new_position): |
|
|
|
locations[location][st.session_state.map_position] = "π" |
|
|
|
st.session_state.map_position = new_position |
|
locations[location][new_position] = "πΆ" |
|
|
|
def handle_location_change(): |
|
|
|
pass |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|