-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (55 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
66
67
68
69
70
71
72
const dino = document.querySelector('.dino');
const background = document.querySelector('.background');
let isjumping = false
let position = 0
function handleKeyup(event) {
if(event.keyCode==32){
if(!isjumping){
jump()
}
}
};
function jump(){
isjumping = true
let upinterval = setInterval (() => {
if(position >= 150){
clearInterval(upinterval);
let downinterval = setInterval (() => {
if (position <=0){
clearInterval(downinterval);
isjumping=false
}else{
position -= 20
dino.style.bottom = position + 'px';
}
},20)
}else{
position += 20
dino.style.bottom = position + 'px';
}
},20 )
};
function createcactus (){
let cactusposition = 100;
const cactus = document.createElement('div')
let randomtime = Math.random()*6000;
cactus.style.right = 100 + 'px';
cactus.classList.add('cactus')
background.appendChild(cactus);
let rigthtinterval = setInterval(()=>{
if (cactusposition > 1400){
clearInterval(rigthtinterval);
background.removeChild(cactus);
}else if ( cactusposition > 0 && cactusposition == 1250 && position < 60){
clearInterval(rigthtinterval);
document.body.innerHTML = '<h1 class = "game-over ">!!!! GAME OVER !!!!</h1>'
}
else{
cactusposition += 10;
cactus.style.right = cactusposition + 'px';
}
},20)
setTimeout(createcactus,randomtime);
}
createcactus();
document.addEventListener('keyup',handleKeyup);