-
Notifications
You must be signed in to change notification settings - Fork 4
/
cabbie.js
141 lines (113 loc) · 3.71 KB
/
cabbie.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const score=document.querySelector('.score');
const startScreen= document.querySelector('.startScreen');
const gameArea=document.querySelector('.gameArea');
// console.log(startScreen);
startScreen.addEventListener('click', start);
let player={ speed: 5, score: 0};
let keys = {ArrowUp :false,ArrowDown : false,ArrowRight :false,ArrowLeft:false}
document.addEventListener('keydown',keyDown);
document.addEventListener('keyup', keyUp);
// e.key is inbuilt js to imply which key has been pressed by the user
//after click of a key
function keyDown(e){
e.preventDefault();
keys[e.key]=true;
// console.log(e.key);
// console.log(keys);
}
// after release
function keyUp(e){
e.preventDefault();
keys[e.key]=false;
// console.log(e.key);
// console.log(keys);
}
function isCollide(a,b){
aRect=a.getBoundingClientRect();
bRect=b.getBoundingClientRect();
return!(aRect.bottom< bRect.top||aRect.top> bRect.bottom||aRect.left> bRect.right||aRect.right< bRect.left);
}
function moveLines(){
let lines=document.querySelectorAll('.lines');
lines.forEach(function(item){
if(item.y >=700){
item.y -= 750;
}
item.y+= player.speed;
item.style.top=item.y + "px";
})
}
function endGame(){
player.start=false;
startScreen.classList.remove('hide');
}
function moveEnemy(car){
let enemy=document.querySelectorAll('.enemyCar');
enemy.forEach(function(item){
if(isCollide(car , item )){
// console.log("boom");
endGame();
}
if(item.y >=750){
item.y = -300;
item.style.left= Math.floor(Math.random() * 350) + "px";
}
item.y+= player.speed;
item.style.top=item.y + "px";
})
}
function gameSound(){
var audio=new Audio('Art-Of-Silence_V2.mp3');
var volume= audio.volume;
volume=volume- 0.45 ;
audio.play();
}
function gamePlay(){
// <embed src="/html/BattleMetal-320bit.mp3" loop="true" autostart="true" width="2" height="0"></embed>
let car=document.querySelector('.car');
let road = gameArea.getBoundingClientRect();
if(player.start){
gameSound();
moveLines();
moveEnemy(car);
if(keys.ArrowUp && player.y> (road.top + 50)){player.y -= player.speed}
if(keys.ArrowDown && player.y< (road.bottom - 70) ){player.y += player.speed}
if(keys.ArrowLeft && player.x >0 ){player.x -= player.speed}
if(keys.ArrowRight && player.x < (road.width-50)){player.x += player.speed}
car.style.top=player.y + "px";
car.style.left=player.x + "px";
window.requestAnimationFrame(gamePlay);
// console.log(player.score++);
player.score++
score.innerText="Score: " +player.score;
}
}
function start(){
// for the animation of the game
// gameArea.classList.remove('hide');
startScreen.classList.add('hide');
gameArea.innerHTML= "";
player.start=true;
player.score=0;
window.requestAnimationFrame(gamePlay);
for(x=0;x<5;x++){
let roadline =document.createElement('div');
roadline.setAttribute('class','lines')
roadline.y=(x*150);
roadline.style.top= roadline.y+ "px";
gameArea.appendChild(roadline);}
let car= document.createElement('div');
car.setAttribute('class' , 'car');
// car.innerText('hie there');
gameArea.appendChild(car);
player.x=car.offsetLeft;
player.y=car.offsetTop;
for(x=0;x<3;x++){
let enemyCar =document.createElement('div');
enemyCar.setAttribute('class','enemyCar')
enemyCar.y=((x+1)*350) * -1;
enemyCar.style.top= enemyCar.y+ "px";
// enemyCar.style.background ='cadetblue';
enemyCar.style.left= Math.floor(Math.random() * 350) + "px";
gameArea.appendChild(enemyCar);}
}