forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArcade-Treasure.js
247 lines (221 loc) · 5.15 KB
/
Arcade-Treasure.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
/*
@title: Treasure_Quest
@author: Albin Thapaliya
@tags: ['puzzle','adventure']
@addedOn: 2024-06-18
// Treasure Quest
// Navigate through obstacles and puzzles to reach the treasure and beaware of enemy!
*/
const player = "p";
const background = "b";
const treasure = "t";
const block = "s";
const enemy = "e";
setLegend(
[player, bitmap`
................
......00000.....
.....0.....0....
....0.0...0.0...
....0..000..0...
.....0.....0....
......00000.....
.....00...00....
....0.......0...
...0.........0..
...0.........0..
...0.........0..
...0.........0..
....000...000...
......0...0.....`],
[background, bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`],
[block, bitmap`
CCCCCCCCL11111111
CCCCCCCCL11111111
CCCCCCCCLL1111111
CCCCCCCCLLL111111
CCCCCCCCCLL111111
CCCCCCCCCCL111111
CCCCCCCCCCL111111
CCCCCCCCCLLL11111
CCCCCCCCCLLL11111
CCCCCCCCLLL111111
CCCCCCCCCLL111111
CCCCCCCCCCL111111
CCCCCCCCCCL111111
CCCCCCCCCLL111111
CCCCCCCCLLL111111
CCCCCCCCLL1111111`],
[treasure, bitmap`
0000000000000000
0CCLCCCCCLCCLCL0
0LCCLCCLLCCCLLC0
0CLCLCLCCCCLCLC0
0CCLLCCLCCLCCLC0
0CCCC11111CCLCL0
0000011111000000
0000011111000000
0CCCC11111CCLCC0
0CLCC11111CCCLC0
0CCLCCCCCCLCCCC0
0CCLCCCCLCCCLCL0
0CLCCCLCCCCLCCC0
0CCCCLCLLCCLLCC0
0CCCCCCCCCLCLCC0
0000000000000000`],
[enemy, bitmap`
9999999999999999
9999111199991119
9911111119111119
9991111199991119
9999111199991119
9999999999999999
9991111199991119
9911111119111119
9911111119111119
9991111199991119
9999111199991119
9999999999999999
9991111199991119
9911111119111119
9991111199991119
9999111199991119`]
);
setSolids([block, player]);
const levels = [
map`
sssss....s..s.s
p........s..s.s
.s..ssss.s..s.s
ss..s....s..s..
.s............s
....sssssss.sss
.s..s..s.......
ss.............
.ss.s.sss.ssss.
.......s...s...
.s..s......s..s
.s..s..s.s....s
sss...ss.sss.ss`
];
function initializeGame() {
clearText();
setMap(levels[0]);
setBackground(background);
timer = 0;
treasureCount = 0;
enemyMoveCounter = 0;
addSprite(14, 3, enemy);
startTimer();
updateText();
placeTreasure();
}
let timer = 0, treasureCount = 0;
let enemyMoveCounter = 0;
const enemyMoveDelay = 2;
let gameInterval;
function startTimer() {
clearInterval(gameInterval);
gameInterval = setInterval(() => {
timer++;
updateText();
}, 1000);
}
function updateText() {
clearText();
addText(`Time: ${timer}s`, { x: 1, y: 1, color: '3' });
addText(`Treasures: ${treasureCount}`, { x: 1, y: 2, color: '3' });
}
function placeTreasure() {
let placed = false;
let enemyPos = getFirst(enemy);
const minDistanceFromEnemy = 5;
while (!placed) {
let x = Math.floor(Math.random() * width());
let y = Math.floor(Math.random() * height());
let distance = Math.abs(x - enemyPos.x) + Math.abs(y - enemyPos.y);
if (getTile(x, y).length === 0 && distance > minDistanceFromEnemy) {
addSprite(x, y, treasure);
placed = true;
}
}
}
function moveEnemy() {
enemyMoveCounter++;
if (enemyMoveCounter >= enemyMoveDelay) {
enemyMoveCounter = 0;
let playerPos = getFirst(player);
let enemyPos = getFirst(enemy);
let moves = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1]
];
moves.sort((a, b) => {
let distA = Math.abs((enemyPos.x + a[0]) - playerPos.x) + Math.abs((enemyPos.y + a[1]) - playerPos.y);
let distB = Math.abs((enemyPos.x + b[0]) - playerPos.x) + Math.abs((enemyPos.y + b[1]) - playerPos.y);
return distA - distB;
});
for (let move of moves) {
let newX = enemyPos.x + move[0];
let newY = enemyPos.y + move[1];
if (isValidMove(newX, newY)) {
enemyPos.x = newX;
enemyPos.y = newY;
break;
}
}
}
}
function isValidMove(x, y) {
return x >= 0 && x < width() && y >= 0 && y < height() && !getTile(x, y).some(sprite => sprite.type === block);
}
onInput("s", () => { getFirst(player).y += 1; });
onInput("w", () => { getFirst(player).y -= 1; });
onInput("a", () => { getFirst(player).x -= 1; });
onInput("d", () => { getFirst(player).x += 1; });
function checkGameOver() {
let playerPos = getFirst(player);
let enemyPos = getFirst(enemy);
if (playerPos.x === enemyPos.x && playerPos.y === enemyPos.y) {
gameOver();
}
}
function gameOver() {
clearInterval(gameInterval);
addText("Game Over!", { x: 5, y: 8, color: '9' });
setTimeout(() => {
initializeGame();
}, 5000);
}
afterInput(() => {
moveEnemy();
checkGameOver();
let playerPos = getFirst(player);
getAll(treasure).forEach(t => {
if (t.x === playerPos.x && t.y === playerPos.y) {
t.remove();
treasureCount++;
placeTreasure();
updateText();
}
});
});
initializeGame();