Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import string
|
|
| 6 |
# Set Streamlit to wide mode
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
|
| 9 |
-
# Define the enhanced HTML content with Three.js game
|
| 10 |
game_html = """
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html lang="en">
|
|
@@ -57,6 +57,7 @@ game_html = """
|
|
| 57 |
scene.add(player);
|
| 58 |
|
| 59 |
spawnBuildings();
|
|
|
|
| 60 |
|
| 61 |
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
|
| 62 |
scene.add(ambientLight);
|
|
@@ -95,12 +96,35 @@ game_html = """
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
function spawnEnemyFromPosition(position) {
|
| 99 |
const enemyGeometry = new THREE.BoxGeometry(0.8, 0.8, 0.8);
|
| 100 |
const enemyMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, shininess: 50 });
|
| 101 |
const enemy = new THREE.Mesh(enemyGeometry, enemyMaterial);
|
| 102 |
enemy.position.copy(position);
|
| 103 |
enemy.velocity = new THREE.Vector3(Math.random() - 0.5, Math.random() - 0.5, 1).normalize();
|
|
|
|
| 104 |
enemies.push(enemy);
|
| 105 |
scene.add(enemy);
|
| 106 |
}
|
|
@@ -148,6 +172,20 @@ game_html = """
|
|
| 148 |
bullet.velocity = new THREE.Vector3(0, 0, -1);
|
| 149 |
bullet.bounces = 0;
|
| 150 |
bullet.timer = 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
bullets.push(bullet);
|
| 152 |
scene.add(bullet);
|
| 153 |
}
|
|
@@ -158,7 +196,7 @@ game_html = """
|
|
| 158 |
bullets[i].position.add(bullets[i].velocity.clone().multiplyScalar(bulletSpeed * delta));
|
| 159 |
bullets[i].timer -= delta;
|
| 160 |
|
| 161 |
-
if (bullets[i].position.z < -100 || bullets[i].timer <= 0) {
|
| 162 |
scene.remove(bullets[i]);
|
| 163 |
bullets.splice(i, 1);
|
| 164 |
continue;
|
|
@@ -178,13 +216,21 @@ game_html = """
|
|
| 178 |
continue;
|
| 179 |
}
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
| 185 |
-
function
|
| 186 |
for (let i = enemies.length - 1; i >= 0; i--) {
|
| 187 |
-
if (bullet.position.distanceTo(enemies[i].position) < 1) {
|
| 188 |
scene.remove(enemies[i]);
|
| 189 |
enemies.splice(i, 1);
|
| 190 |
scene.remove(bullet);
|
|
@@ -193,6 +239,23 @@ game_html = """
|
|
| 193 |
if (clock.getElapsedTime() - lastHitTime < 2) multiplier += 0.5;
|
| 194 |
lastHitTime = clock.getElapsedTime();
|
| 195 |
updateUI();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
break;
|
| 197 |
}
|
| 198 |
}
|
|
@@ -201,12 +264,27 @@ game_html = """
|
|
| 201 |
function updateFlockingEnemies(delta) {
|
| 202 |
const speed = 3;
|
| 203 |
enemies.forEach(enemy => {
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
}
|
| 209 |
});
|
|
|
|
|
|
|
| 210 |
}
|
| 211 |
|
| 212 |
function updateBuildings(delta) {
|
|
@@ -364,11 +442,18 @@ game_html = """
|
|
| 364 |
</html>
|
| 365 |
"""
|
| 366 |
|
| 367 |
-
# Streamlit app with
|
| 368 |
-
st.
|
| 369 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
# Render the HTML game full-screen
|
| 372 |
-
html(game_html, height=1000, width=2000, scrolling=False)
|
|
|
|
|
|
|
| 373 |
|
| 374 |
-
st.write("Note: The game runs in your browser. Ensure you have an internet connection for Three.js to load.")
|
|
|
|
| 6 |
# Set Streamlit to wide mode
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
|
| 9 |
+
# Define the enhanced HTML content with Three.js game
|
| 10 |
game_html = """
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html lang="en">
|
|
|
|
| 57 |
scene.add(player);
|
| 58 |
|
| 59 |
spawnBuildings();
|
| 60 |
+
spawnEnemyFormations();
|
| 61 |
|
| 62 |
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
|
| 63 |
scene.add(ambientLight);
|
|
|
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
+
function spawnEnemyFormations() {
|
| 100 |
+
const enemyGeometry = new THREE.BoxGeometry(0.8, 0.8, 0.8);
|
| 101 |
+
const enemyMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, shininess: 50 });
|
| 102 |
+
const formations = [
|
| 103 |
+
{ pattern: [[-2, 5], [0, 5], [2, 5], [-1, 6], [1, 6]], center: new THREE.Vector3(0, 5.5, -30) },
|
| 104 |
+
{ pattern: [[-3, 4], [-1, 4], [1, 4], [3, 4], [0, 5]], center: new THREE.Vector3(10, 4.5, -40) },
|
| 105 |
+
{ pattern: [[-2, 6], [0, 6], [2, 6], [-1, 7], [1, 7]], center: new THREE.Vector3(-10, 6.5, -35) }
|
| 106 |
+
];
|
| 107 |
+
|
| 108 |
+
formations.forEach(formation => {
|
| 109 |
+
formation.pattern.forEach(pos => {
|
| 110 |
+
const enemy = new THREE.Mesh(enemyGeometry, enemyMaterial);
|
| 111 |
+
enemy.position.set(formation.center.x + pos[0], formation.center.y + pos[1] - formation.center.y, formation.center.z);
|
| 112 |
+
enemy.velocity = new THREE.Vector3();
|
| 113 |
+
enemy.formationCenter = formation.center;
|
| 114 |
+
enemy.shootTimer = Math.random() * 5;
|
| 115 |
+
enemies.push(enemy);
|
| 116 |
+
scene.add(enemy);
|
| 117 |
+
});
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
function spawnEnemyFromPosition(position) {
|
| 122 |
const enemyGeometry = new THREE.BoxGeometry(0.8, 0.8, 0.8);
|
| 123 |
const enemyMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, shininess: 50 });
|
| 124 |
const enemy = new THREE.Mesh(enemyGeometry, enemyMaterial);
|
| 125 |
enemy.position.copy(position);
|
| 126 |
enemy.velocity = new THREE.Vector3(Math.random() - 0.5, Math.random() - 0.5, 1).normalize();
|
| 127 |
+
enemy.shootTimer = Math.random() * 5;
|
| 128 |
enemies.push(enemy);
|
| 129 |
scene.add(enemy);
|
| 130 |
}
|
|
|
|
| 172 |
bullet.velocity = new THREE.Vector3(0, 0, -1);
|
| 173 |
bullet.bounces = 0;
|
| 174 |
bullet.timer = 5;
|
| 175 |
+
bullet.isPlayerBullet = true;
|
| 176 |
+
bullets.push(bullet);
|
| 177 |
+
scene.add(bullet);
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
function shootEnemyBullet(enemy) {
|
| 181 |
+
const bulletGeometry = new THREE.SphereGeometry(0.2, 8, 8);
|
| 182 |
+
const bulletMaterial = new THREE.MeshPhongMaterial({ color: 0xff00ff, shininess: 100 });
|
| 183 |
+
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
|
| 184 |
+
bullet.position.copy(enemy.position);
|
| 185 |
+
bullet.velocity = new THREE.Vector3(0, 0, 1);
|
| 186 |
+
bullet.bounces = 0;
|
| 187 |
+
bullet.timer = 5;
|
| 188 |
+
bullet.isPlayerBullet = false;
|
| 189 |
bullets.push(bullet);
|
| 190 |
scene.add(bullet);
|
| 191 |
}
|
|
|
|
| 196 |
bullets[i].position.add(bullets[i].velocity.clone().multiplyScalar(bulletSpeed * delta));
|
| 197 |
bullets[i].timer -= delta;
|
| 198 |
|
| 199 |
+
if (bullets[i].position.z < -100 || bullets[i].position.z > 10 || bullets[i].timer <= 0) {
|
| 200 |
scene.remove(bullets[i]);
|
| 201 |
bullets.splice(i, 1);
|
| 202 |
continue;
|
|
|
|
| 216 |
continue;
|
| 217 |
}
|
| 218 |
|
| 219 |
+
for (let j = buildings.length - 1; j >= 0; j--) {
|
| 220 |
+
if (buildings[j].children.some(child => child.visible && bullets[i].position.distanceTo(buildings[j].position) < 3)) {
|
| 221 |
+
bullets[i].velocity.z *= -1;
|
| 222 |
+
bullets[i].bounces++;
|
| 223 |
+
break;
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
checkBulletCollisions(bullets[i], i);
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
+
function checkBulletCollisions(bullet, bulletIndex) {
|
| 232 |
for (let i = enemies.length - 1; i >= 0; i--) {
|
| 233 |
+
if (bullet.position.distanceTo(enemies[i].position) < 1 && bullet.isPlayerBullet) {
|
| 234 |
scene.remove(enemies[i]);
|
| 235 |
enemies.splice(i, 1);
|
| 236 |
scene.remove(bullet);
|
|
|
|
| 239 |
if (clock.getElapsedTime() - lastHitTime < 2) multiplier += 0.5;
|
| 240 |
lastHitTime = clock.getElapsedTime();
|
| 241 |
updateUI();
|
| 242 |
+
return;
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
if (!bullet.isPlayerBullet && bullet.position.distanceTo(player.position) < 1 && !exploding) {
|
| 247 |
+
explodePlayer();
|
| 248 |
+
scene.remove(bullet);
|
| 249 |
+
bullets.splice(bulletIndex, 1);
|
| 250 |
+
return;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
for (let j = bullets.length - 1; j >= 0; j--) {
|
| 254 |
+
if (j !== bulletIndex && bullet.position.distanceTo(bullets[j].position) < 0.4) {
|
| 255 |
+
scene.remove(bullet);
|
| 256 |
+
scene.remove(bullets[j]);
|
| 257 |
+
bullets.splice(Math.max(bulletIndex, j), 1);
|
| 258 |
+
bullets.splice(Math.min(bulletIndex, j), 1);
|
| 259 |
break;
|
| 260 |
}
|
| 261 |
}
|
|
|
|
| 264 |
function updateFlockingEnemies(delta) {
|
| 265 |
const speed = 3;
|
| 266 |
enemies.forEach(enemy => {
|
| 267 |
+
if (enemy.formationCenter) {
|
| 268 |
+
const centerDir = enemy.formationCenter.clone().sub(enemy.position).normalize();
|
| 269 |
+
enemy.velocity.lerp(centerDir, delta * 0.5);
|
| 270 |
+
enemy.position.add(enemy.velocity.clone().multiplyScalar(delta * speed));
|
| 271 |
+
} else {
|
| 272 |
+
enemy.position.add(enemy.velocity.clone().multiplyScalar(delta * speed));
|
| 273 |
+
if (enemy.position.z > 10) {
|
| 274 |
+
scene.remove(enemy);
|
| 275 |
+
enemies.splice(enemies.indexOf(enemy), 1);
|
| 276 |
+
return;
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
enemy.shootTimer -= delta;
|
| 281 |
+
if (enemy.shootTimer <= 0) {
|
| 282 |
+
shootEnemyBullet(enemy);
|
| 283 |
+
enemy.shootTimer = Math.random() * 5 + 2;
|
| 284 |
}
|
| 285 |
});
|
| 286 |
+
|
| 287 |
+
if (enemies.length < 10) spawnEnemyFormations();
|
| 288 |
}
|
| 289 |
|
| 290 |
function updateBuildings(delta) {
|
|
|
|
| 442 |
</html>
|
| 443 |
"""
|
| 444 |
|
| 445 |
+
# Streamlit app with sidebar for title and instructions
|
| 446 |
+
with st.sidebar:
|
| 447 |
+
st.title("Galaxxon - Enhanced Arcade Game")
|
| 448 |
+
st.write("**Controls:**")
|
| 449 |
+
st.write("- Use WASD or Arrow Keys to move")
|
| 450 |
+
st.write("- Spacebar to shoot")
|
| 451 |
+
st.write("**Objective:**")
|
| 452 |
+
st.write("- Crash into buildings for bonus points")
|
| 453 |
+
st.write("- Destroy enemies and avoid their bullets")
|
| 454 |
|
| 455 |
# Render the HTML game full-screen
|
| 456 |
+
html(game_html, height=1000, width=2000, scrolling=False)
|
| 457 |
+
|
| 458 |
+
st.write("Note: The game runs in your browser. Ensure you have an internet connection for Three.js to load.")
|
| 459 |
|
|
|