Escape from the Alien body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #000; color: #0f0; } #game-container { text-align: center; } #game-grid { display: grid; grid-template-columns: repeat(15, 20px); grid-gap: 1px; margin: 20px auto; } .cell { width: 20px; height: 20px; background-color: #111; border: 1px solid #0f0; cursor: pointer; } .player { background-color: #00f; } .exit { background-color: #0f0; } .alien { background-color: #f00; } .obstacle { background-color: #888; } .player-obstacle { background-color: #00ffff; } button { background-color: #0f0; color: #000; border: none; padding: 10px 20px; margin: 5px; cursor: pointer; } #timer, #steps, #level, #score, #obstacles-left { font-size: 18px; margin: 10px 0; } #copyright { position: fixed; bottom: 10px; width: 100%; text-align: center; font-size: 14px; color: #0f0; }

Escape from the Alien

Time: 5:00
Steps: 50
Level: 1
Score: 0
Obstacles Left: 3
const gridSize = 15; let playerPosition, exitPosition, alienPosition; let timeLeft = 300; // 5 minutes in seconds let stepsLeft = 50; // Starting steps for each level let obstaclesLeft = 3; // Player obstacles per level let gameInterval, obstacleInterval; let obstacles = new Set(); let playerObstacles = new Set(); let level = 1; let score = 0; let alienVisible = false; function startGame() { document.getElementById('menu').style.display = 'none'; document.getElementById('game-area').style.display = 'block'; initializeGame(); gameInterval = setInterval(updateGame, 1000); obstacleInterval = setInterval(changeObstacles, 10000); // Change obstacles every 10 seconds document.addEventListener('keydown', handleKeyPress); } function initializeGame() { level = 1; score = 0; timeLeft = 300; stepsLeft = 50; obstaclesLeft = 3; alienVisible = false; playerObstacles.clear(); const grid = document.getElementById('game-grid'); grid.innerHTML = ''; for (let i = 0; i < gridSize * gridSize; i++) { const cell = document.createElement('div'); cell.className = 'cell'; cell.setAttribute('data-index', i); cell.addEventListener('click', placeObstacle); grid.appendChild(cell); } resetPositions(); generateObstacles(); updateGridDisplay(); } function resetPositions() { playerPosition = Math.floor(Math.random() * gridSize * gridSize); do { exitPosition = Math.floor(Math.random() * gridSize * gridSize); } while (exitPosition === playerPosition); do { alienPosition = Math.floor(Math.random() * gridSize * gridSize); } while (alienPosition === playerPosition || alienPosition === exitPosition); } function generateObstacles() { obstacles.clear(); const obstacleCount = Math.floor(gridSize * gridSize * 0.2); // 20% of cells are obstacles while (obstacles.size < obstacleCount) { const obstaclePosition = Math.floor(Math.random() * gridSize * gridSize); if (obstaclePosition !== playerPosition && obstaclePosition !== exitPosition && obstaclePosition !== alienPosition) { obstacles.add(obstaclePosition); } } } function changeObstacles() { const currentObstacles = new Set(obstacles); // Remove some existing obstacles for (let obstacle of currentObstacles) { if (Math.random() < 0.3) { // 30% chance to remove each obstacle obstacles.delete(obstacle); } } // Add new obstacles const newObstacleCount = Math.floor(gridSize * gridSize * 0.05); // Add 5% new obstacles for (let i = 0; i 0 && stepsLeft >= 2) { const cellIndex = parseInt(event.target.getAttribute('data-index')); if (cellIndex !== playerPosition && cellIndex !== exitPosition && cellIndex !== alienPosition && !obstacles.has(cellIndex) && !playerObstacles.has(cellIndex)) { playerObstacles.add(cellIndex); obstaclesLeft--; stepsLeft -= 2; updateGridDisplay(); document.getElementById('obstacles-left').textContent = `Obstacles Left: ${obstaclesLeft}`; document.getElementById('steps').textContent = `Steps: ${stepsLeft}`; checkGameStatus(); } } } function updateGridDisplay() { const cells = document.getElementsByClassName('cell'); for (let i = 0; i < cells.length; i++) { cells[i].className = 'cell'; if (i === playerPosition) cells[i].classList.add('player'); if (i === exitPosition) cells[i].classList.add('exit'); if (alienVisible && i === alienPosition) cells[i].classList.add('alien'); if (obstacles.has(i)) cells[i].classList.add('obstacle'); if (playerObstacles.has(i)) cells[i].classList.add('player-obstacle'); } } function handleKeyPress(event) { if (stepsLeft = 0 && to < gridSize * gridSize && !obstacles.has(to) && !playerObstacles.has(to); } function moveAlien() { for (let i = 0; i < 2; i++) { // Alien moves twice let bestMove = alienPosition; let bestDistance = calculateDistance(alienPosition, playerPosition); const possibleMoves = [ alienPosition - gridSize, // Up alienPosition + 1, // Right alienPosition + gridSize, // Down alienPosition - 1 // Left ]; for (let move of possibleMoves) { if (isValidMove(alienPosition, move)) { let distance = calculateDistance(move, playerPosition); if (distance < bestDistance) { bestMove = move; bestDistance = distance; } } } alienPosition = bestMove; if (alienPosition === playerPosition) { break; // Stop if the alien catches the player } } alienVisible = false; } function calculateDistance(pos1, pos2) { const row1 = Math.floor(pos1 / gridSize); const col1 = pos1 % gridSize; const row2 = Math.floor(pos2 / gridSize); const col2 = pos2 % gridSize; return Math.abs(row1 - row2) + Math.abs(col1 - col2); } function updateGame() { timeLeft--; const minutes = Math.floor(timeLeft / 60); const seconds = timeLeft % 60; document.getElementById('timer').textContent = `Time: ${minutes}:${seconds.toString().padStart(2, '0')}`; checkGameStatus(); } function checkGameStatus() { if (playerPosition === exitPosition) { levelUp(); } else if (playerPosition === alienPosition) { endGame("Game Over! The alien caught you!"); } else if (timeLeft <= 0 || stepsLeft = 2) { stepsLeft -= 2; document.getElementById('steps').textContent = `Steps: ${stepsLeft}`; alienVisible = true; updateGridDisplay(); setTimeout(() => { alienVisible = false; updateGridDisplay(); }, 1000); checkGameStatus(); } else { alert("Not enough steps to use the motion detector!"); } } function showInstructions() { alert("Escape from the Alien: Your goal is to reach the exit (green) before the alien catches you, time runs out, or you run out of steps. The alien moves twice as fast as you and always tries to catch you! Use arrow keys to move. Avoid obstacles (gray) that change every 10 seconds. You can place up to 3 of your own obstacles (cyan) per level by clicking on empty cells. Using the motion detector or placing an obstacle costs 2 steps. Good luck!"); }