-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.html
130 lines (116 loc) · 4 KB
/
maze.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
<!DOCTYPE html>
<html>
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-3NHFVTRVLZ"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-3NHFVTRVLZ');
</script>
<title>Maze Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #E6F2F8;
}
.maze-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
.maze-cell {
width: 30px;
height: 30px;
background-color: #fff;
border: 1px solid #333;
display: inline-block;
}
.player-cell {
background-color: #FF8C00;
}
.entrance-cell {
background-color: #00FF00;
}
.exit-cell {
background-color: #FF0000;
}
.wall-cell {
background-color: #333;
}
</style>
</head>
<body>
<div class="maze-container">
<h1>Maze Game</h1>
<div id="maze"></div>
</div>
<script>
const mazeElement = document.getElementById('maze');
const maze = [
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],
['#', ' ', '#', ' ', '#', ' ', ' ', ' ', '#', '#', ' ', '#', '#', ' ', ' ', '#'],
['#', ' ', '#', ' ', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', ' ', ' ', ' ', '#', '#'],
['#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', ' ', ' ', ' ', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', ' ', '#', '#', ' ', '#', '#', '#', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', ' ', '#'],
['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],
];
const entrancePosition = { x: 1, y: 1 };
const exitPosition = { x: 14, y: 9 };
let playerPosition = { x: entrancePosition.x, y: entrancePosition.y };
function drawMaze() {
mazeElement.innerHTML = '';
for (let row = 0; row < maze.length; row++) {
for (let col = 0; col < maze[row].length; col++) {
const cell = document.createElement('div');
cell.className = 'maze-cell';
if (row === playerPosition.y && col === playerPosition.x) {
cell.classList.add('player-cell');
} else if (row === entrancePosition.y && col === entrancePosition.x) {
cell.classList.add('entrance-cell');
} else if (row === exitPosition.y && col === exitPosition.x) {
cell.classList.add('exit-cell');
} else if (maze[row][col] === '#') {
cell.classList.add('wall-cell');
}
mazeElement.appendChild(cell);
}
mazeElement.appendChild(document.createElement('br'));
}
}
function movePlayer(dx, dy) {
const newPosX = playerPosition.x + dx;
const newPosY = playerPosition.y + dy;
if (maze[newPosY][newPosX] !== '#') {
playerPosition.x = newPosX;
playerPosition.y = newPosY;
}
drawMaze();
if (playerPosition.x === exitPosition.x && playerPosition.y === exitPosition.y) {
alert('Congratulations! You reached the exit!');
}
}
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowUp') {
movePlayer(0, -1);
} else if (event.key === 'ArrowDown') {
movePlayer(0, 1);
} else if (event.key === 'ArrowLeft') {
movePlayer(-1, 0);
} else if (event.key === 'ArrowRight') {
movePlayer(1, 0);
}
});
drawMaze();
</script>
</body>
</html>