forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrystal-Caper-The-Jewel-Thieves.js
263 lines (228 loc) · 4.69 KB
/
Crystal-Caper-The-Jewel-Thieves.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
/*
@title: Crystal Caper: The Jewel Thieves
@author: Jaisinh
@tags: []
@addedOn: 2024-11-21
*/
const player = "p";
const Bot = "b";
const Emerald = "e";
const obstacles1 = "o";
const background = "g";
const bullet = "l";
setLegend(
[player, bitmap`
................
................
...........000..
...........000..
...........000..
...000.....6L6..
...000.....0L0..
...000CCCC.0L0..
...00CCCCCC000..
..0LLCCCCCCLLL..
.0330CCCCCC0330.
.0330CCCCCC0330.
.0330CCCCCC0330.
..0000CCCC0000..
................
................`],
[Bot, bitmap`
................
................
...........000..
...........000..
...........000..
...000.....2L2..
...000.....0L0..
...0002222.0L0..
...00222222000..
..0LL222222LLL..
.00002222220000.
.00002222220000.
.00002222220000.
..000022220000..
................
................`],
[Emerald, bitmap`
2222222222222222
2222222222222222
2222244442222222
222242DD44222222
22242DDDD4422222
22244DDDD4422222
22244DDDD4422222
22244DDDD4422222
22244DDDD4422222
22244DDDD4422222
22244DDDD4422222
222244DD44222222
2222244442222222
2222222222222222
2222222222222222
2222222222222222`],
[obstacles1, bitmap`
0000000000000000
0LL1111111111LL0
0L11LLLLLLLL11L0
011LLLLLLLLLL110
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
01LLLLLLLLLLLL10
011LLLLLLLLLL110
0L11LLLLLLLL11L0
0LL1111111111LL0
0000000000000000`],
[background, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
[bullet, bitmap`
................
................
................
................
................
.......L0.......
......LL00......
......L200......
......0000......
......0000......
......0000......
................
................
................
................
................`],
);
setSolids([obstacles1]);
let level = 0;
const levels = [
map`
eggggbggggggoggg
gggggoggeoggoggg
oooogoooooggooog
oggggggggggggggg
oegoggggggggoooo
oobogooogoogoggo
oggogoggbeogogeo
oggogoegggogogoo
ogoogoogoooggggb
gggggggggggggggg
bgggggoooogooogg
oooggggggbggeogg
gggggoooggooooge
pggogggggggggggg`
];
setMap(levels[level]);
let emeraldCount = 0;
const totalEmeralds = 8;
let gameEnded = false;
setPushables({
[player]: []
});
onInput("w", () => movePlayer(0, -1));
onInput("s", () => movePlayer(0, 1));
onInput("a", () => movePlayer(-1, 0));
onInput("d", () => movePlayer(1, 0));
function movePlayer(dx, dy) {
if (gameEnded) return;
const player = getFirst("p");
const newX = player.x + dx;
const newY = player.y + dy;
if (!getTile(newX, newY).some(t => t.type === obstacles1)) {
player.x = newX;
player.y = newY;
collectEmerald(newX, newY);
checkPlayerBotCollision(newX, newY);
}
}
function collectEmerald(playerX, playerY) {
const adjacentPositions = [
{ x: playerX, y: playerY - 1 },
{ x: playerX, y: playerY + 1 },
{ x: playerX - 1, y: playerY },
{ x: playerX + 1, y: playerY }
];
adjacentPositions.forEach(pos => {
const emerald = getTile(pos.x, pos.y).find(t => t.type === Emerald);
if (emerald) {
emeraldCount += 1;
clearTile(pos.x, pos.y);
updateEmeraldCount();
checkWinCondition();
}
});
}
function moveBot(bot) {
if (gameEnded) return;
const directions = [
{ x: 0, y: -1 },
{ x: 0, y: 1 },
{ x: -1, y: 0 },
{ x: 1, y: 0 }
];
const randomDirection = directions[Math.floor(Math.random() * directions.length)];
const newX = bot.x + randomDirection.x;
const newY = bot.y + randomDirection.y;
if (!getTile(newX, newY).some(t => t.type === obstacles1)) {
bot.x = newX;
bot.y = newY;
checkBotCollision(bot.x, bot.y);
}
}
function checkPlayerBotCollision(playerX, playerY) {
const bots = getAll(Bot);
bots.forEach(bot => {
if (bot.x === playerX && bot.y === playerY) {
gameOver();
}
});
}
function checkBotCollision(botX, botY) {
const player = getFirst("p");
if (player.x === botX && player.y === botY) {
gameOver();
}
}
function checkWinCondition() {
if (emeraldCount >= totalEmeralds) {
gameWin();
}
}
function gameOver() {
gameEnded = true;
clearText();
addText("Game Over", { x: 6, y: 6, color: color`3`, fontSize: 20 });
}
function gameWin() {
gameEnded = true;
clearText();
addText("You Win!", { x: 5, y: 6, color: color`3`, });
}
function updateEmeraldCount() {
clearText();
addText(`Emeralds: ${emeraldCount}`, { x: 5, y: 0, color: color`4`,});
}
setInterval(() => {
const bots = getAll(Bot);
bots.forEach(bot => moveBot(bot));
}, 500);