-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (40 loc) · 1.34 KB
/
script.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
var player = document.getElementById("player");
var block = document.getElementById("block");
var score = 0;
var highScore = 0;
var isPlayerDead;
var restartButton = document.getElementById("restartButton");
scoreDisplayer.value = "SCORE:" + score;
setInterval(function(){
var rect1 = player.getBoundingClientRect();
var rect2 = block.getBoundingClientRect();
if (rect1.x < rect2.x + rect2.width && //if block collides with player
rect2.x < rect1.x + rect1.width &&
rect1.y < rect2.y + rect2.height &&
rect2.y < rect1.y + rect1.height) {;
player.style.display="none";
isPlayerDead = "true";
}
})
addEventListener("keypress",function(){
var pressedKey = event.which || event.keyCode();
if(block.classList.contains("blockStartMoving") == false) {
block.classList.add("blockStartMoving");
} else {
if (pressedKey == 32) { //if user presses SPACE
if (player.classList.contains("playerJumping") == false) {
player.classList.add("playerJumping");
setTimeout(function(){
player.classList.remove("playerJumping");
if (isPlayerDead == "true") {} else {
scoreDisplayer.value = "SCORE:" + score++;
}
},750)
}
document.getElementById("ingameText").style.opacity="0";
}
}
})
restartButton.addEventListener("click",function(){
location.reload();
})