-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
maze.html
213 lines (180 loc) · 6.33 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reverse Scrolling Maze Game</title>
<style>
body {
background-color: rgba(20, 108, 21, 0.982);
animation: blink 1s steps 0s infinite normal;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
/* Added a default font */
}
h1 {
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
font-size: 4vw;
/* Responsive font size */
text-align: center;
margin-top: 40px;
}
h2 {
font-size: 3vw;
/* Responsive font size for h2 */
text-align: center;
margin: 20px 0;
/* Added margin */
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.blink {
animation: blink 1s infinite;
}
#maze-section {
margin: 0 auto;
/* Centered the maze section */
width: 90%;
/* Responsive width */
max-width: 800px;
/* Max width for larger screens */
}
canvas {
border: 6px solid black;
width: 100%;
/* Full width */
height: auto;
/* Maintain aspect ratio */
}
.hint {
color: yellow;
text-align: center;
/* Centered hint text */
font-size: 1.5em;
/* Responsive hint font size */
margin: 20px 0;
/* Added margin */
}
@media (min-width: 768px) {
h1 {
font-size: 5vw;
/* Adjust font size for larger screens */
}
h2 {
font-size: 3vw;
/* Adjust h2 font size for larger screens */
}
.hint {
font-size: 1.5em;
/* Keep hint size for larger screens */
}
}
</style>
</head>
<body>
<!-- Main content sections -->
<section>
<h1>Scroll down to discover something fun...</h1>
</section>
<br><br><br><br><br><br><br><br>
<section style="margin-top: 20px;"></section>
<br><br>
<section id="maze-section" class="hidden">
<h2>Reverse Maze Game</h2>
<canvas id="mazeCanvas" width="600" height="600"></canvas>
</section>
<section class="hint">
<p>HINT: Use arrow keys to navigate the maze (controls are reversed).</p>
</section>
<script>
window.addEventListener('scroll', () => {
const mazeSection = document.getElementById('maze-section');
const viewportHeight = window.innerHeight;
const mazePosition = mazeSection.getBoundingClientRect();
// Check if the maze section is within the viewport
if (mazePosition.top < viewportHeight && mazePosition.bottom >= 0) {
mazeSection.classList.remove('hidden'); // Show the maze section
if (!mazeInitialized) {
initMazeGame(); // Initialize maze once when section is visible
}
}
});
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
const mazeSize = 10; // Size of the maze grid (10x10)
const cellSize = canvas.width / mazeSize;
let playerX = 0, playerY = 0; // Initial player position
let maze = []; // Maze grid
let mazeInitialized = false;
// Initialize the maze game
function initMazeGame() {
generateMaze();
drawMaze();
window.addEventListener('keydown', handleMovement);
mazeInitialized = true;
}
// Generate a random maze
function generateMaze() {
maze = [];
for (let i = 0; i < mazeSize; i++) {
const row = [];
for (let j = 0; j < mazeSize; j++) {
row.push(Math.random() > 0.7 ? 1 : 0); // 1 for wall, 0 for free space
}
maze.push(row);
}
maze[0][0] = 0; // Start position
maze[mazeSize - 1][mazeSize - 1] = 0; // Goal position
}
// Draw the maze on the canvas
function drawMaze() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear canvas
for (let i = 0; i < mazeSize; i++) {
for (let j = 0; j < mazeSize; j++) {
if (maze[i][j] === 1) {
ctx.fillStyle = '#333'; // Draw wall
} else {
ctx.fillStyle = '#fff'; // Draw path
}
ctx.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
}
}
drawPlayer();
}
// Draw the player
function drawPlayer() {
ctx.fillStyle = 'blue';
ctx.fillRect(playerX * cellSize, playerY * cellSize, cellSize, cellSize);
}
// Handle reversed movement controls
function handleMovement(event) {
const key = event.key;
if (key === 'ArrowUp' && playerY < mazeSize - 1 && maze[playerY + 1][playerX] === 0) {
playerY++; // Move down (reversed)
} else if (key === 'ArrowDown' && playerY > 0 && maze[playerY - 1][playerX] === 0) {
playerY--; // Move up (reversed)
} else if (key === 'ArrowLeft' && playerX < mazeSize - 1 && maze[playerY][playerX + 1] === 0) {
playerX++; // Move right (reversed)
} else if (key === 'ArrowRight' && playerX > 0 && maze[playerY][playerX - 1] === 0) {
playerX--; // Move left (reversed)
}
drawMaze();
// Check if the player has reached the goal
if (playerX === mazeSize - 1 && playerY === mazeSize - 1) {
alert('Congratulations! You reached the goal.');
window.removeEventListener('keydown', handleMovement);
}
}
</script>
</body>
</html>