Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
432 lines (380 loc) · 10.9 KB
/
Copy pathscript.js
File metadata and controls
432 lines (380 loc) · 10.9 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
const canvas = document.getElementById('gameBoard');
const ctx = canvas.getContext('2d');
const nextCanvas = document.getElementById('nextPiece');
const nextCtx = nextCanvas.getContext('2d');
const BOARD_WIDTH = 10;
const BOARD_HEIGHT = 20;
const CELL_SIZE = 30;
// Tetromino shapes
const SHAPES = {
I: [
[1, 1, 1, 1]
],
O: [
[1, 1],
[1, 1]
],
T: [
[0, 1, 0],
[1, 1, 1]
],
S: [
[0, 1, 1],
[1, 1, 0]
],
Z: [
[1, 1, 0],
[0, 1, 1]
],
J: [
[1, 0, 0],
[1, 1, 1]
],
L: [
[0, 0, 1],
[1, 1, 1]
]
};
const COLORS = {
I: '#00f0f0',
O: '#f0f000',
T: '#a000f0',
S: '#00f000',
Z: '#f00000',
J: '#0000f0',
L: '#f0a000'
};
let board = [];
let currentPiece = null;
let nextPiece = null;
let score = 0;
let level = 1;
let lines = 0;
let dropTime = 0;
let dropInterval = 1000;
let gameRunning = true;
let paused = false;
// Touch control variables
let touchStartTime = 0;
let touchRepeatTimer = null;
function initBoard() {
board = [];
for (let row = 0; row < BOARD_HEIGHT; row++) {
board[row] = new Array(BOARD_WIDTH).fill(0);
}
}
function createPiece() {
const shapes = Object.keys(SHAPES);
const randomShape = shapes[Math.floor(Math.random() * shapes.length)];
return {
shape: SHAPES[randomShape],
color: COLORS[randomShape],
x: Math.floor(BOARD_WIDTH / 2) - Math.floor(SHAPES[randomShape][0].length / 2),
y: 0,
type: randomShape
};
}
function drawCell(ctx, x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
ctx.strokeRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let row = 0; row < BOARD_HEIGHT; row++) {
for (let col = 0; col < BOARD_WIDTH; col++) {
if (board[row][col]) {
drawCell(ctx, col, row, board[row][col]);
}
}
}
}
function drawPiece(piece, context = ctx, offsetX = 0, offsetY = 0) {
for (let row = 0; row < piece.shape.length; row++) {
for (let col = 0; col < piece.shape[row].length; col++) {
if (piece.shape[row][col]) {
if (context === ctx) {
drawCell(context, piece.x + col + offsetX, piece.y + row + offsetY, piece.color);
} else {
// For next piece preview
context.fillStyle = piece.color;
context.fillRect(
(col + offsetX) * 20 + 10,
(row + offsetY) * 20 + 10,
18, 18
);
}
}
}
}
}
function drawNext() {
nextCtx.clearRect(0, 0, nextCanvas.width, nextCanvas.height);
if (nextPiece) {
drawPiece(nextPiece, nextCtx, 0, 0);
}
}
function isValidPosition(piece, dx = 0, dy = 0, newShape = null) {
const shape = newShape || piece.shape;
const newX = piece.x + dx;
const newY = piece.y + dy;
for (let row = 0; row < shape.length; row++) {
for (let col = 0; col < shape[row].length; col++) {
if (shape[row][col]) {
const x = newX + col;
const y = newY + row;
if (x < 0 || x >= BOARD_WIDTH || y >= BOARD_HEIGHT ||
(y >= 0 && board[y][x])) {
return false;
}
}
}
}
return true;
}
function placePiece() {
for (let row = 0; row < currentPiece.shape.length; row++) {
for (let col = 0; col < currentPiece.shape[row].length; col++) {
if (currentPiece.shape[row][col]) {
const x = currentPiece.x + col;
const y = currentPiece.y + row;
if (y >= 0) {
board[y][x] = currentPiece.color;
}
}
}
}
// Check for game over
if (currentPiece.y <= 0) {
gameOver();
return;
}
clearLines();
currentPiece = nextPiece;
nextPiece = createPiece();
}
function clearLines() {
let linesCleared = 0;
for (let row = BOARD_HEIGHT - 1; row >= 0; row--) {
if (board[row].every(cell => cell !== 0)) {
board.splice(row, 1);
board.unshift(new Array(BOARD_WIDTH).fill(0));
linesCleared++;
row++; // Check the same row again
}
}
if (linesCleared > 0) {
lines += linesCleared;
score += linesCleared * 100 * level;
level = Math.floor(lines / 10) + 1;
dropInterval = Math.max(100, 1000 - (level - 1) * 50);
updateDisplay();
}
}
function rotatePiece() {
const rotated = currentPiece.shape[0].map((_, i) =>
currentPiece.shape.map(row => row[i]).reverse()
);
if (isValidPosition(currentPiece, 0, 0, rotated)) {
currentPiece.shape = rotated;
}
}
function hardDrop() {
while (isValidPosition(currentPiece, 0, 1)) {
currentPiece.y++;
score += 2;
}
placePiece();
}
function moveLeft() {
if (gameRunning && !paused && isValidPosition(currentPiece, -1, 0)) {
currentPiece.x--;
}
}
function moveRight() {
if (gameRunning && !paused && isValidPosition(currentPiece, 1, 0)) {
currentPiece.x++;
}
}
function moveDown() {
if (gameRunning && !paused && isValidPosition(currentPiece, 0, 1)) {
currentPiece.y++;
score++;
updateDisplay();
}
}
function togglePause() {
if (gameRunning) {
paused = !paused;
const pauseBtn = document.getElementById('pauseBtn');
pauseBtn.textContent = paused ? '▶' : '⏸';
}
}
function updateDisplay() {
document.getElementById('score').textContent = score;
document.getElementById('level').textContent = level;
document.getElementById('lines').textContent = lines;
}
function gameOver() {
gameRunning = false;
document.getElementById('finalScore').textContent = `Final Score: ${score}`;
document.getElementById('gameOver').style.display = 'block';
}
function restartGame() {
initBoard();
currentPiece = createPiece();
nextPiece = createPiece();
score = 0;
level = 1;
lines = 0;
dropInterval = 1000;
gameRunning = true;
paused = false;
document.getElementById('gameOver').style.display = 'none';
document.getElementById('pauseBtn').textContent = '⏸';
updateDisplay();
}
function gameLoop(time) {
if (!gameRunning || paused) {
requestAnimationFrame(gameLoop);
return;
}
if (time - dropTime > dropInterval) {
if (isValidPosition(currentPiece, 0, 1)) {
currentPiece.y++;
} else {
placePiece();
}
dropTime = time;
}
drawBoard();
drawPiece(currentPiece);
drawNext();
requestAnimationFrame(gameLoop);
}
// Desktop keyboard controls
document.addEventListener('keydown', (e) => {
if (!gameRunning) return;
switch (e.code) {
case 'ArrowLeft':
moveLeft();
break;
case 'ArrowRight':
moveRight();
break;
case 'ArrowDown':
moveDown();
break;
case 'ArrowUp':
if (!paused) rotatePiece();
break;
case 'Space':
e.preventDefault();
if (!paused) {
hardDrop();
updateDisplay();
}
break;
case 'KeyP':
togglePause();
break;
}
});
// Touch controls with repeat functionality
function setupTouchControls() {
const buttons = {
leftBtn: moveLeft,
rightBtn: moveRight,
downBtn: moveDown,
rotateBtn: rotatePiece,
hardDropBtn: () => {
if (!paused) {
hardDrop();
updateDisplay();
}
},
pauseBtn: togglePause
};
Object.keys(buttons).forEach(btnId => {
const btn = document.getElementById(btnId);
const action = buttons[btnId];
// For movement buttons, enable repeat on hold
const isMovementBtn = ['leftBtn', 'rightBtn', 'downBtn'].includes(btnId);
btn.addEventListener('touchstart', (e) => {
e.preventDefault();
action();
if (isMovementBtn) {
touchStartTime = Date.now();
touchRepeatTimer = setTimeout(() => {
const repeatInterval = setInterval(() => {
if (gameRunning && !paused) {
action();
}
}, 100);
btn.repeatInterval = repeatInterval;
}, 300); // Initial delay before repeat
}
});
btn.addEventListener('touchend', (e) => {
e.preventDefault();
if (touchRepeatTimer) {
clearTimeout(touchRepeatTimer);
touchRepeatTimer = null;
}
if (btn.repeatInterval) {
clearInterval(btn.repeatInterval);
btn.repeatInterval = null;
}
});
// Also handle mouse events for desktop
btn.addEventListener('mousedown', (e) => {
e.preventDefault();
action();
if (isMovementBtn) {
touchStartTime = Date.now();
touchRepeatTimer = setTimeout(() => {
const repeatInterval = setInterval(() => {
if (gameRunning && !paused) {
action();
}
}, 100);
btn.repeatInterval = repeatInterval;
}, 300);
}
});
btn.addEventListener('mouseup', (e) => {
e.preventDefault();
if (touchRepeatTimer) {
clearTimeout(touchRepeatTimer);
touchRepeatTimer = null;
}
if (btn.repeatInterval) {
clearInterval(btn.repeatInterval);
btn.repeatInterval = null;
}
});
btn.addEventListener('mouseleave', (e) => {
if (touchRepeatTimer) {
clearTimeout(touchRepeatTimer);
touchRepeatTimer = null;
}
if (btn.repeatInterval) {
clearInterval(btn.repeatInterval);
btn.repeatInterval = null;
}
});
});
}
// Prevent context menu on touch
document.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
// Initialize game
initBoard();
currentPiece = createPiece();
nextPiece = createPiece();
updateDisplay();
setupTouchControls();
requestAnimationFrame(gameLoop);