Untitled
unknown
plain_text
5 months ago
3.3 kB
2
Indexable
let bus; // Represents the bus let school; // Represents the school let obstacles = []; // Array to store obstacles let level = 1; // Start at level 1 let maxLevels = 15; // Total number of levels let mapWidth = 10; // Grid width let mapHeight = 10; // Grid height let grid = []; // 2D array representing the map let busPosition = { x: 0, y: 0 }; // Starting bus position let schoolPosition = { x: 9, y: 9 }; // Default school position function setupGrid() { grid = []; for (let i = 0; i < mapHeight; i++) { let row = []; for (let j = 0; j < mapWidth; j++) { row.push('empty'); // Initialize grid cells as 'empty' } grid.push(row); } grid[busPosition.x][busPosition.y] = 'bus'; // Set bus position grid[schoolPosition.x][schoolPosition.y] = 'school'; // Set school position setupObstacles(); } function setupObstacles() { obstacles = []; let obstacleCount = level + 2; // Increase obstacle count with each level for (let i = 0; i < obstacleCount; i++) { let obsX = Math.floor(Math.random() * mapWidth); let obsY = Math.floor(Math.random() * mapHeight); if (grid[obsX][obsY] === 'empty') { grid[obsX][obsY] = 'obstacle'; obstacles.push({ x: obsX, y: obsY }); } } }function displayGrid() { for (let i = 0; i < grid.length; i++) { let row = ''; for (let j = 0; j < grid[i].length; j++) { row += grid[i][j] + ' '; } console.log(row); } } function moveBus(direction) { grid[busPosition.x][busPosition.y] = 'empty'; // Clear old position if (direction === 'up' && busPosition.y > 0) busPosition.y--; if (direction === 'down' && busPosition.y < mapHeight - 1) busPosition.y++; if (direction === 'left' && busPosition.x > 0) busPosition.x--; if (direction === 'right' && busPosition.x < mapWidth - 1) busPosition.x++; if (grid[busPosition.x][busPosition.y] === 'obstacle') { console.log("Crashed into obstacle!"); resetBusPosition(); // Reset bus if it crashes } else if (grid[busPosition.x][busPosition.y] === 'school') { console.log("Arrived at school! Level up."); nextLevel(); } grid[busPosition.x][busPosition.y] = 'bus'; // Update new position } function resetBusPosition() { busPosition = { x: 0, y: 0 }; setupGrid(); // Redraw the grid } function nextLevel() { if (level < maxLevels) { level++; schoolPosition = { x: Math.floor(Math.random() * mapWidth), y: Math.floor(Math.random() * mapHeight) }; // New school position busPosition = { x: 0, y: 0 }; // Reset bus to start setupGrid(); // Reset grid for new level console.log("Welcome to Level " + level); } else { console.log("You've completed all levels! Congratulations!"); } } document.addEventListener('keydown', function(event) { if (event.key === 'ArrowUp') moveBus('up'); if (event.key === 'ArrowDown') moveBus('down'); if (event.key === 'ArrowLeft') moveBus('left'); if (event.key === 'ArrowRight') moveBus('right'); }); function startGame() { setupGrid(); displayGrid(); console.log("Use arrow keys to move the bus to the school."); } startGame();
Editor is loading...
Leave a Comment