File size: 3,818 Bytes
ae46e18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
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()
|