-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze-hunter.html
156 lines (145 loc) · 5.02 KB
/
maze-hunter.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Maze Runner</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: white;
margin: 0;
padding: 0;
}
canvas {
border: 4px solid white;
display: block;
margin: 20px auto;
max-width: 100%;
height: auto;
}
.controls {
margin-top: 10px;
}
.btn {
background: #ff9800;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
font-size: 16px;
margin: 5px;
border-radius: 5px;
}
.touch-controls {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 10px;
}
.touch-row {
display: flex;
}
.touch-btn {
width: 60px;
height: 60px;
margin: 5px;
background: #ff9800;
color: white;
font-size: 24px;
border: none;
border-radius: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Maze Runner</h1>
<p>Navigate through the maze to reach the exit!</p>
<canvas id="mazeCanvas" width="375" height="375"></canvas>
<div class="controls">
<button class="btn" onclick="generateMaze()">New Maze</button>
</div>
<div class="touch-controls">
<button class="touch-btn" onclick="movePlayer(0, -1)">▲</button>
<div class="touch-row">
<button class="touch-btn" onclick="movePlayer(-1, 0)">◀</button>
<button class="touch-btn" onclick="movePlayer(1, 0)">▶</button>
</div>
<button class="touch-btn" onclick="movePlayer(0, 1)">▼</button>
</div>
<script>
const canvas = document.getElementById("mazeCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 21;
let player = { x: 1, y: 1 };
let exit = { x: gridSize - 2, y: gridSize - 2 };
let maze = [];
let cellSize;
function resizeCanvas() {
canvas.width = Math.min(window.innerWidth - 40, 400);
canvas.height = canvas.width;
cellSize = canvas.width / gridSize;
drawMaze();
}
function generateMaze() {
maze = Array.from({ length: gridSize }, () => Array(gridSize).fill(1));
function carvePassages(x, y) {
const directions = [
[0, -1], [0, 1], [-1, 0], [1, 0]
].sort(() => Math.random() - 0.5);
for (const [dx, dy] of directions) {
const nx = x + dx * 2;
const ny = y + dy * 2;
if (nx > 0 && ny > 0 && nx < gridSize - 1 && ny < gridSize - 1 && maze[ny][nx] === 1) {
maze[y + dy][x + dx] = 0;
maze[ny][nx] = 0;
carvePassages(nx, ny);
}
}
}
maze[1][1] = 0;
carvePassages(1, 1);
maze[exit.y][exit.x] = 0;
player = { x: 1, y: 1 };
resizeCanvas();
}
function drawMaze() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
ctx.fillStyle = maze[y][x] === 1 ? "#000" : "#fff";
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
ctx.fillStyle = "blue";
ctx.fillRect(player.x * cellSize, player.y * cellSize, cellSize, cellSize);
ctx.fillStyle = "green";
ctx.fillRect(exit.x * cellSize, exit.y * cellSize, cellSize, cellSize);
}
function movePlayer(dx, dy) {
let nx = player.x + dx;
let ny = player.y + dy;
if (nx >= 0 && ny >= 0 && nx < gridSize && ny < gridSize && maze[ny][nx] === 0) {
player.x = nx;
player.y = ny;
drawMaze();
if (nx === exit.x && ny === exit.y) {
setTimeout(() => alert("You Win!"), 100);
generateMaze();
}
}
}
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowUp") movePlayer(0, -1);
else if (e.key === "ArrowDown") movePlayer(0, 1);
else if (e.key === "ArrowLeft") movePlayer(-1, 0);
else if (e.key === "ArrowRight") movePlayer(1, 0);
});
window.addEventListener("resize", resizeCanvas);
generateMaze();
</script>
</body>
</html>