Introduction :
This project is a simple maze game built using HTML, CSS, and JavaScript. The goal of the game is to navigate a player character from the starting position to the end position within a grid-based maze, using arrow keys for movement. The maze is represented by a grid, where different cells can be paths, walls, the start, or the end.
Explanation :
Structure
- HTML: Defines the main layout of the game.
- CSS: Adds visual styling to the maze and its elements.
- JavaScript: Handles the game’s functionality, including creating the maze and moving the player.
HTML
- The HTML part creates the basic webpage structure.
- A
<div>
element with the ID “maze” is used to hold the maze grid.
CSS
- The CSS styles the page to center the maze both vertically and horizontally.
- The maze is displayed as a grid with 10 rows and 10 columns.
- Different classes (
.cell
,.start
,.end
,.wall
,.player
) are used to style the maze cells with different colors and borders:.cell
: Basic cell styling..start
: Green cell indicating the start position..end
: Red cell indicating the end position..wall
: Black cell representing walls..player
: Blue cell representing the player’s current position.
JavaScript
1. Maze Definition:
- The maze is represented as a 2D array with
1
for paths and0
for walls.
2. Creating the Maze:
- The
createMaze
function reads the 2D array and creates a grid ofdiv
elements for each cell. - Each cell is styled according to its type (path, wall, start, end).
3. Player Movement:
- The player starts at the top-left corner (the start position).
- The player can move up, down, left, or right using the arrow keys.
- The
movePlayer
function handles movement by:- Determining the new position based on the key pressed.
- Checking if the new position is valid (i.e., within the maze bounds and not a wall).
- Updating the player’s position if the move is valid.
4. Utility Functions:
getPlayerPosition
finds the player’s current position within the grid.isValidMove
checks if the proposed move is within the bounds and not a wall.
5. Initialization:
- The maze is created when the page loads.
- The player is placed at the starting position.
- An event listener is added to handle key presses for player movement.
Purpose of Functions
- createMaze: Generates the visual representation of the maze based on the 2D array.
- movePlayer: Updates the player’s position based on key presses and checks for winning condition.
- getPlayerPosition: Finds the current position of the player in the grid.
- isValidMove: Ensures the player moves within the maze’s boundaries and avoids walls.
Summary
The maze game is a simple yet interactive project that combines HTML, CSS, and JavaScript. HTML sets up the structure, CSS styles it, and JavaScript brings the game to life by handling the maze creation and player movement. This project demonstrates basic principles of web development and game logic.
SOURCE CODE :
HTML (index.html)
Maze Game
c
CSS (style.css)
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#maze {
display: grid;
grid-template-columns: repeat(10, 50px);
grid-template-rows: repeat(10, 50px);
gap: 1px;
background-color: #eee;
}
.cell {
background-color: #fff;
border: 1px solid #ccc;
}
.cell.start {
background-color: green;
}
.cell.end {
background-color: red;
}
.cell.wall {
background-color: #000;
}
.cell.player {
background-color: blue;
}
JavaScript (index.js)
const maze = [
[1, 1, 1, 0, 1, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 1, 0,],
[1, 0, 1, 0, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1]
];
const mazeElement = document.getElementById('maze');
function createMaze() {
for (let i = 0; i < maze.length; i++) {
for (let j = 0; j < maze[i].length; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
if (maze[i][j] === 0) {
cell.classList.add('wall');
} else if (i === 0 && j === 0) {
cell.classList.add('start');
} else if (i === maze.length - 1 && j === maze[i].length - 1) {
cell.classList.add('end');
}
mazeElement.appendChild(cell);
}
}
}
function movePlayer(event) {
const key = event.key;
let playerPosition = document.querySelector('.player');
let [row, col] = getPlayerPosition(playerPosition);
let newRow = row;
let newCol = col;
switch (key) {
case 'ArrowUp':
newRow = row - 1;
break;
case 'ArrowDown':
newRow = row + 1;
break;
case 'ArrowLeft':
newCol = col - 1;
break;
case 'ArrowRight':
newCol = col + 1;
break;
default:
return;
}
if (isValidMove(newRow, newCol)) {
playerPosition.classList.remove('player');
mazeElement.children[newRow * maze.length + newCol].classList.add('player');
}
if (newRow === maze.length - 1 && newCol === maze[newRow].length - 1) {
alert('You won!');
window.removeEventListener('keydown', movePlayer);
}
}
function getPlayerPosition(playerPosition) {
const index = Array.from(mazeElement.children).indexOf(playerPosition);
const row = Math.floor(index / maze.length);
const col = index % maze.length;
return [row, col];
}
function isValidMove(row, col) {
return row >= 0 && row < maze.length && col >= 0 && col < maze[row].length && maze[row][col] !== 0;
}
createMaze();
mazeElement.children[0].classList.add('player');
window.addEventListener('keydown', movePlayer);