awacke1's picture
Update app.py
0438bd7 verified
raw
history blame
4.33 kB
import streamlit as st
import streamlit.components.v1 as components
import random
import time
import os
# Load the initial HTML5 app
if not os.path.exists("chofko_ecosystem.html"):
with open("chofko_ecosystem.html", "w") as f:
f.write('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chofko's Diverse Ecosystem Simulator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/1.2.0/aframe.min.js"></script>
<style>
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
}
.controls button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#score {
position: absolute;
top: 20px;
left: 20px;
font-size: 24px;
color: white;
background-color: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<a-scene>
<a-assets>
<img id="sky" src="/api/placeholder/1024/512" alt="surrealist sky">
</a-assets>
<a-sky src="#sky"></a-sky>
<a-plane position="0 0 -4" rotation="-90 0 0" width="100" height="100" color="#7BC8A4"></a-plane>
<a-entity id="entities"></a-entity>
<a-entity id="camera" camera look-controls position="0 40 0" rotation="-90 0 0"></a-entity>
</a-scene>
<div id="score">Score: 0</div>
<div class="controls">
<button onmousedown="startMove('left')" onmouseup="stopMove('left')" ontouchstart="startMove('left')" ontouchend="stopMove('left')">Left</button>
<button onmousedown="startMove('right')" onmouseup="stopMove('right')" ontouchstart="startMove('right')" ontouchend="stopMove('right')">Right</button>
<button onmousedown="startMove('up')" onmouseup="stopMove('up')" ontouchstart="startMove('up')" ontouchend="stopMove('up')">Up</button>
<button onmousedown="startMove('down')" onmouseup="stopMove('down')" ontouchstart="startMove('down')" ontouchend="stopMove('down')">Down</button>
<button onclick="toggleSpeed()">Toggle Speed</button>
</div>
<script>
// ... (Include the entire JavaScript code from the original HTML file here)
</script>
</body>
</html>
''')
with open("chofko_ecosystem.html", "r") as f:
initial_html = f.read()
# Initialize session state
if "html_content" not in st.session_state:
st.session_state.html_content = initial_html
if "player_names" not in st.session_state:
st.session_state.player_names = []
# Function to generate a random name for a new player
def generate_player_name():
adjectives = ["Adventurous", "Brave", "Curious", "Daring", "Fearless", "Intrepid", "Valiant"]
nouns = ["Explorer", "Wanderer", "Pathfinder", "Trailblazer", "Voyager", "Wayfarer", "Traveler"]
return random.choice(adjectives) + " " + random.choice(nouns)
# Function to add a new player to the scene
def add_player():
player_name = generate_player_name()
while player_name in st.session_state.player_names:
player_name = generate_player_name()
st.session_state.player_names.append(player_name)
player_html = f'''
new Entity(
{random.uniform(-50, 50)},
{random.uniform(-50, 50)},
false,
'a-sphere',
'{random.choice(['male', 'female'])}',
1
);
'''
st.session_state.html_content = st.session_state.html_content.replace("createEntities();", f"createEntities();\n {player_html}")
# Streamlit app
def main():
st.set_page_config(page_title="Chofko's Diverse Ecosystem Simulator", layout="wide")
st.title("Chofko's Diverse Ecosystem Simulator")
# Add a new player button
if st.button("Add New Player", key="add_player"):
add_player()
# Display the HTML5 app
components.html(st.session_state.html_content, height=800)
# Editor for HTML5 content
st.subheader("Edit Content")
new_html = st.text_area("HTML5 Content", st.session_state.html_content, height=300)
# Save button
if st.button("Save", key="save_button"):
st.session_state.html_content = new_html
# Refresh button
if st.button("Refresh", key="refresh_button"):
st.experimental_rerun()
if __name__ == "__main__":
main()