This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazegame.html
More file actions
285 lines (263 loc) · 12.1 KB
/
Copy pathmazegame.html
File metadata and controls
285 lines (263 loc) · 12.1 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Maze Game v2</title>
<link rel="icon" type="image/x-icon" href="https://usrx215.github.io/scgames/mazegame.png">
<style>
:root { --bg:#0b0e14; --panel:#11151f; --ink:#e6e6e6; --muted:#9aa4b2; --accent:#6ee7ff; --accent2:#a7f3d0; }
* { box-sizing: border-box; }
body { margin:0; font-family: system-ui, -apple-system, Segoe UI, Ubuntu, Cantarell, Roboto, "Noto Sans", Arial, sans-serif; background:var(--bg); color:var(--ink); display:grid; min-height:100svh; grid-template-rows:auto 1fr; }
header { padding:12px 16px; background:linear-gradient(180deg, #0f1422, #0b0e14); border-bottom:1px solid #1d2433; display:flex; gap:12px; align-items:center; flex-wrap:wrap; }
header h1 { font-size:16px; margin:0; letter-spacing:0.3px; font-weight:700; }
.controls { display:flex; gap:10px; align-items:center; flex-wrap:wrap; }
.controls label { font-size:12px; color:var(--muted); }
.controls input[type="number"]{ width:72px; }
.controls input, .controls button { background:#0f1422; color:var(--ink); border:1px solid #263046; border-radius:10px; padding:6px 10px; font-size:12px; }
.controls button { cursor:pointer; transition: transform .05s ease; }
.controls button:active { transform: translateY(1px); }
.controls .hint { color:var(--muted); font-size:12px; margin-left:6px; }
main { display:grid; place-items:center; padding:12px; position:relative; }
canvas { background:#05070c; border-radius:12px; box-shadow: 0 10px 40px #00000066, inset 0 0 0 1px #1d2433; image-rendering: pixelated; }
#minimap { position:absolute; top:20px; right:20px; background:#000; border:1px solid #444; border-radius:8px; image-rendering: pixelated; }
footer { text-align:center; color:var(--muted); font-size:12px; padding:8px 0 12px; }
.kbd { padding:1px 6px; border:1px solid #32415f; border-bottom-width:2px; border-radius:6px; background:#0f1422; color:#c9d2e1; font-family:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; font-size:12px; }
</style>
</head>
<body>
<header>
<h1>Maze Game v2</h1>
<div class="controls">
<label>Width (cells)
<input id="w" type="number" min="5" step="2" value="41" />
</label>
<label>Height (cells)
<input id="h" type="number" min="5" step="2" value="41" />
</label>
<label>Cell px
<input id="cell" type="number" min="4" max="64" value="16" />
</label>
<label>Seed
<input id="seed" type="text" placeholder="random" />
</label>
<label><input id="showPath" type="checkbox" /> Show solution</label>
<button id="regen">Regenerate</button>
<button id="reset">Reset Player</button>
<button id="showSeed">Show Seed</button>
<span class="hint">Controls: <span class="kbd">WASD</span>/<span class="kbd">↑←↓→</span>, <span class="kbd">R</span> regen</span>
</div>
</header>
<main>
<canvas id="maze"></canvas>
<canvas id="minimap" width="160" height="160"></canvas>
</main>
<footer>
Perfect maze — camera locked to player, minimap reveals visited tiles.
</footer>
<script>
// PRNG (seedable)
function xmur3(str){ let h=1779033703^str.length; for(let i=0;i<str.length;i++){h=Math.imul(h^str.charCodeAt(i),3432918353); h=h<<13|h>>>19;} return ()=>{h=Math.imul(h^h>>>16,2246822507); h=Math.imul(h^h>>>13,3266489909); return (h^h>>>16)>>>0;}; }
function mulberry32(a){ return function(){ let t=a+=0x6D2B79F5; t=Math.imul(t^t>>>15,t|1); t^=t+Math.imul(t^t>>>7,t|61); return ((t^t>>>14)>>>0)/4294967296; } }
function makeRng(seedStr){ if(!seedStr) return Math.random; const seedFn=xmur3(seedStr); return mulberry32(seedFn()); }
const canvas = document.getElementById('maze');
const ctx = canvas.getContext('2d');
const minimap = document.getElementById('minimap');
const mctx = minimap.getContext('2d');
let grid, W, H, CELL, rng, player, goal, visited;
const VIEW = 21; // visible tile window (odd preferred)
let currentSeed = null;
function initGrid(widthCells, heightCells){
W = widthCells|0; H = heightCells|0; if(W%2===0) W++; if(H%2===0) H++;
grid = Array.from({length:H}, ()=>Array(W).fill(1));
visited = Array.from({length:H}, ()=>Array(W).fill(false));
}
function shuffle(arr){ for(let i=arr.length-1;i>0;i--){const j=(rng()*(i+1))|0; [arr[i],arr[j]]=[arr[j],arr[i]];} return arr; }
function carveMaze(){
const stack = [[1,1]]; grid[1][1] = 0;
while(stack.length){
const [cx,cy] = stack[stack.length-1];
const dirs = shuffle([[0,-2],[0,2],[2,0],[-2,0]]);
let carved = false;
for(const [dx,dy] of dirs){
const nx = cx+dx, ny = cy+dy;
if(ny>0 && ny<H-1 && nx>0 && nx<W-1 && grid[ny][nx]===1){
grid[cy+dy/2][cx+dx/2] = 0; grid[ny][nx] = 0;
stack.push([nx,ny]); carved = true; break;
}
}
if(!carved) stack.pop();
}
player = {x:1,y:1};
goal = farthestFrom(player);
player = farthestFrom(goal);
}
function bfsDistances(sx,sy){
const dist = Array.from({length:H}, ()=>Array(W).fill(Infinity));
const q = [[sx,sy]]; dist[sy][sx]=0;
const moves = [[1,0],[-1,0],[0,1],[0,-1]];
while(q.length){
const [x,y] = q.shift();
for(const [dx,dy] of moves){
const nx = x+dx, ny = y+dy;
if(nx>=0 && ny>=0 && nx<W && ny<H && grid[ny][nx]===0 && dist[ny][nx]===Infinity){
dist[ny][nx] = dist[y][x] + 1; q.push([nx,ny]);
}
}
}
return dist;
}
function farthestFrom(start){
const dist = bfsDistances(start.x, start.y);
let max = -1, pos = {x:start.x,y:start.y};
for(let y=1;y<H;y+=2){ for(let x=1;x<W;x+=2){ if(dist[y][x]!==Infinity && dist[y][x] > max){ max = dist[y][x]; pos = {x,y}; } } }
return pos;
}
function shortestPath(from, to){
const dist = bfsDistances(from.x, from.y);
if(dist[to.y][to.x]===Infinity) return [];
const path = [[to.x,to.y]];
const moves = [[1,0],[-1,0],[0,1],[0,-1]];
let [x,y] = [to.x,to.y];
while(x!==from.x || y!==from.y){
let best = null, bestd = Infinity;
for(const [dx,dy] of moves){
const nx = x+dx, ny = y+dy;
if(nx>=0 && ny>=0 && nx<W && ny<H && dist[ny][nx] < bestd){ bestd = dist[ny][nx]; best = [nx,ny]; }
}
if(!best) break;
[x,y] = best; path.push([x,y]);
}
return path.reverse();
}
function draw(){
canvas.width = VIEW * CELL;
canvas.height = VIEW * CELL;
ctx.fillStyle = '#03060b'; ctx.fillRect(0,0,canvas.width,canvas.height);
const half = VIEW >> 1;
for(let dy=-half; dy<=half; dy++){
for(let dx=-half; dx<=half; dx++){
const worldX = player.x + dx;
const worldY = player.y + dy;
const sx = (dx + half) * CELL;
const sy = (dy + half) * CELL;
if(worldX >= 0 && worldY >= 0 && worldX < W && worldY < H){
if(grid[worldY][worldX] === 1){ ctx.fillStyle = '#101826'; ctx.fillRect(sx, sy, CELL, CELL); ctx.fillStyle='rgba(255,255,255,0.04)'; ctx.fillRect(sx, sy, CELL, Math.max(1, CELL*0.12)); }
else { ctx.fillStyle = '#0c1220'; ctx.fillRect(sx, sy, CELL, CELL); }
} else { ctx.fillStyle = '#0b0f16'; ctx.fillRect(sx, sy, CELL, CELL); }
}
}
if(document.getElementById('showPath').checked){
const path = shortestPath(player, goal);
ctx.globalAlpha = 0.95;
for(const [px,py] of path){
const dx = px - player.x; const dy = py - player.y;
if(Math.abs(dx) <= half && Math.abs(dy) <= half){
const sx = (dx + half) * CELL + CELL*0.25;
const sy = (dy + half) * CELL + CELL*0.25;
ctx.fillStyle = '#6ee7ff'; ctx.fillRect(sx, sy, CELL*0.5, CELL*0.5);
}
}
ctx.globalAlpha = 1;
}
const gdx = goal.x - player.x, gdy = goal.y - player.y;
if(Math.abs(gdx) <= half && Math.abs(gdy) <= half){
const gsx = (gdx + half) * CELL + CELL*0.15;
const gsy = (gdy + half) * CELL + CELL*0.15;
ctx.fillStyle = '#a7f3d0'; ctx.fillRect(gsx, gsy, CELL*0.7, CELL*0.7);
}
const center = half * CELL;
ctx.fillStyle = '#e5e7eb'; ctx.fillRect(center + CELL*0.2, center + CELL*0.2, CELL*0.6, CELL*0.6);
visited[player.y][player.x] = true;
drawMinimap();
}
function drawMinimap(){
const mw = minimap.width, mh = minimap.height;
const scaleX = mw / W, scaleY = mh / H;
mctx.fillStyle = '#000'; mctx.fillRect(0,0,mw,mh);
for(let y=0;y<H;y++){
for(let x=0;x<W;x++){
if(!visited[y][x]) continue;
mctx.fillStyle = grid[y][x]===0 ? '#0c1220' : '#101826';
mctx.fillRect(x*scaleX, y*scaleY, scaleX, scaleY);
}
}
mctx.fillStyle = '#e5e7eb'; mctx.fillRect(player.x*scaleX, player.y*scaleY, scaleX, scaleY);
mctx.fillStyle = '#a7f3d0'; mctx.fillRect(goal.x*scaleX, goal.y*scaleY, scaleX, scaleY);
}
function regenerate(){
const w = Math.max(5, parseInt(document.getElementById('w').value,10) || 31);
const h = Math.max(5, parseInt(document.getElementById('h').value,10) || 31);
CELL = Math.max(4, Math.min(64, parseInt(document.getElementById('cell').value,10) || 16));
const seedStr = document.getElementById('seed').value.trim();
currentSeed = seedStr || (Math.random() + '').slice(2);
rng = makeRng(seedStr || currentSeed);
initGrid(w,h);
carveMaze();
visited = Array.from({length:H}, ()=>Array(W).fill(false));
visited[player.y][player.x] = true;
canvas.width = VIEW * CELL; canvas.height = VIEW * CELL;
draw();
}
function tryMove(dx,dy){
const nx = player.x + dx, ny = player.y + dy;
if(nx<0||ny<0||nx>=W||ny>=H) return;
if(grid[ny][nx]===0){ player.x = nx; player.y = ny; draw(); checkWin(); }
}
function checkWin(){ if(player.x===goal.x && player.y===goal.y) flashWin(); }
function flashWin(){
let t=0;
function step(){
draw();
const half = VIEW>>1;
const cx = half*CELL + CELL*0.5, cy = cx;
ctx.save();
ctx.globalAlpha = Math.max(0,1.2 - t);
ctx.strokeStyle = '#6ee7ff';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(cx, cy, CELL*1.2*t, 0, Math.PI*2);
ctx.stroke();
ctx.restore();
t+=0.06;
if(t<1.2) requestAnimationFrame(step);
else {
document.getElementById('seed').value = '';
currentSeed = (Math.random() + '').slice(2);
rng = makeRng(currentSeed);
regenerate();
}
}
step();
}
// movement
window.addEventListener('keydown', (e)=>{
const k = e.key.toLowerCase();
if(k==='arrowup' || k==='w') tryMove(0,-1);
else if(k==='arrowdown' || k==='s') tryMove(0,1);
else if(k==='arrowleft' || k==='a') tryMove(-1,0);
else if(k==='arrowright' || k==='d') tryMove(1,0);
else if(k==='r') regenerate();
});
document.getElementById('regen').addEventListener('click', regenerate);
document.getElementById('reset').addEventListener('click', ()=>{ player = {x:1,y:1}; visited = Array.from({length:H}, ()=>Array(W).fill(false)); visited[player.y][player.x]=true; draw();});
document.getElementById('showPath').addEventListener('change', draw);
document.getElementById('showSeed').addEventListener('click', () => {
document.getElementById('seed').value = currentSeed;
});
(function start(){
document.getElementById('w').value = 41;
document.getElementById('h').value = 41;
document.getElementById('cell').value = 16;
currentSeed = (Math.random() + '').slice(2);
rng = makeRng(currentSeed);
initGrid(41,41);
carveMaze();
CELL = parseInt(document.getElementById('cell').value,10) || 16;
canvas.width = VIEW * CELL; canvas.height = VIEW * CELL;
visited[player.y][player.x] = true;
draw();
})();
</script>
</body>
</html>