-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (77 loc) · 2.78 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
score=0
canjump = true
play = true
gamesound = new Audio('gamesound.mp3')
gameoversound = new Audio('gameoversound.mp3')
document.onkeydown= function(e){
setTimeout(() => {
if(play==true){
gamesound.play()
gameo= document.querySelector('.gameover')
gameo.style.visibility="hidden"
}
}, 200);
console.log(e.keyCode)
if(e.keyCode==38){
player1 = document.querySelector('.player')
player1.classList.add('animatedplayer')
setTimeout(() => {
player1.classList.remove('animatedplayer')
}, 500);
}
if(e.keyCode==39){
proplayer = document.querySelector('.player')
pxvalue= parseInt(window.getComputedStyle(proplayer,null).getPropertyValue('left'))
proplayer.style.left = pxvalue + 100 +"px"
}
if(e.keyCode==37){
proplayer = document.querySelector('.player')
pxvalue= parseInt(window.getComputedStyle(proplayer,null).getPropertyValue('left'))
proplayer.style.left = pxvalue - 100 +"px"
}
}
setInterval(() => {
Player = document.querySelector('.player')
Enemy = document.querySelector('.enemy')
px= parseInt(window.getComputedStyle(Player,null).getPropertyValue('left'))
ex= parseInt(window.getComputedStyle(Enemy,null).getPropertyValue('left'))
py= parseInt(window.getComputedStyle(Player,null).getPropertyValue('top'))
ey= parseInt(window.getComputedStyle(Enemy,null).getPropertyValue('top'))
diffX= Math.abs(ex-px)
diffY= Math.abs(ey-py)
if(diffX<50 && diffY<52){
Enemy.classList.remove('movingenemy')
gameelement = document.querySelector('.gameover')
gameelement.innerHTML= "Game Over"
gameelement.style.visibility="visible"
var points = document.querySelector('.points')
var textcon=points.textContent
var arrofvalue = textcon.split(":")
var realvalue =arrofvalue[1]
var text= "Your Score is "+realvalue
parent = document.querySelector('.container')
newdiv=document.createElement('div')
newdiv.innerHTML=text
newdiv.style.color="rgb(201, 46, 72)"
newdiv.style.textAlign="center"
newdiv.style.fontSize="2.5rem"
newdiv.style.fontWeight="bold"
newdiv.style.textShadow="2px 4px 4px rgba(216, 52, 161, 0.6)"
parent.appendChild(newdiv)
points.style.visibility="hidden"
gamesound.pause()
play = false
gameoversound.play()
}else if(diffX<130 && canjump){
score+=1
displayscore(score)
canjump = false
setTimeout(() => {
canjump = true
}, 1000);
}
}, 10);
function displayscore(score){
scoreelement = document.querySelector('.points')
scoreelement.innerHTML = "Score: "+ score
}