forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectors-Quest.js
320 lines (294 loc) · 5.54 KB
/
Collectors-Quest.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
/*
@title: Collector's Quest
@author: PawiX25
@tags: []
@addedOn: 2024-08-13
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started
*/
const player = "p";
const enemy = "e";
const wall = "w";
const collectible = "c";
let level = 0;
let isGameOver = false;
let levelStartTime = 0;
let timeSpent = 0;
let timerInterval;
let bonusAchieved = false;
const levels = [
map`
p.w.w...
.w.w.w..
.w.e.w..
...w.c..
..w...w.
w....w..
w.w.w..w`,
map`
p..w.w..
..w.w.w.
.w.e...w
....w.c.
......w.
w.w.w.w.
....w...`,
map`
p...w..w
w.w.w.w.
...e.w.w
w..w..c.
w.w.w..w
.w...w..
..w.w...`,
map`
p.w.....
w....w..
..w.e...
w...w.c.
.w......
w.w....w
...w.w..`,
map`
p..w..w.
.w.....w
..e.....
.w.w.w.c
.......w
....w.w.
w.w..w..`,
map`
p.....w.
w.w.w.w.
....e.w.
w.w...c.
.w.w..w.
w....w.w
..w.w...`,
map`
p.w..w.w
w...w..w
..w.e.w.
....w.w.
w.w....c
.w......
...w.w..`,
map`
p......w
...w.w.w
w.e.....
..w...c.
w...w.w.
.....w..
..w.....`,
map`
p.w.w.w.
.....w..
.w...e.w
....w.c.
w......w
w.w.w...
..w.....`,
map`
p...w...
w.w....w
...e.w.w
.w....c.
..w.w..w
......w.
...w.w..`,
map`
p..w....
..w....w
w...w...
..w.e..w
w......c
w.w....w
...w..w.`,
map`
p.w....w
w...w..w
..w.e.w.
....w..w
.w....c.
w.w.w.w.
w.......`,
];
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
shuffle(levels);
setLegend(
[player, bitmap`
................
................
.......000......
.......0.0......
......0..0......
......0...0.0...
....0003.30.0...
....0.0...000...
....0.05550.....
......0...0.....
.....0....0.....
.....0...0......
......000.......
......0.0.......
.....00.00......
................`],
[enemy, bitmap`
................
................
.......333......
.......3.3......
......3..3......
......3...3.3...
....3334.43.3...
....3.3...333...
....3.34443.....
......3...3.....
.....3....3.....
.....3...3......
......333.......
......3.3.......
.....33.33......
................`],
[wall, bitmap`
LLLLLLLLLLLLLLLL
L11111111111111L
L1LLLLLLLLLLLL1L
L1L1111111111L1L
L1L1LLLLLLLL1L1L
L1L1L1111111L1L1
L1L1L1LLLL1L1L1L
L1L1L1L11L1L1L1L
L1L1L1L11L1L1L1L
L1L1L1LLLL1L1L1L
L1L1L1111111L1L1
L1L1LLLLLLLL1L1L
L1L1111111111L1L
L1LLLLLLLLLLLL1L
L11111111111111L
LLLLLLLLLLLLLLLL`],
[collectible, bitmap`
................
................
.......666......
.......6.6......
......6..6......
......6...6.6...
....6665.56.6...
....6.6...666...
....6.65556.....
......6...6.....
.....6....6.....
.....6...6......
......666.......
......6.6.......
.....66.66......
................`]
);
setSolids([wall, player]);
function displayTimer() {
clearText();
addText(`Level: ${level + 1}`, { x: 1, y: 1, color: color`3` });
addText(`Time: ${timeSpent}s`, { x: 1, y: 2, color: color`3` });
}
function startTimer() {
levelStartTime = Date.now();
timerInterval = setInterval(() => {
if (!isGameOver) {
timeSpent = Math.floor((Date.now() - levelStartTime) / 1000);
displayTimer();
}
}, 1000);
}
function gameOver(message, playAgainMessage) {
isGameOver = true;
clearInterval(timerInterval);
clearText();
addText(message, { x: 5, y: 6, color: color`3` });
addText(playAgainMessage, { x: 1, y: 8, color: color`3` });
}
function restartGame() {
level = 0;
isGameOver = false;
timeSpent = 0;
bonusAchieved = false;
shuffle(levels);
setMap(levels[level]);
startTimer();
clearText();
displayTimer();
}
function checkBonus() {
if (timeSpent <= 30) {
bonusAchieved = true;
addText("Bonus Achieved!", { x: 3, y: 10, color: color`6` });
}
}
setMap(levels[level]);
startTimer();
onInput("w", () => {
if (!isGameOver) getFirst(player).y -= 1;
});
onInput("s", () => {
if (!isGameOver) getFirst(player).y += 1;
});
onInput("a", () => {
if (!isGameOver) getFirst(player).x -= 1;
});
onInput("d", () => {
if (!isGameOver) getFirst(player).x += 1;
});
onInput("l", () => {
if (isGameOver) restartGame();
});
afterInput(() => {
if (isGameOver) return;
const playerPosition = getFirst(player);
const enemyPosition = getFirst(enemy);
const dx = playerPosition.x - enemyPosition.x;
const dy = playerPosition.y - enemyPosition.y;
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0 && getTile(enemyPosition.x + 1, enemyPosition.y).length === 0) {
enemyPosition.x += 1;
} else if (dx < 0 && getTile(enemyPosition.x - 1, enemyPosition.y).length === 0) {
enemyPosition.x -= 1;
}
} else {
if (dy > 0 && getTile(enemyPosition.x, enemyPosition.y + 1).length === 0) {
enemyPosition.y += 1;
} else if (dy < 0 && getTile(enemyPosition.x, enemyPosition.y - 1).length === 0) {
enemyPosition.y -= 1;
}
}
if (playerPosition.x === enemyPosition.x && playerPosition.y === enemyPosition.y) {
gameOver("Game Over", "Press 'L' to Retry");
}
const collectibles = getAll(collectible);
for (const item of collectibles) {
if (playerPosition.x === item.x && playerPosition.y === item.y) {
clearTile(item.x, item.y);
addText("Collected!", { x: 5, y: 4, color: color`5` });
if (getAll(collectible).length === 0) {
checkBonus();
level += 1;
if (level < levels.length) {
setMap(levels[level]);
timeSpent = 0;
startTimer();
} else {
gameOver("You Win!", "Press 'L' to Retry");
clearText();
addText("You Win!", { x: 6, y: 6, color: color`6` });
addText("Press 'L' to Retry", { x: 1, y: 8, color: color`6` });
}
}
}
}
});