-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
469 lines (409 loc) · 13 KB
/
game.js
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score-value');
const nextPieceCanvas = document.getElementById('next-piece-canvas');
const nextPieceCtx = nextPieceCanvas.getContext('2d');
const linesElement = document.getElementById('lines');
const levelElement = document.getElementById('level');
const BLOCK_SIZE = 30;
const BOARD_WIDTH = 10;
const BOARD_HEIGHT = 20;
let score = 0;
let level = 1;
let lines = 0;
let nextPiece = null;
let gameSpeed = 1000;
// Tetromino shapes
const SHAPES = [
[[1, 1, 1, 1]], // I
[[1, 1], [1, 1]], // O
[[1, 1, 1], [0, 1, 0]], // T
[[1, 1, 1], [1, 0, 0]], // L
[[1, 1, 1], [0, 0, 1]], // J
[[1, 1, 0], [0, 1, 1]], // S
[[0, 1, 1], [1, 1, 0]], // Z
];
const COLORS = [
'#00f0f0', // cyan
'#f0f000', // yellow
'#a000f0', // purple
'#f0a000', // orange
'#0000f0', // blue
'#00f000', // green
'#f00000', // red
];
let board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
let currentPiece = null;
let currentPieceX = 0;
let currentPieceY = 0;
let currentPieceColor = '';
const PARTICLES = [];
const GLOW_INTENSITY = 20;
const MAX_PARTICLES = 50;
class Piece {
constructor(shape, color) {
this.shape = shape;
this.color = color;
}
}
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.size = Math.random() * 3 + 2;
this.speedX = (Math.random() - 0.5) * 4;
this.speedY = -Math.random() * 4 - 2;
this.gravity = 0.1;
this.life = 1.0;
this.fadeSpeed = 0.02;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.speedY += this.gravity;
this.life -= this.fadeSpeed;
}
draw(ctx) {
ctx.save();
ctx.globalAlpha = this.life;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
function createParticles(x, y, color, count = 10) {
for (let i = 0; i < count; i++) {
if (PARTICLES.length < MAX_PARTICLES) {
PARTICLES.push(new Particle(x, y, color));
}
}
}
function createNewPiece() {
if (nextPiece === null) {
const randomIndex = Math.floor(Math.random() * SHAPES.length);
nextPiece = new Piece(SHAPES[randomIndex], COLORS[randomIndex]);
}
currentPiece = nextPiece;
// Reset position for new piece
currentPieceX = Math.floor(BOARD_WIDTH / 2) - Math.floor(currentPiece.shape[0].length / 2);
currentPieceY = 0;
// Create next piece
const randomIndex = Math.floor(Math.random() * SHAPES.length);
nextPiece = new Piece(SHAPES[randomIndex], COLORS[randomIndex]);
// Draw next piece preview
drawNextPiece();
}
function drawNextPiece() {
if (!nextPiece) return;
nextPieceCtx.fillStyle = '#1a1a1a';
nextPieceCtx.fillRect(0, 0, nextPieceCanvas.width, nextPieceCanvas.height);
const blockSize = 20;
const offsetX = (nextPieceCanvas.width - nextPiece.shape[0].length * blockSize) / 2;
const offsetY = (nextPieceCanvas.height - nextPiece.shape.length * blockSize) / 2;
for (let y = 0; y < nextPiece.shape.length; y++) {
for (let x = 0; x < nextPiece.shape[y].length; x++) {
if (nextPiece.shape[y][x]) {
// Draw block
nextPieceCtx.fillStyle = nextPiece.color;
nextPieceCtx.fillRect(
offsetX + x * blockSize + 1,
offsetY + y * blockSize + 1,
blockSize - 2,
blockSize - 2
);
// Add 3D effect
nextPieceCtx.fillStyle = 'rgba(255, 255, 255, 0.3)';
nextPieceCtx.fillRect(
offsetX + x * blockSize + 1,
offsetY + y * blockSize + 1,
blockSize - 2,
3
);
}
}
}
}
function draw() {
// Clear canvas with a dark background
ctx.fillStyle = '#1a1a1a';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw grid with subtle glow
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
for (let x = 0; x < BOARD_WIDTH; x++) {
for (let y = 0; y < BOARD_HEIGHT; y++) {
ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
// Draw board with glow effect
for (let y = 0; y < BOARD_HEIGHT; y++) {
for (let x = 0; x < BOARD_WIDTH; x++) {
if (board[y][x]) {
const color = board[y][x];
// Draw glow
ctx.shadowColor = color;
ctx.shadowBlur = GLOW_INTENSITY;
// Draw block
ctx.fillStyle = color;
ctx.fillRect(
x * BLOCK_SIZE + 1,
y * BLOCK_SIZE + 1,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2
);
// Add shine effect
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.fillRect(
x * BLOCK_SIZE + 1,
y * BLOCK_SIZE + 1,
BLOCK_SIZE - 2,
BLOCK_SIZE / 3
);
}
}
}
// Draw current piece with glow
if (currentPiece) {
ctx.shadowColor = currentPiece.color;
ctx.shadowBlur = GLOW_INTENSITY;
ctx.fillStyle = currentPiece.color;
for (let y = 0; y < currentPiece.shape.length; y++) {
for (let x = 0; x < currentPiece.shape[y].length; x++) {
if (currentPiece.shape[y][x]) {
ctx.fillRect(
(currentPieceX + x) * BLOCK_SIZE + 1,
(currentPieceY + y) * BLOCK_SIZE + 1,
BLOCK_SIZE - 2,
BLOCK_SIZE - 2
);
// Add shine effect
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.fillRect(
(currentPieceX + x) * BLOCK_SIZE + 1,
(currentPieceY + y) * BLOCK_SIZE + 1,
BLOCK_SIZE - 2,
BLOCK_SIZE / 3
);
}
}
}
}
// Reset shadow
ctx.shadowBlur = 0;
// Update and draw particles
for (let i = PARTICLES.length - 1; i >= 0; i--) {
PARTICLES[i].update();
PARTICLES[i].draw(ctx);
if (PARTICLES[i].life <= 0) {
PARTICLES.splice(i, 1);
}
}
}
function isValidMove(pieceX, pieceY, piece = currentPiece.shape) {
for (let y = 0; y < piece.length; y++) {
for (let x = 0; x < piece[y].length; x++) {
if (piece[y][x]) {
const newX = pieceX + x;
const newY = pieceY + y;
if (newX < 0 || newX >= BOARD_WIDTH || newY >= BOARD_HEIGHT) {
return false;
}
if (newY >= 0 && board[newY][newX]) {
return false;
}
}
}
}
return true;
}
function mergePiece() {
for (let y = 0; y < currentPiece.shape.length; y++) {
for (let x = 0; x < currentPiece.shape[y].length; x++) {
if (currentPiece.shape[y][x]) {
board[currentPieceY + y][currentPieceX + x] = currentPiece.color;
}
}
}
}
function clearLines() {
let linesCleared = 0;
for (let y = BOARD_HEIGHT - 1; y >= 0; y--) {
if (board[y].every(cell => cell !== 0)) {
// Create particles for the cleared line
for (let x = 0; x < BOARD_WIDTH; x++) {
createParticles(
x * BLOCK_SIZE + BLOCK_SIZE / 2,
y * BLOCK_SIZE + BLOCK_SIZE / 2,
board[y][x],
3
);
}
board.splice(y, 1);
board.unshift(Array(BOARD_WIDTH).fill(0));
linesCleared++;
lines++;
y++;
}
}
if (linesCleared > 0) {
score += linesCleared * 100 * level;
scoreElement.textContent = score;
linesElement.textContent = lines;
// Update level every 10 lines
const newLevel = Math.floor(lines / 10) + 1;
if (newLevel !== level) {
level = newLevel;
levelElement.textContent = level;
gameSpeed = Math.max(100, 1000 - (level - 1) * 100);
// Update game loop speed
clearInterval(gameLoop);
gameLoop = setInterval(() => {
update();
draw();
}, gameSpeed);
}
}
}
function rotatePiece() {
const rotated = currentPiece.shape[0].map((_, i) =>
currentPiece.shape.map(row => row[row.length - 1 - i])
);
if (isValidMove(currentPieceX, currentPieceY, rotated)) {
currentPiece.shape = rotated;
}
}
function gameOver() {
clearInterval(gameLoop);
gameLoop = null;
// Draw "Game Over" text
ctx.fillStyle = 'rgba(0, 0, 0, 0.75)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#ffffff';
ctx.font = 'bold 24px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over!', canvas.width / 2, canvas.height / 2 - 30);
ctx.fillText(`Score: ${score}`, canvas.width / 2, canvas.height / 2 + 10);
ctx.font = '16px Arial';
ctx.fillText('Press SPACE to restart', canvas.width / 2, canvas.height / 2 + 50);
}
function update() {
if (currentPiece === null) {
createNewPiece();
if (!isValidMove(currentPieceX, currentPieceY)) {
gameOver();
return;
}
}
if (isValidMove(currentPieceX, currentPieceY + 1)) {
currentPieceY++;
} else {
mergePiece();
clearLines();
createNewPiece();
// Check if the new piece can be placed
if (!isValidMove(currentPieceX, currentPieceY)) {
gameOver();
return;
}
}
}
function hardDrop() {
let dropDistance = 0;
while (isValidMove(currentPieceX, currentPieceY + 1)) {
currentPieceY++;
dropDistance++;
}
// Create particles at landing position
for (let y = 0; y < currentPiece.shape.length; y++) {
for (let x = 0; x < currentPiece.shape[y].length; x++) {
if (currentPiece.shape[y][x]) {
createParticles(
(currentPieceX + x) * BLOCK_SIZE + BLOCK_SIZE / 2,
(currentPieceY + y) * BLOCK_SIZE + BLOCK_SIZE / 2,
currentPiece.color,
2
);
}
}
}
score += dropDistance * 2;
scoreElement.textContent = score;
mergePiece();
clearLines();
createNewPiece();
}
// Event listeners
document.addEventListener('keydown', (e) => {
// If game is over and space is pressed, restart the game
if (!gameLoop && e.key === ' ') {
initGame();
return;
}
// Rest of the existing key handlers
switch (e.key) {
case 'ArrowLeft':
if (isValidMove(currentPieceX - 1, currentPieceY)) {
currentPieceX--;
}
break;
case 'ArrowRight':
if (isValidMove(currentPieceX + 1, currentPieceY)) {
currentPieceX++;
}
break;
case 'ArrowDown':
if (isValidMove(currentPieceX, currentPieceY + 1)) {
currentPieceY++;
score += 1;
scoreElement.textContent = score;
}
break;
case 'ArrowUp':
rotatePiece();
break;
case ' ':
if (gameLoop) { // Only allow hard drop if game is running
hardDrop();
}
break;
}
if (gameLoop) { // Only redraw if game is running
draw();
}
});
// Add this function for game initialization
function initGame() {
// Reset game state
board = Array(BOARD_HEIGHT).fill().map(() => Array(BOARD_WIDTH).fill(0));
score = 0;
lines = 0;
level = 1;
nextPiece = null;
currentPiece = null;
gameSpeed = 1000;
// Update display
scoreElement.textContent = score;
linesElement.textContent = lines;
levelElement.textContent = level;
// Create first piece
createNewPiece();
// Clear any existing game loop
if (gameLoop) {
clearInterval(gameLoop);
}
// Start game loop
gameLoop = setInterval(() => {
update();
draw();
}, gameSpeed);
// Initial draw
draw();
}
// Make sure the game starts when the page loads
window.addEventListener('load', () => {
initGame();
});
let gameLoop = null;
initGame();