forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDogger.js
384 lines (335 loc) · 9.01 KB
/
Dogger.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
/*
@title: Dogger
@author: Boyce Dyson
@tags: []
@addedOn: 2024-00-00
*/
const player = "p"
const road = "r"
const car = "c"
const grass = "g"
const water = "w"
const log = "l"
let Score = 0
let Dead = false
setLegend(
[ player, bitmap`
................
................
................
................
................
.......1L..L1...
........LLLL....
........L0L0....
...1....LLLLL0..
..1.....LLLLL...
..L..LLLL11.....
...LLLLLLLL.....
....LLLLLLL.....
....LLLLLLL.....
....L.L.L.L.....
....L.L.L.L.....` ],
[ car, bitmap`
................
..3333..........
..3003..........
..3003..........
..3333..........
..333333333333..
.33333333333333.
.33333333333333.
.33333333333333.
.33333333333333.
.3000......0003.
.00000....00000.
.00000....00000.
.00000....00000.
..000......000..
................
................`],
[ grass, bitmap`
DDDD444444444444
DDDD444444444444
DDD44444444DDD44
DDD4444444DDDDD4
D444444444DDDDD4
44444444444DDD44
4444444444444444
4444444444444444
4444444444444444
444444DD44444444
44444DDDD4444D44
4444DDDD4444DD44
44444DDD4444DDD4
44444444444DDDD4
4444444444444444
4444444444444444`],
[ road, bitmap`
1111111111111111
1111111111111111
1111111111111111
0000000000000000
0000000000000000
0000000000000000
0000000000000000
6660666066606660
0000000000000000
0000000000000000
0000000000000000
0000000000000000
1111111111111111
1111111111111111
1111111111111111
1111111111111111`],
[ log, bitmap`
................
................
................
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
................
................
................`],
[ water, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
7777777777777777
7755577777555557
5557555555577775
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7755577777555557
5557555555577775
7777777777777777
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`]
)
setSolids([])
let level = 0
const levels = [
map`
gggggggggggg
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
gggggggggggg`,
map`
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
gggggggggggg
rrrrrrrrrrrr
rrrrrrrrrrrr
gggggggggggg
gggggggggggg
gggggggggggg`,
map`
gggggggggggg
gggggggggggg
wwwwwwwwwwww
rrrrrrrrrrrr
gggggggggggg
wwwwwwwwwwww
gggggggggggg
gggggggggggg
gggggggggggg`,
map`
gggggggggggg
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
wwwwwwwwwwww
wwwwwwwwwwww
gggggggggggg
gggggggggggg
gggggggggggg`,
map`
gggggggggggg
gggggggggggg
rrrrrrrrrrrr
wwwwwwwwwwww
rrrrrrrrrrrr
gggggggggggg
rrrrrrrrrrrr
gggggggggggg
gggggggggggg`
]
const MoveSound = tune`
136.36363636363637: A4~136.36363636363637,
4227.272727272728`
const DeathJingle = tune`
285.7142857142857: A4-285.7142857142857,
285.7142857142857: F4~285.7142857142857,
285.7142857142857: G4-285.7142857142857 + E4~285.7142857142857,
285.7142857142857: F4-285.7142857142857 + D4~285.7142857142857,
285.7142857142857: E4-285.7142857142857,
285.7142857142857: G4/285.7142857142857,
285.7142857142857: C4/285.7142857142857,
7142.857142857143`
setMap(levels[level])
setPushables({
[ player ]: []
})
function Death() {
Dead = true
Score = 0
playTune(DeathJingle)
setTimeout(() => {
const randomLevelIndex = Math.floor(Math.random() * levels.length);
setMap(levels[randomLevelIndex]);
spawnPlayer();
Dead = false
}, 3000); // Wait for 3 seconds before selecting a random level
}
function NewLevel(){
const randomLevelIndex = Math.floor(Math.random() * levels.length);
setMap(levels[randomLevelIndex]);
Score += 1
spawnPlayer();
// Spawn cars in random positions on the road when entering a new level
for (let i = 0; i < 7; i++) { // Spawn 3 cars randomly
const randomY = Math.floor(Math.random() * height());// Generate a random Y coordinate
const randomX = Math.floor(Math.random() * width());
if (getTile(randomX, randomY).some(sprite => sprite.type === road)) { // Check if the tile at (0, randomY) is a road tile
addSprite(randomX, randomY, car);} // Spawn a car sprite on the left side of the map at the random Y coordinate on a road tile
}
}
function spawnPlayer() {
const playerY = height() - 1; // Set the player's initial Y position to the bottom of the map
const playerX = Math.floor(width() / 2); // Set the player's initial X position to the middle of the map
addSprite(playerX, playerY, player); // Spawn the player sprite at the defined position
}
spawnPlayer(); // Call the spawnPlayer function to place the player at the bottom of the map when the game starts.
function spawnCar() {
const randomY = Math.floor(Math.random() * height()); // Generate a random Y coordinate
if (getTile(0, randomY).some(sprite => sprite.type === road)) { // Check if the tile at (0, randomY) is a road tile
addSprite(0, randomY, car); // Spawn a car sprite on the left side of the map at the random Y coordinate on a road tile
}
}
setInterval(spawnCar, Math.random() * 90 + 40 ); // Spawn a car at a random interval between 0.01 and 0.09 seconds
function MoveCars() {
const cars = getAll(car);
const playerSprite = getFirst(player);
if (!playerSprite) return; // Check if the player sprite exists
cars.forEach(carSprite => {
if (carSprite.x === playerSprite.x && carSprite.y === playerSprite.y) {
playerSprite.remove(); // Remove the player sprite if hit by a car
console.log("Player died."); // Display a message and perform game over actions
Death();
}
else {
if (carSprite.x >= width() - 1) {
carSprite.remove(); // Remove the car sprite when it reaches the end
}
else {
carSprite.x += 1; // Move the car sprite to the right
}
}
});
}
setInterval(MoveCars, Math.random() * 50 + 25);
let logsSpawned = 0; // Variable to track the number of logs spawned
function spawnLogs() {
const randomY = Math.floor(Math.random() * height()); // Generate a random Y coordinate
if (getTile(0, randomY).some(sprite => sprite.type === water && logsSpawned <8)) { // Check if the tile at (0, randomY) is a water tile
addSprite(0, randomY, log); // Spawn a car sprite on the left side of the map at the random Y coordinate on a water tile
}
}
setInterval(() => {
logsSpawned = 0; // Reset the logsSpawned count for the next cycle
}, 2000); // Reset every 2 seconds
setInterval(spawnLogs, 150); // Spawn logs at a random interval between 1 and 2 seconds
function moveLogs() {
const logs = getAll(log);
const playerSprite = getFirst(player);
logs.forEach(logSprite => {
if (logSprite.x >= width() - 1) {
if (playerSprite && playerSprite.x === logSprite.x && playerSprite.y === logSprite.y) {
playerSprite.remove(); // Remove the player sprite if on a log and hitting the right side
console.log("Player rode the river off the map.");
Death();
}
else {
logSprite.remove(); // Remove the log sprite when it reaches the end
}
}
else {
logSprite.x += 1; // Move the log sprite to the right
// Check if the player is on the log and move the player with the log
if (playerSprite && playerSprite.x === logSprite.x - 1 && playerSprite.y === logSprite.y) {
playerSprite.x += 1; // Move the player along with the log
}
}
});
}
setInterval(moveLogs, 550);
const keyState = {}; // Object to track key states
document.addEventListener('keydown', (event) => {
if (event.key === 'w' || event.key === 'a' || event.key === 's' || event.key === 'd') {
if (!keyState[event.key] && !Dead) {
keyState[event.key] = true;
movePlayer(event.key);
}
}
});
document.addEventListener('keyup', (event) => {
keyState[event.key] = false;
});
function movePlayer(direction) {
const playerSprite = getFirst(player);
if (!playerSprite) return;
let newX = playerSprite.x;
let newY = playerSprite.y;
// Calculate the new position based on the movement direction
if (direction === 'w') {
newY -= 1;
} else if (direction === 'a') {
newX -= 1;
} else if (direction === 's') {
newY += 1;
} else if (direction === 'd') {
newX += 1;
}
// Check if the player is on a log at the new position
const isOnLog = getTile(newX, newY).some(sprite => sprite.type === log);
playTune(MoveSound)
// If the player is on a log or on grass, allow movement
if (isOnLog || getTile(newX, newY).some(sprite => sprite.type === grass || getTile(newX, newY).some(sprite => sprite.type === road))) {
playerSprite.x = newX;
playerSprite.y = newY;
} else if (!isOnLog && getTile(newX, newY).some(sprite => sprite.type === water)) {
// If the player is not on a log and is on water, consider it as death
console.log("Player drowned in water.");
playerSprite.remove();
Death();
}
}
afterInput(() => {
const playerSprite = getFirst(player);
addText(`${Score}`, {
x: 15,
y: 1,
color: color`2`
})
if (playerSprite && playerSprite.y === 0) {
console.log("Player reached the top of the map.");
NewLevel();
}
})