-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (52 loc) · 1.76 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var character = document.getElementById('character');
var block = document.getElementById('block');
var scorebox = document.getElementById('score');
var highscorebox = document.getElementById('Highscore');
let hiscoreval = 0;
let score = 0;
var start = document.getElementById('start');
let intervalId;
let intervalId2;
window.addEventListener('keydown', function(event) {
if (event.code === 'ArrowUp' || event.code === 'Space') {
console.log('Arrow up or Space key pressed!');
character.classList.add("animate");
intervalId2 = this.setTimeout(()=>{
character.classList.remove('animate');
},500)
}
});
// To Start Button
window.addEventListener('click',function(){
block.classList.add('animateblock');
intervalId = setInterval(() => {
score++;
scorebox.innerHTML = "Score : "+score;
if(score>hiscoreval){
hiscoreval = score;
localStorage.setItem("hiscore", JSON.stringify(hiscoreval));
highscorebox.innerHTML = "High Score: " + hiscoreval;
}
}, 300);
});
// check for Game Over
var checkdead = setInterval(function(){
var charactertop = parseInt(window.getComputedStyle(character).getPropertyValue("top"));
var blockleft = parseInt(window.getComputedStyle(block).getPropertyValue("left"));
if(blockleft<80 && blockleft>0 && charactertop>=180){
block.classList.remove('animateblock');
alert("Game Over. Score is " +score+ ".");
score = 0;
scorebox.innerHTML = "Score : "+score;
clearInterval(intervalId,intervalId2);
// location.reload()
}
},10);
let hiscore = localStorage.getItem("hiscore");
if(hiscore === null){
localStorage.setItem("hiscore", JSON.stringify(hiscoreval))
}
else{
hiscoreval = JSON.parse(hiscore);
highscorebox.innerHTML = "High Score: " + hiscore;
}