forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFallingTiles.js
287 lines (248 loc) · 5.87 KB
/
FallingTiles.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
/*
@title: FallingTiles
@author: Rishabh R
@tags: []
@addedOn: 2024-09-03
*/
const tile = "t";
const background = "b";
const line = "l";
const flashSprite = "f"; // New sprite for flashing effect
setLegend(
[tile, bitmap`
. . . . . . . .
. 1 1 1 1 1 1 .
. 1 1 1 1 1 1 .
. 1 1 1 1 1 1 .
. 1 1 1 1 1 1 .
. 1 1 1 1 1 1 .
. 1 1 1 1 1 1 .
. . . . . . . .
`],
[background, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................`],
[flashSprite, bitmap`
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444`],
[line, bitmap`
. . . . . . . .
. . . . . . . .
. . . . . . . .
. 3 3 3 3 3 3 .
. 3 3 3 3 3 3 .
. . . . . . . .
. . . . . . . .
. . . . . . . .
`]
);
const numColumns = 4;
const gridHeight = 8;
const gridWidth = 4;
const scoreRow = gridHeight - 3;
let score = 0;
let highScore = 0;
let misses = 0;
const maxMisses = 5;
let gameOver = false;
let tilesReadyToRemove = [];
const spawnRate = 600;
const moveRate = 300;
let currentState = "start";
let gameLoopInterval, spawnInterval, moveInterval;
const missedSound = tune`
65.50218340611353: F4/65.50218340611353,
65.50218340611353: C4/65.50218340611353,
1965.065502183406`;
const scoredSound = tune`
42.97994269340974: G5-42.97994269340974,
42.97994269340974: B5-42.97994269340974,
1289.3982808022922`;
const inputCooldowns = [false, false, false, false]; // Track cooldowns for each column
const keyStates = { a: false, d: false, j: false, l: false }; // Track if a key is pressed
function startScreen() {
clearText();
addText("Falling Tiles", { x: 4, y: 2, color: color`0` });
addText("press s to start", { x: 2, y: 14, color: color`L` });
setMap(map`
.....
.....
.....
.....`);
}
function gameScreen() {
score = 0;
misses = 0;
gameOver = false;
tilesReadyToRemove = [];
const level = map`
....
....
....
....
....
....
....
....`;
setMap(level);
setBackground(background);
for (let x = 0; x < gridWidth; x++) {
addSprite(x, scoreRow, line);
}
onInput("a", () => handleInput(0, "a"));
onInput("d", () => handleInput(1, "d"));
onInput("j", () => handleInput(2, "j"));
onInput("l", () => handleInput(3, "l"));
// Reset the key state after each input
afterInput(() => {
resetKeyState("a");
resetKeyState("d");
resetKeyState("j");
resetKeyState("l");
});
gameLoopInterval = setInterval(gameLoop, 50);
spawnInterval = setInterval(spawnTile, spawnRate);
moveInterval = setInterval(handleTileMovement, moveRate);
}
function loseScreen() {
if (score > highScore) highScore = score;
clearText();
addText("YOU LOSE", { x: 6, y: 2, color: color`3` });
addText(`Score: ${score}`, { x: 6, y: 5, color: color`5` });
if (score === highScore) {
addText(`NEW High Score: ${highScore}`, { x: 2, y: 7, color: color`6` });
} else {
addText(`High Score: ${highScore}`, { x: 4, y: 7, color: color`6` });
}
addText("press w to restart", { x: 1, y: 14, color: color`L` });
setMap(map`
.....
.....
.....
.....`);
}
function updateScore() {
clearText();
addText("Score: " + score, {
x: 0,
y: 0,
color: gameOver ? color`3` : color`5`
});
addText("Misses: " + misses, {
x: 0,
y: 1,
color: gameOver ? color`3` : color`5`
});
}
function spawnTile() {
if (currentState !== "game") return;
if (Math.random() >= 0.5) {
const col = Math.floor(Math.random() * numColumns);
addSprite(col, 0, tile);
}
}
function handleTileMovement() {
if (currentState !== "game") return;
const allTiles = getAll(tile);
const newTilesReadyToRemove = [];
allTiles.forEach(t => {
if (t.y < gridHeight - 1) {
t.y += 1;
if (t.y == gridHeight - 1) newTilesReadyToRemove.push(t);
} else if (tilesReadyToRemove.includes(t)) {
t.remove();
misses++;
playTune(missedSound);
if (misses >= maxMisses) {
gameOver = true;
currentState = "lose";
clearInterval(gameLoopInterval);
clearInterval(spawnInterval);
clearInterval(moveInterval);
loseScreen();
}
}
});
tilesReadyToRemove = newTilesReadyToRemove;
}
function handleInput(col, key) {
if (currentState !== "game" || inputCooldowns[col] || keyStates[key]) return;
keyStates[key] = true; // Mark key as held
// Original input handling logic
inputCooldowns[col] = true;
flashColumn(col);
checkScored(col);
setTimeout(() => inputCooldowns[col] = false, 200); // Cooldown duration
}
function resetKeyState(key) {
keyStates[key] = false; // Mark key as released
}
function flashColumn(col) {
// Add flash sprites to the column
for (let y = 0; y < gridHeight; y++) {
addSprite(col, y, flashSprite);
}
// Remove flash sprites after a short duration
setTimeout(() => removeFlashColumn(col), 100); // Flash duration of 100ms
}
function removeFlashColumn(col) {
const flashSprites = getAll(flashSprite).filter(s => s.x === col);
flashSprites.forEach(sprite => sprite.remove());
}
function checkScored(col) {
const tileAtScoreRow = getTile(col, scoreRow).find(t => t.type === tile);
if (tileAtScoreRow) {
tileAtScoreRow.remove();
score++;
playTune(scoredSound);
}
}
function gameLoop() {
if (currentState !== "game") return;
updateScore();
}
function resetGame() {
clearInterval(gameLoopInterval);
gameScreen();
}
onInput("s", () => {
if (currentState === "start") {
currentState = "game";
gameScreen();
}
});
onInput("w", () => {
if (currentState === "lose") {
currentState = "start";
startScreen();
}
});
startScreen();