-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (141 loc) · 4.37 KB
/
script.js
File metadata and controls
154 lines (141 loc) · 4.37 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
const grid = document.getElementById('grid');
const scoreDisplay = document.getElementById('score');
const startBtn = document.getElementById('start-btn');
const gameOverDiv = document.getElementById('game-over');
const restartBtn = document.getElementById('restart-btn');
const gameContainer = document.getElementById('game-container');
const COLS = 4;
const TILE_HEIGHT = 80;
const GAME_HEIGHT = 480;
const TILE_SPEED = 2.5; // px per frame
const TILE_INTERVAL = 700; // ms between new tiles
const CM_TO_PX = 37.8; // 1cm ≈ 37.8px on most screens
const TILE_GAP_CM = 4;
const TILE_GAP_PX = TILE_GAP_CM * CM_TO_PX; // 4cm gap in px
let tiles = [];
let score = 0;
let gameRunning = false;
let animationId = null;
let tileIntervalId = null;
// The Happy Birthday note sequence
const noteSequence = [
'Do','Do','Re','Do','Fa','Mi',
'Do','Do','Re','Do','Sol','Fa',
'Do','Do','La','Fa','Mi',
'Re','Si','Si','La','Fa','Sol','Fa'
];
let noteIndex = 0;
// Preload audio elements for each note
const noteNames = ['Do', 'Re', 'Mi', 'Fa', 'Sol', 'La', 'Si'];
const noteAudios = {};
noteNames.forEach(note => {
const audio = new Audio(`sounds/${note}.mp3`);
noteAudios[note] = audio;
});
function resetGame() {
tiles = [];
score = 0;
noteIndex = 0; // Reset note sequence
scoreDisplay.textContent = 'Score: 0';
grid.innerHTML = '';
gameOverDiv.style.display = 'none';
gameRunning = false;
cancelAnimationFrame(animationId);
clearInterval(tileIntervalId);
}
function startGame() {
resetGame();
gameRunning = true;
startBtn.style.display = 'none';
spawnTile();
tileIntervalId = setInterval(spawnTile, TILE_INTERVAL);
animationId = requestAnimationFrame(gameLoop);
}
function endGame() {
gameRunning = false;
clearInterval(tileIntervalId);
cancelAnimationFrame(animationId);
gameOverDiv.style.display = 'block';
startBtn.style.display = 'block';
}
function spawnTile() {
// Find which columns are eligible for a new tile (no tile or last tile is far enough down)
const eligibleCols = [];
for (let col = 0; col < COLS; col++) {
// Find the highest (most recent) tile in this column
const colTiles = tiles.filter(t => t.col === col && !t.clicked);
if (colTiles.length === 0) {
eligibleCols.push(col);
} else {
// Find the tile with the smallest y (closest to the top)
const lastTile = colTiles.reduce((minT, t) => t.y > minT.y ? t : minT, colTiles[0]);
if (lastTile.y > TILE_GAP_PX) {
eligibleCols.push(col);
}
}
}
if (eligibleCols.length === 0) return; // No eligible columns, skip this spawn
const col = eligibleCols[Math.floor(Math.random() * eligibleCols.length)];
const tile = {
col,
y: -TILE_HEIGHT,
clicked: false,
el: null
};
const tileDiv = document.createElement('div');
tileDiv.className = 'tile';
tileDiv.style.left = (col * 25) + '%';
tileDiv.style.top = tile.y + 'px';
tileDiv.style.height = TILE_HEIGHT + 'px';
tileDiv.addEventListener('click', () => {
if (!gameRunning || tile.clicked) return;
tile.clicked = true;
tileDiv.classList.add('clicked');
setTimeout(() => tileDiv.classList.remove('clicked'), 180);
tileDiv.style.background = '#4caf50';
// Play the next note in the Happy Birthday sequence
if (noteIndex < noteSequence.length) {
const note = noteSequence[noteIndex];
const audio = noteAudios[note];
if (audio) {
audio.currentTime = 0;
audio.play();
}
noteIndex++;
}
score++;
scoreDisplay.textContent = 'Score: ' + score;
setTimeout(() => {
if (tileDiv.parentNode) tileDiv.parentNode.removeChild(tileDiv);
}, 100);
});
tile.el = tileDiv;
grid.appendChild(tileDiv);
tiles.push(tile);
}
function gameLoop() {
if (!gameRunning) return;
for (let i = tiles.length - 1; i >= 0; i--) {
const tile = tiles[i];
if (tile.clicked) {
tiles.splice(i, 1);
continue;
}
tile.y += TILE_SPEED;
tile.el.style.top = tile.y + 'px';
if (tile.y + TILE_HEIGHT >= GAME_HEIGHT) {
// Missed tile
endGame();
return;
}
}
animationId = requestAnimationFrame(gameLoop);
}
startBtn.addEventListener('click', startGame);
restartBtn.addEventListener('click', startGame);
// Optional: allow pressing space to start/restart
document.addEventListener('keydown', (e) => {
if ((e.key === ' ' || e.key === 'Spacebar') && !gameRunning) {
startGame();
}
});