-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
248 lines (213 loc) · 6.86 KB
/
scripts.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
// preload images
const obstacleImage = new Image();
obstacleImage.src = 'images/minecraftobstacle.jpeg';
const sanitizerImage = new Image();
sanitizerImage.src = 'images/handsanitizer.png';
const maskImage = new Image();
maskImage.src = 'images/mask.png';
var canvas = document.getElementById("canv");
var ctx = canvas.getContext("2d");
canvas.width = 1200;
canvas.height = 800;
var gravity = 0.5;
var canJump = true;
var score = 0;
var currentQuestionId = 1;
var lifesaver = false;
var jetpack = false;
// Player class
class Player {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.ySpeed = 0;
this.xSpeed = 5;
}
show() {
ctx.fillStyle = lifesaver ? "blue" : "red";
ctx.fillRect(this.x, this.y, this.width, this.height);
}
update() {
this.y += this.ySpeed;
this.ySpeed += gravity;
if (this.y + this.height > canvas.height - 100) { // Check ground collision
this.y = canvas.height - 100 - this.height;
this.ySpeed = 0;
canJump = true;
}
}
}
var player = new Player(100, canvas.height - 150, 40, 60);
// GameElement class
class GameElement {
constructor(image, x, y, width, height) {
this.image = image;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
show() {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
update() {
this.x -= player.xSpeed;
}
}
// Obstacle class
class Obstacle extends GameElement {
constructor(image, x, y, width, height) {
super(image, x, y, width, height);
this.active = true;
}
update() {
super.update();
if (this.active) {
this.checkCollision();
}
}
checkCollision() {
if (this.x < player.x + player.width && this.x + this.width > player.x &&
this.y < player.y + player.height && this.y + this.height > player.y) {
if (!lifesaver) {
isDead();
} else {
lifesaver = false; // disable lifesaver after surviving one hit
this.active = false; // it's not active
}
}
}
}
// Sanitizer class
class Sanitizer extends GameElement {
constructor(image, x, y, width, height) {
super(image, x, y, width, height);
this.collected = false; // New property to control whether it's collected
}
update() {
super.update();
if (!this.collected) {
this.checkCollision();
}
}
checkCollision() {
// collision detection logic
if (this.x < player.x + player.width && this.x + this.width > player.x &&
this.y < player.y + player.height && this.y + this.height > player.y) {
lifesaver = true;
this.collected = true; // set the sanitizer as collected
}
}
}
// Mask class
class Mask extends GameElement {
constructor(image, x, y, width, height) {
super(image, x, y, width, height);
this.collided = false; // flag to check if the collision has already been handled
}
update() {
super.update();
this.checkCollision();
}
checkCollision() {
// colllision detection logic
if (!this.collided && this.x < player.x + player.width && this.x + this.width > player.x &&
this.y < player.y + player.height && this.y + this.height > player.y) {
this.collided = true; // true on first collision
fetchQuestion();
setTimeout(() => this.collided = false, 3000); // reset collision after a delay to allow re-triggering
}
}
}
function fetchQuestion() {
const apiUrl = `https://3k722ttd40.execute-api.us-west-1.amazonaws.com/prod/question?questionID=${currentQuestionId}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
askQuestion(data.question, data.answer);
currentQuestionId = (currentQuestionId % 29) + 1; // loop back to 1 after 29
})
.catch(error => {
console.error('Error fetching question:', error);
});
}
function askQuestion(question, correctAnswer) {
const userAnswer = prompt(question);
if (userAnswer && userAnswer.toUpperCase() === correctAnswer.toUpperCase()) {
console.log("Correct answer!");
score += 10; // add the score
} else {
console.log("Wrong answer. The correct answer was: ", correctAnswer);
setTimeout(() => location.reload(), 1000)
}
drawScore();
}
var obstacles = [];
var sanitizers = [];
var masks = [];
function populateGameElements() {
setInterval(() => {
// generate new elements at rand, off the right side
obstacles.push(new Obstacle(obstacleImage, canvas.width + Math.random() * 700, canvas.height - 150, 50, 50));
sanitizers.push(new Sanitizer(sanitizerImage, canvas.width + Math.random() * 1000, 550, 28, 42));
masks.push(new Mask(maskImage, canvas.width + Math.random() * 1500, 470, 48, 27));
}, 4000); // every 4000, can change
}
function drawGround() {
ctx.fillStyle = "green";
ctx.fillRect(0, canvas.height - 100, canvas.width, 100);
}
function drawScore() {
ctx.clearRect(0, 0, canvas.width, 50); // clear the score area
ctx.font = "26px Arial";
ctx.fillStyle = "#0081bf";
ctx.fillText("Score: " + score, 20, 30);
}
function isDead() {
console.log("Game Over! Score: " + score);
setTimeout(() => location.reload(), 500); // restart the game after 2 seconds
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // clear the canvas
drawGround();
player.show();
player.update();
// display + update obstacles, sanitizers, masks
obstacles.forEach((obstacle, index) => {
if (obstacle.x + obstacle.width < 0) {
obstacles.splice(index, 1); // remove off-screen obstacles
} else {
obstacle.show();
obstacle.update();
}
});
sanitizers.forEach((sanitizer, index) => {
if (sanitizer.x + sanitizer.width < 0) {
sanitizers.splice(index, 1); // remove off-screen sanitizers
} else {
sanitizer.show();
sanitizer.update();
}
});
masks.forEach((mask, index) => {
if (mask.x + mask.width < 0) {
masks.splice(index, 1); // remove off-screen masks
} else {
mask.show();
mask.update();
}
});
drawScore(); // draw the score
requestAnimationFrame(gameLoop); // loop
}
// keyboard controls
document.addEventListener('keydown', function (event) {
if (event.key === 'ArrowUp' && canJump) {
player.ySpeed = -15;
canJump = false;
}
});
populateGameElements(); // populate game elements
gameLoop(); // game loop