forked from manoharys/Car-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
193 lines (168 loc) · 5.14 KB
/
app.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const score = document.querySelector('.score');
const gameStart = document.querySelector(".gameStart");
const gameArea = document.querySelector(".gameArea");
const body = document.querySelector('body');
const end = document.querySelector('.end');
//sounds
let drive = new Audio('./audio/drive.mp3');
let crash = new Audio('./audio/crash.mp3');
//crash.pause();
//images
let carsImg = ['./images/E1.png', './images/E2.jpg', './images/E3.jpg'];
//Object which init game play
let player = {
speed: 5,
score: 0
};
//Object which tracks the keyPresses
let keys = {
ArrowUp: false,
ArrowRight: false,
ArrowDown: false,
ArrowLeft: false
}
//Welcome window
gameStart.addEventListener('click', start);
//KeyPress Event
window.document.addEventListener('keydown', pressOn);
window.document.addEventListener('keyup', pressOff);
//functions which tracks the keypress events
function pressOn(event) {
keys[event.key] = true;
//console.log(keys);
}
function pressOff(event) {
keys[event.key] = false;
//console.log(keys);
}
//function which starts the game Play
function start() {
gameArea.innerHTML = "";
//gameArea.classList.remove('hide');
gameStart.classList.add('hide');
player.start = true;
for (let i = 0; i < 10; i++) { //RoadLines
let div = document.createElement('div');
div.classList.add("line");
div.y = (i * 150);
div.style.top = (i * 150) + 'px';
gameArea.appendChild(div);
}
window.requestAnimationFrame(playGame);
let car = document.createElement('div');
car.setAttribute("class", "car");
gameArea.appendChild(car);
player.x = car.offsetLeft;
player.y = car.offsetTop;
//console.log(car.getBoundingClientRect());
for (let i = 0; i < 5; i++) { //Enemy cars
let enemy = document.createElement('div');
enemy.classList.add("enemy");
enemy.y = ((i + 1) * 600) * -1;
enemy.style.top = enemy.y + "px";
enemy.style.left = Math.floor(Math.random() * 150) + "px";
enemy.style.backgroundColor = 'black'
enemy.style.backgroundImage = `url(${carsImg[Math.floor(Math.random()*3)]})`;
gameArea.appendChild(enemy);
}
}
//function which starts the game play
function playGame() {
//console.log("inplay");
drive.play();
let car = document.querySelector(".car");
let road = gameArea.getBoundingClientRect();
enemyCars(car);
moveLines();
score.style.display = "block";
//console.log("x="+player.x);
//console.log("y="+player.y);
//console.log(road);
if (player.start) {
if (keys.ArrowUp && (player.y > (road.top = -250))) {
player.y -= player.speed;
}
if (keys.ArrowDown && player.y < road.bottom) {
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.left = player.x + 'px';
car.style.top = player.y + 'px';
player.score++;
score.innerText = "Score : " + player.score;
window.requestAnimationFrame(playGame);
}
}
//Function which moves lines
function moveLines() {
let lines = document.querySelectorAll('.line');
lines.forEach(function (item) {
//console.log("y=" + item.y);
if (item.y >= 750) {
item.y -= 750;
}
item.y += player.speed;
item.style.top = item.y + 'px';
})
}
//Function which moves enemy cars
function enemyCars(car) {
let ele = document.querySelectorAll(".enemy");
ele.forEach(function (item) {
if (isCollide(car, item)) {
//console.log('hit');
drive.pause();
crash.play();
endGame();
}
})
ele.forEach(function (item) {
if (item.y >= 1500) {
item.y = -600;
item.style.left = Math.floor(Math.random() * 300) + "px";
}
item.y += player.speed;
item.style.top = item.y + "px";
})
}
//Function which checks the collision detection
function isCollide(a, b) {
let aRect = a.getBoundingClientRect();
let bRect = b.getBoundingClientRect();
return !(
(aRect.top > bRect.bottom) ||
(aRect.bottom < bRect.top) ||
(aRect.left > bRect.right) ||
(aRect.right < bRect.left)
)
}
//function which ends the game play and displays the score
function endGame() {
player.start = false;
endGameStyles();
}
//function which displays the score and restarts the gameplay
function endGameStyles() {
end.classList.remove('hide');
end.classList.add('gameOver');
end.innerHTML = "Game over!! " + "<br>" + "your score : " + player.score;
crash.currentTime = 5;
setTimeout(function () {
drive.currentTime = 100;
end.classList.add('hide');
gameStart.classList.remove('hide');
}, 1500);
}
//function which return the random colors
function randomColors() {
function color() {
let c = Math.floor(Math.random() * 256);
return c;
}
return "rgb(" + color() + "," + color() + "," + color() + ")";
}