-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabyrinth.html
More file actions
173 lines (149 loc) · 5.08 KB
/
Copy pathlabyrinth.html
File metadata and controls
173 lines (149 loc) · 5.08 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Labyrinth Generator</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 20px;
}
canvas {
border: 1px solid #000;
margin-top: 20px;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<div>
<h1>Labyrinth Generator for Kids</h1>
<p>Click "Generate Labyrinth" to create a fun maze. Drag the green dot to the red square!</p>
</div>
<div>
<button id="generate">Generate Labyrinth</button>
</div>
<canvas id="labyrinthCanvas" width="600" height="600"></canvas>
<script>
const canvas = document.getElementById('labyrinthCanvas');
const ctx = canvas.getContext('2d');
const config = {
cellSize: 12,
gridSize: Math.floor(canvas.width / 12),
colors: {
walls: '#000000',
path: '#FFFFFF',
start: 'green',
end: 'red',
player: 'blue'
}
};
let player = { x: 1, y: 1 };
let mazeCells = [];
function generateLabyrinth() {
const { gridSize } = config;
mazeCells = Array.from({ length: gridSize }, () => Array(gridSize).fill(false));
const directions = [
{ dx: -1, dy: 0 }, // top
{ dx: 1, dy: 0 }, // bottom
{ dx: 0, dy: -1 }, // left
{ dx: 0, dy: 1 } // right
];
function isValidCell(x, y) {
return x >= 0 && y >= 0 && x < gridSize && y < gridSize && !mazeCells[x][y];
}
function carve(x, y) {
mazeCells[x][y] = true;
const shuffledDirections = directions.sort(() => Math.random() - 0.5);
shuffledDirections.forEach(({ dx, dy }) => {
const nx = x + dx * 2;
const ny = y + dy * 2;
if (isValidCell(nx, ny)) {
mazeCells[x + dx][y + dy] = true; // Remove wall
carve(nx, ny);
}
});
}
carve(0, 0); // Start carving from the top-left corner
player = { x: 1, y: 1 }; // Reset player position
drawLabyrinth();
}
function drawLabyrinth() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const { cellSize, colors, gridSize } = config;
mazeCells.forEach((row, x) => {
row.forEach((cell, y) => {
ctx.fillStyle = cell ? colors.path : colors.walls;
ctx.fillRect(y * cellSize, x * cellSize, cellSize, cellSize);
});
});
// Draw start and end points
ctx.fillStyle = colors.start;
ctx.fillRect(1, 1, cellSize - 2, cellSize - 2);
ctx.fillStyle = colors.end;
ctx.fillRect(
(gridSize - 1) * cellSize + 1,
(gridSize - 1) * cellSize + 1,
cellSize - 2,
cellSize - 2
);
// Draw player
ctx.fillStyle = colors.player;
ctx.beginPath();
ctx.arc(
player.y * cellSize + cellSize / 2,
player.x * cellSize + cellSize / 2,
cellSize / 3,
0,
2 * Math.PI
);
ctx.fill();
}
function movePlayer(dx, dy) {
const { cellSize, gridSize } = config;
const newX = player.x + dx;
const newY = player.y + dy;
if (
newX >= 0 &&
newY >= 0 &&
newX < gridSize &&
newY < gridSize &&
mazeCells[newX][newY]
) {
player.x = newX;
player.y = newY;
drawLabyrinth();
// Check if player reaches the end
if (newX === gridSize - 1 && newY === gridSize - 1) {
alert('Congratulations! You solved the labyrinth!');
}
}
}
document.addEventListener('keydown', (event) => {
switch (event.key) {
case 'ArrowUp':
movePlayer(-1, 0);
break;
case 'ArrowDown':
movePlayer(1, 0);
break;
case 'ArrowLeft':
movePlayer(0, -1);
break;
case 'ArrowRight':
movePlayer(0, 1);
break;
}
});
document.getElementById('generate').addEventListener('click', generateLabyrinth);
// Generate the first labyrinth on load
generateLabyrinth();
</script>
</body>
</html>