-
Notifications
You must be signed in to change notification settings - Fork 1
/
gem_rush.html
328 lines (290 loc) · 13.5 KB
/
gem_rush.html
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
<!DOCTYPE html>
<html>
<head>
<title>Collect the Gems!</title>
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P" rel="stylesheet">
<style>
body {
font-family: "Press Start 2P", sans-serif;
}
#game-container {
position: relative;
}
canvas, #counter, #game-start, #game-over {
position: absolute;
top: 0px;
left: 0px;
}
canvas, #game-start, #game-over {
border: 1px solid gray;
width: 1500px;
height: 730px;
}
canvas {
background: url(image/background_image.png);
background-size: cover;
}
#game-start, #game-over {
background: rgba(1, 1, 1, 0.8);
}
#counter text {
font-size: 130%;
fill: white;
stroke: black;
stroke-width: 1px;
}
#game-start text {
font-size: 150%;
fill: white;
text-anchor: middle;
}
#game-start #game-title {
font-size: 400%;
fill: url(#title-fill);
stroke: black;
}
#game-over text {
font-size: 120%;
fill: url(#game-over-fill);
text-anchor: middle;
}
</style>
</head>
<body>
<div id="game-container">
<canvas width="1500px" height="730px"></canvas>
<svg xmlns="http://www.w3.org/2000/svg" id="counter">
<text x="10" y="35">
TIME:<tspan id="time-remaining">20</tspan>
</text>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" id="game-start">
<defs>
<linearGradient id="title-fill" x1="0" y1="0" x2="0" y2="1">
<stop offset="0.2" stop-color="red" />
<stop offset="0.4" stop-color="yellow" />
<stop offset="0.6" stop-color="green" />
<stop offset="0.8" stop-color="purple" />
</linearGradient>
</defs>
<text id="game-title" x="50%" y="45%">GEM RUSH!</text>
<text x="50%" y="60%">Click here to start the game</text>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" id="game-over" style="display: none">
<defs>
<linearGradient id="game-over-fill" x1="0" y1="0" x2="0" y2="1">
<stop offset="0" stop-color="red" />
<stop offset="0.5" stop-color="yellow" />
<stop offset="1" stop-color="red" />
</linearGradient>
</defs>
<text x="50%" y="50%">
Time's up! You have collected
<tspan id="final-gems">0</tspan>
gem(s).
</text>
</svg>
</div>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script src="bounding_box.js"></script>
<script src="sprite.js"></script>
<script src="player.js"></script>
<script src="Fire.js"></script>
<script src="all_stairs.js"></script>
<script src="stair.js"></script>
<script src="gem.js"></script>
<script>
$(document).ready(function() {
/* Get the canvas and 2D context */
const cv = $("canvas").get(0);
const context = cv.getContext("2d");
/* Sounds effect */
const sounds = {
background: new Audio("background.mp3"),
collect: new Audio("collect.mp3"),
gameover: new Audio("gameover.mp3")
};
/* Global variable */
const totalGameTime = 210; // Total game time in seconds
let gameStartTime = 0; // The timestamp when the game starts
const vertical_total = 7; // Total number of vertical stairs
const horizontal_total = 5; // Total number of horizontal stairs
/* Create the game area */
const gameArea = BoundingBox(context, 110, 100, 700, 1400); // Area that the player allowed to move
const corners = gameArea.getPoints();
/* Create the sprites in the game */
const player = Player(context, 100, 685, gameArea); // The player
// another initial position: (1400, 650)
/*vertical_all_x = [200, 200, 200, 200, 200, 200, 200, 200, 200,
400, 400, 400, 400, 400, 400, 400, 400, 400,
600, 600, 600, 600, 600, 600, 600, 600, 600,
800, 800, 800, 800, 800, 800, 800, 800, 800,
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200, 1200,
1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400, 1400
];
vertical_all_y = [90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730,
90, 170, 250, 330, 410, 490, 570, 650, 730
];
horizontal_all_x = [90, 170, 250, 330, 410, 490, 570, 650, 730, 810, 890, 970, 1050, 1130, 1210, 1290, 1370, 1450,
90, 170, 250, 330, 410, 490, 570, 650, 730, 810, 890, 970, 1050, 1130, 1210, 1290, 1370, 1450,
90, 170, 250, 330, 410, 490, 570, 650, 730, 810, 890, 970, 1050, 1130, 1210, 1290, 1370, 1450,
90, 170, 250, 330, 410, 490, 570, 650, 730, 810, 890, 970, 1050, 1130, 1210, 1290, 1370, 1450
];
horizontal_all_y = [200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200,
400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700
];*/
/* All the stairs */
const vertical_all_x = [200, 400, 600, 800, 1000, 1200, 1400];
const vertical_all_y = [365, 365, 365, 365, 365, 365, 365];
const horizontal_all_x = [750, 750, 750, 750, 750];
const horizontal_all_y = [150, 300, 400, 550, 700];
/* Get the stair objects*/
const vertical_all_stairs = All_Stairs(context, vertical_all_x, vertical_all_y, vertical_total, "vertical");
const vertical_stairs = vertical_all_stairs.getStairs();
const horizontal_all_stairs = All_Stairs(context, horizontal_all_x, horizontal_all_y, horizontal_total, "horizontal");
const horizontal_stairs = horizontal_all_stairs.getStairs();
/* The main processing of the game */
function doFrame(now) {
sounds.background.play();
if (gameStartTime == 0) gameStartTime = now;
/* Update the time remaining */
const gameTimeSoFar = now - gameStartTime;
const timeRemaining = Math.ceil((totalGameTime * 1000 - gameTimeSoFar) / 1000);
$("#time-remaining").text(timeRemaining);
/* Handle the game over situation here */
// if (timeRemaining <= 0){
// sounds.background.pause();
// sounds.gameover.play();
// $("#final-gems").text(collectedGems);
// $("#game-over").show();
// return
// }
/* Update the sprites*/
for (let j=0; j<vertical_total; j++){
vertical_stairs[j].update(now);
}
for (let j=0; j<horizontal_total; j++){
horizontal_stairs[j].update(now);
}
player.update(now);
/*Handle player control*/
// player, vertical_stairs, horizontal_stairs, total
player_box = player.getBoundingBox();
const {x_center, y_center} = player_box.getCenter();
let at_intersection = false; // Check whether player is at the intersection
for (let j=0; j<vertical_total; j++){
const vert_stair_box = vertical_stairs[j].getBoundingBox();
for (let k=0; k<horizontal_total; k++){
const hor_stair_box = horizontal_stairs[k].getBoundingBox();
if (hor_stair_box.isPointInBox(x_center, y_center) && vert_stair_box.isPointInBox(x_center, y_center)){
// Change the keydown event
// Player can go left, right, up and down
$(document).off("keydown");
$(document).on("keydown", function(event) {
console.log(event.keyCode);
switch (event.keyCode) {
case 16: player.furtherSpeedUp(); break; // Shift
case 32: player.speedUp(); break; // Space
case 37: player.move(1); break; // Left
case 38: player.move(2); break; // Up
case 39: player.move(3); break; // Right
case 40: player.move(4); break; // Down
}
});
at_intersection = true;
break;
}
}
if (at_intersection){
break;
}
}
if (!at_intersection){
for (let j=0; j<vertical_total; j++){
const vert_stair_box = vertical_stairs[j].getBoundingBox();
if (vert_stair_box.isPointInBox(x_center, y_center)){
// Change the keydown event
// Player can only go up and down
$(document).off("keydown");
$(document).on("keydown", function(event) {
switch (event.keyCode) {
case 16: player.furtherSpeedUp(); break; // Shift
case 32: player.speedUp(); break; // Space
case 38: player.move(2); break; // Up
case 40: player.move(4); break; // Down
}
});
break;
}else{
// Change the keydown event
// Player can only go left and right
$(document).off("keydown");
$(document).on("keydown", function(event) {
switch (event.keyCode) {
case 16: player.furtherSpeedUp(); break; // Shift
case 32: player.speedUp(); break; // Space
case 37: player.move(1); break; // Left
case 39: player.move(3); break; // Right
}
});
}
}
}
/* Clear the screen */
context.clearRect(0, 0, cv.width, cv.height);
/* Draw the sprites*/
for (let j=0; j<vertical_total; j++){
vertical_stairs[j].draw();
}
for (let j=0; j<horizontal_total; j++){
horizontal_stairs[j].draw();
}
player.draw();
/* Process the next frame */
requestAnimationFrame(doFrame);
}
/* Handle the start of the game */
$("#game-start").on("click", function() {
/* Hide the start screen */
$("#game-start").hide();
/* Handle the keydown of arrow keys and spacebar */
$(document).on("keydown", function(event) {
/* TODO */
/* Handle the key down */
switch (event.keyCode) {
case 16: player.furtherSpeedUp(); break; // shift
case 32: player.speedUp(); break; // space
case 37: player.move(1); break; // left
//case 38: player.move(2); break; // up
case 39: player.move(3); break; // right
//case 40: player.move(4); break; // down
}
});
/* Handle the keyup of arrow keys and spacebar */
$(document).on("keyup", function(event) {
/* TODO */
/* Handle the key up */
switch (event.keyCode) {
case 16: player.slowDown(); break;
case 32: player.slowDown(); break;
case 37: player.stop(1); break;
case 38: player.stop(2); break;
case 39: player.stop(3); break;
case 40: player.stop(4); break;
}
});
/* Start the game */
requestAnimationFrame(doFrame);
});
});
</script>
</body>
</html>