Update app.py
Browse files
app.py
CHANGED
@@ -17,10 +17,10 @@ from audio_recorder_streamlit import audio_recorder
|
|
17 |
nest_asyncio.apply()
|
18 |
|
19 |
# Page Config
|
20 |
-
st.set_page_config(page_title="Galaxian Snake
|
21 |
|
22 |
-
st.title("Galaxian Snake
|
23 |
-
st.write("Navigate a
|
24 |
|
25 |
# Sliders for container size
|
26 |
max_width = min(1200, st.session_state.get('window_width', 1200))
|
@@ -159,7 +159,7 @@ html_code = f"""
|
|
159 |
<head>
|
160 |
<meta charset="UTF-8">
|
161 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
162 |
-
<title>Galaxian Snake
|
163 |
<style>
|
164 |
body {{ margin: 0; overflow: hidden; font-family: Arial, sans-serif; }}
|
165 |
#gameContainer {{ width: {container_width}px; height: {container_height}px; position: relative; }}
|
@@ -183,7 +183,7 @@ html_code = f"""
|
|
183 |
<body>
|
184 |
<div id="gameContainer">
|
185 |
<div class="ui-container">
|
186 |
-
<h2>Galaxian Snake
|
187 |
<div id="players">Players: 1</div>
|
188 |
<div id="score">Score: 0</div>
|
189 |
<div id="length">Length: 3</div>
|
@@ -199,7 +199,7 @@ html_code = f"""
|
|
199 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
200 |
<script>
|
201 |
// Game variables
|
202 |
-
let score = 0, players = {{}}, foodItems = [], lSysCreatures = [], quineAgents = [], buildings = [];
|
203 |
let snake = [], moveDir = new THREE.Vector3(1, 0, 0), moveCounter = 0, moveInterval = 0.1;
|
204 |
const initialLength = 3, playerName = "{st.session_state.username}";
|
205 |
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
|
@@ -208,26 +208,16 @@ html_code = f"""
|
|
208 |
// Scene setup
|
209 |
const scene = new THREE.Scene();
|
210 |
const camera = new THREE.PerspectiveCamera(75, {container_width} / {container_height}, 0.1, 1000);
|
211 |
-
camera.position.set(0,
|
|
|
212 |
const renderer = new THREE.WebGLRenderer({{ antialias: true }});
|
213 |
renderer.setSize({container_width}, {container_height});
|
214 |
renderer.shadowMap.enabled = true;
|
215 |
document.getElementById('gameContainer').appendChild(renderer.domElement);
|
216 |
|
217 |
// Lighting
|
218 |
-
const ambientLight = new THREE.AmbientLight(0xffffff, 0.
|
219 |
scene.add(ambientLight);
|
220 |
-
const sunLight = new THREE.DirectionalLight(0xffddaa, 0.8);
|
221 |
-
sunLight.castShadow = true;
|
222 |
-
sunLight.shadow.mapSize.width = 2048;
|
223 |
-
sunLight.shadow.mapSize.height = 2048;
|
224 |
-
sunLight.shadow.camera.near = 1;
|
225 |
-
sunLight.shadow.camera.far = 500;
|
226 |
-
sunLight.shadow.camera.left = -100;
|
227 |
-
sunLight.shadow.camera.right = 100;
|
228 |
-
sunLight.shadow.camera.top = 100;
|
229 |
-
sunLight.shadow.camera.bottom = -100;
|
230 |
-
scene.add(sunLight);
|
231 |
|
232 |
// Ground
|
233 |
const textureLoader = new THREE.TextureLoader();
|
@@ -342,6 +332,16 @@ html_code = f"""
|
|
342 |
road.position.set(0, 0.01, z * spacing);
|
343 |
scene.add(road);
|
344 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
}}
|
346 |
|
347 |
function evolveCity() {{
|
@@ -531,10 +531,6 @@ html_code = f"""
|
|
531 |
return;
|
532 |
}}
|
533 |
}}
|
534 |
-
|
535 |
-
const headPos = newHead.position;
|
536 |
-
camera.position.set(headPos.x, headPos.y + 20, headPos.z + 30);
|
537 |
-
camera.lookAt(headPos);
|
538 |
}}
|
539 |
|
540 |
function updateQuineAgents(delta) {{
|
@@ -557,14 +553,14 @@ html_code = f"""
|
|
557 |
}}
|
558 |
}}
|
559 |
|
560 |
-
//
|
561 |
let cycleTime = 0;
|
562 |
function updateLighting(delta) {{
|
563 |
cycleTime += delta;
|
564 |
const cycleDuration = 120;
|
565 |
const angle = (cycleTime / cycleDuration) * Math.PI * 2;
|
566 |
-
|
567 |
-
|
568 |
const dayColor = new THREE.Color(0x87CEEB);
|
569 |
const nightColor = new THREE.Color(0x001133);
|
570 |
scene.background = dayColor.clone().lerp(nightColor, Math.max(0, -Math.sin(angle)));
|
@@ -585,17 +581,9 @@ html_code = f"""
|
|
585 |
chatBox.scrollTop = chatBox.scrollHeight;
|
586 |
}};
|
587 |
|
588 |
-
ws.onopen = function() {{
|
589 |
-
|
590 |
-
}};
|
591 |
-
|
592 |
-
ws.onerror = function(error) {{
|
593 |
-
console.error('WebSocket error:', error);
|
594 |
-
}};
|
595 |
-
|
596 |
-
ws.onclose = function() {{
|
597 |
-
console.log('Disconnected from WebSocket server');
|
598 |
-
}};
|
599 |
|
600 |
// Game loop
|
601 |
let lastTime = performance.now();
|
@@ -643,7 +631,6 @@ audio_bytes = audio_recorder()
|
|
643 |
if audio_bytes:
|
644 |
with open("temp_audio.wav", "wb") as f:
|
645 |
f.write(audio_bytes)
|
646 |
-
# Here you would typically transcribe the audio, but for simplicity, we'll simulate a message
|
647 |
message = "Voice message received"
|
648 |
asyncio.run(save_chat_entry(st.session_state.username, message))
|
649 |
|
@@ -656,6 +643,6 @@ st.sidebar.write("""
|
|
656 |
### How to Play
|
657 |
- **W/A/S/D or Arrow Keys**: Move your snake
|
658 |
- Eat yellow cubes to grow and score
|
659 |
-
- City evolves with new buildings
|
660 |
- Chat with other players in real-time
|
661 |
""")
|
|
|
17 |
nest_asyncio.apply()
|
18 |
|
19 |
# Page Config
|
20 |
+
st.set_page_config(page_title="Galaxian Snake 2D Multiplayer", layout="wide")
|
21 |
|
22 |
+
st.title("Galaxian Snake 2D Multiplayer")
|
23 |
+
st.write("Navigate a top-down city with others, eat food, and chat in real-time!")
|
24 |
|
25 |
# Sliders for container size
|
26 |
max_width = min(1200, st.session_state.get('window_width', 1200))
|
|
|
159 |
<head>
|
160 |
<meta charset="UTF-8">
|
161 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
162 |
+
<title>Galaxian Snake 2D Multiplayer</title>
|
163 |
<style>
|
164 |
body {{ margin: 0; overflow: hidden; font-family: Arial, sans-serif; }}
|
165 |
#gameContainer {{ width: {container_width}px; height: {container_height}px; position: relative; }}
|
|
|
183 |
<body>
|
184 |
<div id="gameContainer">
|
185 |
<div class="ui-container">
|
186 |
+
<h2>Galaxian Snake 2D</h2>
|
187 |
<div id="players">Players: 1</div>
|
188 |
<div id="score">Score: 0</div>
|
189 |
<div id="length">Length: 3</div>
|
|
|
199 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
200 |
<script>
|
201 |
// Game variables
|
202 |
+
let score = 0, players = {{}}, foodItems = [], lSysCreatures = [], quineAgents = [], buildings = [], lights = [];
|
203 |
let snake = [], moveDir = new THREE.Vector3(1, 0, 0), moveCounter = 0, moveInterval = 0.1;
|
204 |
const initialLength = 3, playerName = "{st.session_state.username}";
|
205 |
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];
|
|
|
208 |
// Scene setup
|
209 |
const scene = new THREE.Scene();
|
210 |
const camera = new THREE.PerspectiveCamera(75, {container_width} / {container_height}, 0.1, 1000);
|
211 |
+
camera.position.set(0, 100, 0); // Top-down view
|
212 |
+
camera.lookAt(0, 0, 0); // Looking straight down
|
213 |
const renderer = new THREE.WebGLRenderer({{ antialias: true }});
|
214 |
renderer.setSize({container_width}, {container_height});
|
215 |
renderer.shadowMap.enabled = true;
|
216 |
document.getElementById('gameContainer').appendChild(renderer.domElement);
|
217 |
|
218 |
// Lighting
|
219 |
+
const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // Dim ambient for night effect
|
220 |
scene.add(ambientLight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
// Ground
|
223 |
const textureLoader = new THREE.TextureLoader();
|
|
|
332 |
road.position.set(0, 0.01, z * spacing);
|
333 |
scene.add(road);
|
334 |
}}
|
335 |
+
// Add city lights at intersections
|
336 |
+
for (let x = -citySize; x <= citySize; x++) {{
|
337 |
+
for (let z = -citySize; z <= citySize; z++) {{
|
338 |
+
const light = new THREE.PointLight(0xffff99, 1, 20); // Yellowish light, intensity 1, distance 20
|
339 |
+
light.position.set(x * spacing, 5, z * spacing); // 5 units above ground
|
340 |
+
light.castShadow = true;
|
341 |
+
scene.add(light);
|
342 |
+
lights.push(light);
|
343 |
+
}}
|
344 |
+
}}
|
345 |
}}
|
346 |
|
347 |
function evolveCity() {{
|
|
|
531 |
return;
|
532 |
}}
|
533 |
}}
|
|
|
|
|
|
|
|
|
534 |
}}
|
535 |
|
536 |
function updateQuineAgents(delta) {{
|
|
|
553 |
}}
|
554 |
}}
|
555 |
|
556 |
+
// Lighting cycle
|
557 |
let cycleTime = 0;
|
558 |
function updateLighting(delta) {{
|
559 |
cycleTime += delta;
|
560 |
const cycleDuration = 120;
|
561 |
const angle = (cycleTime / cycleDuration) * Math.PI * 2;
|
562 |
+
const intensity = Math.max(0, Math.sin(angle)) * 0.5 + 0.5; // Varies between 0.5 and 1
|
563 |
+
lights.forEach(light => light.intensity = intensity);
|
564 |
const dayColor = new THREE.Color(0x87CEEB);
|
565 |
const nightColor = new THREE.Color(0x001133);
|
566 |
scene.background = dayColor.clone().lerp(nightColor, Math.max(0, -Math.sin(angle)));
|
|
|
581 |
chatBox.scrollTop = chatBox.scrollHeight;
|
582 |
}};
|
583 |
|
584 |
+
ws.onopen = function() {{ console.log('Connected to WebSocket server'); }};
|
585 |
+
ws.onerror = function(error) {{ console.error('WebSocket error:', error); }};
|
586 |
+
ws.onclose = function() {{ console.log('Disconnected from WebSocket server'); }};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
|
588 |
// Game loop
|
589 |
let lastTime = performance.now();
|
|
|
631 |
if audio_bytes:
|
632 |
with open("temp_audio.wav", "wb") as f:
|
633 |
f.write(audio_bytes)
|
|
|
634 |
message = "Voice message received"
|
635 |
asyncio.run(save_chat_entry(st.session_state.username, message))
|
636 |
|
|
|
643 |
### How to Play
|
644 |
- **W/A/S/D or Arrow Keys**: Move your snake
|
645 |
- Eat yellow cubes to grow and score
|
646 |
+
- City evolves with new buildings and lights at intersections
|
647 |
- Chat with other players in real-time
|
648 |
""")
|