-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (41 loc) · 1.33 KB
/
script.js
File metadata and controls
48 lines (41 loc) · 1.33 KB
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
const player = document.getElementById("player");
const obstacle = document.getElementById("obstacle");
document.addEventListener("keydown", function(event) {
if (event.key === "ArrowUp") {
if (!player.classList.contains("jump")) {
player.classList.add("jump");
setTimeout(() => {
player.classList.remove("jump");
}, 500);
}
}
if (event.key === "ArrowDown") {
player.classList.add("duck");
}
});
document.addEventListener("keyup", function(event) {
if (event.key === "ArrowDown") {
player.classList.remove("duck");
}
});
function checkCollision() {
const playerRect = player.getBoundingClientRect();
const obstacleRect = obstacle.getBoundingClientRect();
if (
playerRect.right > obstacleRect.left &&
playerRect.left < obstacleRect.right &&
playerRect.bottom > obstacleRect.top &&
playerRect.top < obstacleRect.bottom
) {
document.getElementById("gameOver").style.display = "block";
obstacle.style.animation = "none";
clearInterval(collisionInterval);
}
}
function restartGame() {
obstacle.style.animation = "moveObstacle 2s linear infinite";
document.getElementById("gameOver").style.display = "none";
// Reset score, position, etc. here if needed
collisionInterval = setInterval(checkCollision, 10);
}
let collisionInterval = setInterval(checkCollision, 10);