create a single html amazing random maze, make it directly, after creation release 1000…
- User
create a single html amazing random maze, make it directly, after creation release 1000 balls at start point and see it evolve, generate maze at once, make sure to use all screen
- Assistant
Certainly! Below is an HTML file that generates a random maze covering the entire screen. After the maze is generated, 1000 balls are released from the starting point, and you'll see them navigate through the maze.
Copy the entire code into a file with an
.htmlextension and open it in a modern web browser.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Random Maze with Moving Balls</title> <style> body, html { margin: 0; padding: 0; overflow: hidden; background: #000; } canvas { display: block; background: #000; } </style> </head> <body> <canvas id="mazeCanvas"></canvas> <script> // Get the canvas element and context const canvas = document.getElementById('mazeCanvas'); const ctx = canvas.getContext('2d'); // Set canvas to full window size canvas.width = window.innerWidth; canvas.height = window.innerHeight; // Maze variables const cellSize = 20; // Size of each cell const cols = Math.floor(canvas.width / cellSize); const rows = Math.floor(canvas.height / cellSize); const grid = []; const stack = []; // Ball variables const balls = []; const numBalls = 1000; // Directions const DIR = { TOP: 'top', RIGHT: 'right', BOTTOM: 'bottom', LEFT: 'left', }; // Cell constructor function Cell(x, y) { this.x = x; this.y = y; this.walls = { top: true, right: true, bottom: true, left: true, }; this.visited = false; } // Initialize grid for (let y = 0; y < rows; y++) { for (let x = 0; x < cols; x++) { const cell = new Cell(x, y); grid.push(cell); } } // Get index of cell in grid array function index(x, y) { if (x < 0 || y < 0 || x >= cols || y >= rows) { return -1; } return x + y * cols; } // Generate maze using Recursive Backtracker Algorithm function generateMaze() { const current = grid[0]; current.visited = true; stack.push(current); while (stack.length > 0) { const current = stack.pop(); const next = checkNeighbors(current); if (next) { stack.push(current); removeWalls(current, next); next.visited = true; stack.push(next); } } } // Check unvisited neighbors function checkNeighbors(cell) { const neighbors = []; const top = grid[index(cell.x, cell.y - 1)]; const right = grid[index(cell.x + 1, cell.y)]; const bottom = grid[index(cell.x, cell.y + 1)]; const left = grid[index(cell.x - 1, cell.y)]; if (top && !top.visited) neighbors.push(top); if (right && !right.visited) neighbors.push(right); if (bottom && !bottom.visited) neighbors.push(bottom); if (left && !left.visited) neighbors.push(left); if (neighbors.length > 0) { const r = Math.floor(Math.random() * neighbors.length); return neighbors[r]; } else { return undefined; } } // Remove walls between two cells function removeWalls(a, b) { const x = a.x - b.x; if (x === 1) { a.walls.left = false; b.walls.right = false; } else if (x === -1) { a.walls.right = false; b.walls.left = false; } const y = a.y - b.y; if (y === 1) { a.walls.top = false; b.walls.bottom = false; } else if (y === -1) { a.walls.bottom = false; b.walls.top = false; } } // Draw the maze function drawMaze() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = '#FFF'; grid.forEach(cell => { const x = cell.x * cellSize; const y = cell.y * cellSize; if (cell.walls.top) { ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + cellSize, y); ctx.stroke(); } if (cell.walls.right) { ctx.beginPath(); ctx.moveTo(x + cellSize, y); ctx.lineTo(x + cellSize, y + cellSize); ctx.stroke(); } if (cell.walls.bottom) { ctx.beginPath(); ctx.moveTo(x + cellSize, y + cellSize); ctx.lineTo(x, y + cellSize); ctx.stroke(); } if (cell.walls.left) { ctx.beginPath(); ctx.moveTo(x, y + cellSize); ctx.lineTo(x, y); ctx.stroke(); } }); } // Ball constructor function Ball() { this.x = cellSize / 2; this.y = cellSize / 2; this.currentCell = grid[0]; // Start at the first cell this.path = []; this.speed = 1; // Pixels per frame this.radius = 2; this.color = 'hsl(' + Math.random() * 360 + ', 100%, 50%)'; // Find a path through the maze this.findPath(); } // Find path using Depth-First Search Ball.prototype.findPath = function() { const visited = new Set(); const stack = []; stack.push(this.currentCell); while (stack.length > 0) { const cell = stack.pop(); if (!visited.has(cell)) { visited.add(cell); this.path.push(cell); if (cell.x === cols - 1 && cell.y === rows - 1) { break; // Reached end } const neighbors = []; // Add neighbors without walls between if (!cell.walls.top) neighbors.push(grid[index(cell.x, cell.y - 1)]); if (!cell.walls.right) neighbors.push(grid[index(cell.x + 1, cell.y)]); if (!cell.walls.bottom) neighbors.push(grid[index(cell.x, cell.y + 1)]); if (!cell.walls.left) neighbors.push(grid[index(cell.x - 1, cell.y)]); neighbors.forEach(n => { if (!visited.has(n)) { stack.push(n); } }); } } // Convert path to pixel coordinates this.path = this.path.reverse().map(cell => { return { x: cell.x * cellSize + cellSize / 2, y: cell.y * cellSize + cellSize / 2 }; }); }; // Update ball position Ball.prototype.update = function() { if (this.path.length > 0) { const target = this.path[this.path.length - 1]; const dx = target.x - this.x; const dy = target.y - this.y; const dist = Math.sqrt(dx * dx + dy * dy); if (dist < this.speed) { this.x = target.x; this.y = target.y; this.path.pop(); } else { this.x += (dx / dist) * this.speed; this.y += (dy / dist) * this.speed; } } }; // Draw the ball Ball.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); }; // Initialize balls function initBalls() { for (let i = 0; i < numBalls; i++) { balls.push(new Ball()); } } // Animation loop function animate() { requestAnimationFrame(animate); // Clear only the ball area ctx.fillStyle = 'rgba(0, 0, 0, 0.05)'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw maze once // Commented out to prevent redrawing maze lines over balls // drawMaze(); // Update and draw balls balls.forEach(ball => { ball.update(); ball.draw(); }); } // Run everything generateMaze(); drawMaze(); initBalls(); animate(); </script> </body> </html>Explanation:
-
Canvas Setup: The
<canvas>element is set to the full window size, ensuring the maze uses the entire screen. -
Maze Generation:
- The maze is generated using the Recursive Backtracker Algorithm, which is a depth-first search algorithm. Each cell in the grid has walls on all sides initially.
- As the algorithm visits cells, it removes walls between the current cell and a randomly selected unvisited neighbor.
-
Drawing the Maze:
- After the maze is generated, it's drawn onto the canvas using line segments for the walls.
-
Balls Initialization:
- We create 1000
Ballobjects. Each ball has its own path through the maze, which is calculated using a depth-first search from the starting cell to the end cell. - Each ball is given a random color for visual distinction.
- We create 1000
-
Animation:
- The
animatefunction updates the position of each ball along its calculated path and redraws them on the canvas. - The canvas is partially cleared each frame to create a trailing effect, so you can see the paths the balls have taken.
- The
-
Performance Considerations:
- Drawing 1000 balls and animating them can be intensive. The code uses several optimizations:
- RequestAnimationFrame: Efficiently handles animations.
- Partial Clearing: Instead of clearing the entire canvas every frame, we draw a semi-transparent rectangle over the canvas to create a fading trail effect without redrawing the maze walls.
- Simplified Ball Movement: Balls follow precomputed paths to reduce computational overhead during animation.
- Drawing 1000 balls and animating them can be intensive. The code uses several optimizations:
Notes:
- You can adjust
cellSizeto make the maze cells larger or smaller. - The
numBallsvariable controls how many balls are released. - The
Ballclass uses its own path to prevent all balls from following the exact same path, adding to the visual complexity. - Resize your browser window before loading the page to change the size of the maze. The maze is generated once per page load.
Enjoy watching the balls navigate through the randomly generated maze!
-