Spaces:
Running
Running
File size: 18,752 Bytes
a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 f6ec781 6dda961 87bce5e 6a1913e 6dda961 2f9b023 6dda961 2f9b023 6dda961 a161f40 2f9b023 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 2f9b023 6dda961 2f9b023 6dda961 2f9b023 6dda961 2f9b023 6dda961 2f9b023 6dda961 2f9b023 f6ec781 6dda961 f6ec781 6dda961 87bce5e 6dda961 6a1913e 6dda961 f6ec781 fcac069 6a1913e fcac069 6a1913e 6dda961 6a1913e 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 a161f40 6dda961 | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
// Game variables
let scene, camera, renderer, controls;
let player, walls = [], enemies = [], coins = [], emojis = [], powerups = [];
let score = 0, timeLeft = 60, gameActive = false, gameOver = false;
let playerSpeed = 5, invincible = false, doublePoints = false;
let clock = new THREE.Clock();
let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
// Audio elements
const bgMusic = document.getElementById('bg-music');
const coinSound = document.getElementById('coin-sound');
const enemySound = document.getElementById('enemy-sound');
const winSound = document.getElementById('win-sound');
const loseSound = document.getElementById('lose-sound');
// UI elements
const scoreDisplay = document.getElementById('score-display');
const timerDisplay = document.getElementById('timer');
// Initialize the game
function init() {
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
// Create camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 0);
// Create renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Add pointer lock controls
controls = new THREE.PointerLockControls(camera, renderer.domElement);
// Create player
createPlayer();
// Generate maze
generateMaze();
// Add lights
addLights();
// Add event listeners
setupEventListeners();
// Start render loop
animate();
// Setup restart button
document.getElementById('restart-btn').addEventListener('click', restartGame);
}
// Create player
function createPlayer() {
// Create player with better 3D model
const group = new THREE.Group();
// Body
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.5, 1, 0.3),
new THREE.MeshStandardMaterial({ color: 0x4ECDC4 })
);
body.position.y = 0.5;
// Head
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.3, 16, 16),
new THREE.MeshStandardMaterial({ color: 0xFFD166 })
);
head.position.y = 1.15;
// Eyes
const eyeGeometry = new THREE.SphereGeometry(0.05, 8, 8);
const leftEye = new THREE.Mesh(eyeGeometry, new THREE.MeshStandardMaterial({ color: 0x000000 }));
const rightEye = new THREE.Mesh(eyeGeometry, new THREE.MeshStandardMaterial({ color: 0x000000 }));
leftEye.position.set(0.1, 1.15, 0.25);
rightEye.position.set(-0.1, 1.15, 0.25);
group.add(body);
group.add(head);
group.add(leftEye);
group.add(rightEye);
player = group;
player.castShadow = true;
player.position.y = 0.85;
scene.add(player);
}
// Generate maze
function generateMaze() {
const size = 30;
const gridSize = 15;
// Add skybox
const skyGeometry = new THREE.BoxGeometry(100, 100, 100);
const skyMaterial = new THREE.MeshBasicMaterial({
color: 0x87CEEB,
side: THREE.BackSide
});
const skybox = new THREE.Mesh(skyGeometry, skyMaterial);
scene.add(skybox);
const maze = [];
// Create grid
for (let i = 0; i < gridSize; i++) {
maze[i] = [];
for (let j = 0; j < gridSize; j++) {
maze[i][j] = Math.random() > 0.7 ? 1 : 0;
}
}
// Ensure exit path
maze[0][0] = 0;
maze[gridSize-1][gridSize-1] = 0;
// Create walls
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (maze[i][j] === 1) {
const wall = new THREE.Mesh(
new THREE.BoxGeometry(size/gridSize, 2, size/gridSize),
new THREE.MeshStandardMaterial({ color: 0xFFD166 })
);
wall.position.x = (i - gridSize/2) * (size/gridSize);
wall.position.z = (j - gridSize/2) * (size/gridSize);
wall.position.y = 1;
wall.castShadow = true;
wall.receiveShadow = true;
scene.add(wall);
walls.push(wall);
}
}
}
// Create coins
for (let i = 0; i < 30; i++) {
createCoin();
}
// Create enemies
for (let i = 0; i < 5; i++) {
createEnemy();
}
// Create powerups
for (let i = 0; i < 3; i++) {
createPowerup();
}
// Create exit
const exit = new THREE.Mesh(
new THREE.BoxGeometry(size/gridSize, 2, size/gridSize),
new THREE.MeshStandardMaterial({ color: 0x06D6A0, transparent: true, opacity: 0.5 })
);
exit.position.x = (gridSize/2 - 0.5) * (size/gridSize);
exit.position.z = (gridSize/2 - 0.5) * (size/gridSize);
exit.position.y = 1;
scene.add(exit);
exit.isExit = true;
walls.push(exit);
}
function createCoin() {
const coin = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, 0.1, 32),
new THREE.MeshStandardMaterial({
color: 0xFFD700,
metalness: 0.9,
roughness: 0.1
})
);
coin.castShadow = true;
coin.rotation.x = Math.PI / 2;
coin.position.set(
(Math.random() - 0.5) * 18,
0.5,
(Math.random() - 0.5) * 18
);
coin.castShadow = true;
coin.isCoin = true;
scene.add(coin);
coins.push(coin);
}
function createEnemy() {
const enemy = new THREE.Mesh(
new THREE.ConeGeometry(0.4, 1.2, 4),
new THREE.MeshStandardMaterial({
color: 0xFF6B6B,
emissive: 0x880000,
emissiveIntensity: 0.2
})
);
enemy.position.set(
(Math.random() - 0.5) * 18,
0.5,
(Math.random() - 0.5) * 18
);
enemy.castShadow = true;
enemy.speed = 0.02 + Math.random() * 0.03;
enemy.direction = new THREE.Vector3(Math.random() - 0.5, 0, Math.random() - 0.5).normalize();
scene.add(enemy);
enemies.push(enemy);
// Create emoji above enemy
const emoji = document.createElement('div');
emoji.className = 'enemy-emote';
emoji.textContent = ['😈', '👹', '👻', '🤡', '👾'][Math.floor(Math.random() * 5)];
document.getElementById('ui').appendChild(emo);
emojis.push({ element: emoji, enemy: enemy });
}
function addLights() {
// Ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// Directional light (sun)
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
scene.add(directionalLight);
// Hemisphere light for more natural outdoor lighting
const hemisphereLight = new THREE.HemisphereLight(0x87CEEB, 0x006400, 0.3);
scene.add(hemisphereLight);
// Add some point lights for better 3D effect
const pointLight1 = new THREE.PointLight(0xFFD166, 0.5, 10);
pointLight1.position.set(5, 3, 5);
scene.add(pointLight1);
const pointLight2 = new THREE.PointLight(0x4ECDC4, 0.5, 10);
pointLight2.position.set(-5, 3, -5);
scene.add(pointLight2);
}
function setupEventListeners() {
// Movement controls
document.addEventListener('keydown', (event) => {
switch (event.code) {
case 'KeyW':
case 'ArrowUp': moveForward = true; break;
case 'KeyA':
case 'ArrowLeft': moveLeft = true; break;
case 'KeyS':
case 'ArrowDown': moveBackward = true; break;
case 'KeyD':
case 'ArrowRight': moveRight = true; break;
}
});
document.addEventListener('keyup', (event) => {
switch (event.code) {
case 'KeyW':
case 'ArrowUp': moveForward = false; break;
case 'KeyA':
case 'ArrowLeft': moveLeft = false; break;
case 'KeyS':
case 'ArrowDown': moveBackward = false; break;
case 'KeyD':
case 'ArrowRight': moveRight = false; break;
}
});
// Touch controls
const upBtn = document.getElementById('up');
const leftBtn = document.getElementById('left');
const downBtn = document.getElementById('down');
const rightBtn = document.getElementById('right');
const lookBtn = document.getElementById('look');
// Touch start events
upBtn.addEventListener('touchstart', (e) => { e.preventDefault(); moveForward = true; });
leftBtn.addEventListener('touchstart', (e) => { e.preventDefault(); moveLeft = true; });
downBtn.addEventListener('touchstart', (e) => { e.preventDefault(); moveBackward = true; });
rightBtn.addEventListener('touchstart', (e) => { e.preventDefault(); moveRight = true; });
lookBtn.addEventListener('touchstart', (e) => {
e.preventDefault();
if (!controls.isLocked) {
controls.lock();
}
});
// Touch end events
upBtn.addEventListener('touchend', (e) => { e.preventDefault(); moveForward = false; });
leftBtn.addEventListener('touchend', (e) => { e.preventDefault(); moveLeft = false; });
downBtn.addEventListener('touchend', (e) => { e.preventDefault(); moveBackward = false; });
rightBtn.addEventListener('touchend', (e) => { e.preventDefault(); moveRight = false; });
// Mouse events for desktop
upBtn.addEventListener('mousedown', () => moveForward = true);
leftBtn.addEventListener('mousedown', () => moveLeft = true);
downBtn.addEventListener('mousedown', () => moveBackward = true);
rightBtn.addEventListener('mousedown', () => moveRight = true);
lookBtn.addEventListener('mousedown', () => {
if (!controls.isLocked) {
controls.lock();
}
});
upBtn.addEventListener('mouseup', () => moveForward = false);
leftBtn.addEventListener('mouseup', () => moveLeft = false);
downBtn.addEventListener('mouseup', () => moveBackward = false);
rightBtn.addEventListener('mouseup', () => moveRight = false);
// Pointer lock controls
const lockChangeAlert = () => {
if (document.pointerLockElement === renderer.domElement) {
controls.isLocked = true;
} else {
controls.isLocked = false;
}
};
document.addEventListener('pointerlockchange', lockChangeAlert, false);
// Window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
}
function startGame() {
// This function is now empty since we start the game automatically
}
function restartGame() {
// Reset all game state
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
// Clear all arrays
walls = [];
enemies = [];
coins = [];
powerups = [];
emojis.forEach(emoji => emoji.element.remove());
emojis = [];
// Reset variables
score = 0;
timeLeft = 60;
gameActive = false;
gameOver = false;
playerSpeed = 5;
invincible = false;
doublePoints = false;
// Update UI
scoreDisplay.textContent = `Coins: ${score}`;
timerDisplay.textContent = `Time: ${timeLeft}`;
// Hide end screen
document.getElementById('end-screen').style.display = 'none';
document.getElementById('start-screen').style.display = 'flex';
// Rebuild game elements
createPlayer();
generateMaze();
addLights();
// Reattach camera
controls.getObject().position.set(0, 1.6, 0);
camera.position.set(0, 1.6, 0);
camera.lookAt(0, 1.6, -1);
}
function endGame(win) {
gameOver = true;
gameActive = false;
if (controls.isLocked) {
controls.unlock();
}
const endScreen = document.getElementById('end-screen');
const endMessage = document.getElementById('end-message');
endScreen.style.display = 'flex';
if (win) {
endMessage.textContent = `You Escaped!\nFinal Score: ${score}`;
endMessage.style.animation = 'rainbow 2s infinite';
winSound.play();
} else {
endMessage.textContent = `Time's Up!\nFinal Score: ${score}`;
endMessage.style.color = '#FF6B6B';
loseSound.play();
}
}
function createPowerup() {
const types = ['speed', 'invincible', 'double'];
const type = types[Math.floor(Math.random() * types.length)];
let color, shape;
switch(type) {
case 'speed':
color = 0x00FF00;
shape = new THREE.SphereGeometry(0.4);
break;
case 'invincible':
color = 0x0000FF;
shape = new THREE.TetrahedronGeometry(0.5);
break;
case 'double':
color = 0xFF00FF;
shape = new THREE.OctahedronGeometry(0.4);
break;
}
const powerup = new THREE.Mesh(
shape,
new THREE.MeshStandardMaterial({
color: color,
emissive: color,
emissiveIntensity: 0.3
})
);
powerup.position.set(
(Math.random() - 0.5) * 28,
0.5,
(Math.random() - 0.5) * 28
);
powerup.castShadow = true;
powerup.type = type;
scene.add(powerup);
powerups.push(powerup);
}
function checkCollisions() {
// Check coin collisions
for (let i = coins.length - 1; i >= 0; i--) {
const coin = coins[i];
if (player.position.distanceTo(coin.position) < 1) {
scene.remove(coin);
coins.splice(i, 1);
score += doublePoints ? 20 : 10;
scoreDisplay.textContent = `Coins: ${score}`;
coinSound.play();
createCoin();
}
}
// Check powerup collisions
for (let i = powerups.length - 1; i >= 0; i--) {
const powerup = powerups[i];
if (player.position.distanceTo(powerup.position) < 1) {
scene.remove(powerup);
powerups.splice(i, 1);
switch(powerup.type) {
case 'speed':
playerSpeed = 8;
setTimeout(() => playerSpeed = 5, 10000);
break;
case 'invincible':
invincible = true;
setTimeout(() => invincible = false, 15000);
break;
case 'double':
doublePoints = true;
setTimeout(() => doublePoints = false, 20000);
break;
}
coinSound.play();
}
}
// Check wall collisions
const playerBox = new THREE.Box3().setFromObject(player);
for (const wall of walls) {
const wallBox = new THREE.Box3().setFromObject(wall);
if (playerBox.intersectsBox(wallBox)) {
if (wall.isExit) {
endGame(true);
} else {
// Simple collision response
const direction = new THREE.Vector3().subVectors(player.position, wall.position).normalize();
player.position.add(direction.multiplyScalar(0.1));
}
}
}
// Check enemy collisions
for (let i = 0; i < enemies.length; i++) {
const enemy = enemies[i];
if (player.position.distanceTo(enemy.position) < 1 && !invincible) {
score = Math.max(0, score - 5);
scoreDisplay.textContent = `Coins: ${score}`;
enemySound.play();
// Push player away
const direction = new THREE.Vector3().subVectors(player.position, enemy.position).normalize();
player.position.add(direction.multiplyScalar(1.5));
}
}
}
function updateEnemies() {
for (let i = 0; i < enemies.length; i++) {
const enemy = enemies[i];
// Simple AI - change direction randomly
if (Math.random() < 0.01) {
enemy.direction = new THREE.Vector3(Math.random() - 0.5, 0, Math.random() - 0.5).normalize();
}
// Move enemy
enemy.position.add(enemy.direction.clone().multiplyScalar(enemy.speed));
// Boundary check
if (enemy.position.x > 9 || enemy.position.x < -9 || enemy.position.z > 9 || enemy.position.z < -9) {
enemy.direction.negate();
}
// Wall collision
const enemyBox = new THREE.Box3().setFromObject(enemy);
for (const wall of walls) {
const wallBox = new THREE.Box3().setFromObject(wall);
if (enemyBox.intersectsBox(wallBox) && !wall.isExit) {
enemy.direction.negate();
break;
}
}
// Update emoji position
if (emojis[i]) {
const emoji = emojis[i].element;
const screenPos = enemy.position.clone().project(camera);
emoji.style.left = `${(screenPos.x * 0.5 + 0.5) * window.innerWidth}px`;
emoji.style.top = `${(-(screenPos.y * 0.5) + 0.5) * window.innerHeight - 50}px`;
// Show emoji when enemy is close
const distance = player.position.distanceTo(enemy.position);
emoji.style.opacity = distance < 5 ? '1' : '0';
}
}
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
if (gameActive && controls.isLocked) {
// Player movement
const speed = playerSpeed * delta;
if (moveForward) {
controls.moveForward(speed);
}
if (moveBackward) {
controls.moveForward(-speed);
}
if (moveLeft) {
controls.moveRight(-speed);
}
if (moveRight) {
controls.moveRight(speed);
}
// Update player position
player.position.copy(controls.getObject().position);
player.position.y = 0.85;
// Check collisions
checkCollisions();
// Update enemies
updateEnemies();
}
renderer.render(scene, camera);
}
// Start the game
init(); |