-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (40 loc) · 1.78 KB
/
main.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
//---Ayush Jaiswal
var blueCar = document.getElementById("bluecar");
var raceCar = document.getElementById("racecar");
var result = document.getElementById("result")
const score = document.getElementById("score")
var game = document.getElementById("game");
var counter = 0;
var jumpsound = document.getElementById("jumpsound")
///////////////////////////////////////////////////////////////Now DEFINE MOVES //////////////////////////////////////////////////////////
// bluecar move
blueCar.addEventListener("animationiteration", function(){
var random = ((Math.floor(Math.random() * 3)) * 130)
blueCar.style.left = random + "px";
counter++
})
//rececar move
window.addEventListener("keydown", function(e){
if(e.keyCode == "39"){ var raceCarLeft = parseInt(window.getComputedStyle(raceCar).getPropertyValue("left"))
if(raceCarLeft < 260){raceCar.style.left = (raceCarLeft + 130) + "px"}
jumpsound.play()
};
if(e.keyCode == "37"){
var raceCarLeft = parseInt(window.getComputedStyle(raceCar).getPropertyValue("left"))
if(raceCarLeft > 0){raceCar.style.left = (raceCarLeft - 130) + "px"
jumpsound.play()
}
}
})
//Game over
setInterval(function Gameover (){
var blueCarTop = parseInt(window.getComputedStyle(blueCar).getPropertyValue("top"))
var blueCarLeft = parseInt(window.getComputedStyle(blueCar).getPropertyValue("left"));
var raceCarLeft = parseInt(window.getComputedStyle(raceCar).getPropertyValue("left"));
if((blueCarLeft === raceCarLeft) && (blueCarTop > 250) && (blueCarTop < 450)){
result.style.display = "block";
game.style.display = "none";
score.innerHTML = `score: ${counter} `;
counter = 0;
}
}, 10)