-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
212 lines (175 loc) · 6.49 KB
/
app.js
File metadata and controls
212 lines (175 loc) · 6.49 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const scoreDiv = document.querySelector(".score");
const score = document.querySelector(".current-score");
const highScore = document.querySelector(".high-score");
const gameArea = document.querySelector(".game-area");
const startScreen = document.querySelector(".start-screen");
// ********************* GAME SOUNDS ********************
let carIgnitionSound = new Audio("game-sounds/mixkit-car-ignition-1535.wav");
let carRunningSound = new Audio("game-sounds/mixkit-passing-car-and-urban-ambience-1554.wav");
let carCrashSound = new Audio("game-sounds/mixkit-car-explosion-debris-1562.wav");
carIgnitionSound.play();
// **************** PAUSE-SCREEN ******************
const pauseDiv = document.createElement("div");
pauseDiv.classList.add("hide");
pauseDiv.textContent = "Game is paused";
pauseDiv.classList.add("pause-screen");
gameArea.appendChild(pauseDiv);
let hsVal = localStorage.getItem("highScoreVal");
if (hsVal === null) {
localStorage.setItem("highScoreVal", 0);
}
highScore.textContent = "High-Score : " + localStorage.getItem("highScoreVal");
let keys = { ArrowUp: false, ArrowDown: false, ArrowLeft: false, ArrowRight: false };
let player = { speed: 10 };
let lastPaintTime = 0;
let lastScore = 0;
let pauseGame = false;
startScreen.addEventListener("click", start);
document.addEventListener("keydown", keyDown);
document.addEventListener("keyup", keyUp);
function keyDown(e) {
keys[e.key] = true;
}
function keyUp(e) {
keys[e.key] = false;
}
function collide(playerCar, opponentCar) {
pr = playerCar.getBoundingClientRect();
or = opponentCar.getBoundingClientRect();
return !((pr.bottom < or.top) || (pr.top > or.bottom) || (pr.left > or.right) || (pr.right < or.left))
}
// ***************** MOVING THE ROAD LINES FUNCTION *******************
function moveLines() {
let lines = document.querySelectorAll(".road-line");
lines.forEach((item) => {
if (item.y > 900) {
item.y -= 900;
}
item.y += player.speed;
item.style.top = item.y + "px";
})
}
// ***************** MOVING THE ENEMY CARS FUNCTION ******************
function moveEnemyCars(myCar) {
let enemyCars = document.querySelectorAll(".enemy-car-div");
enemyCars.forEach(function (car) {
// Use the below line to generate constantly colour changing cars ...
// car.style.backgroundColor = "rgb(" + generateRandomColour() + ")";
if (collide(myCar, car)) {
carCrashSound.play();
carRunningSound.pause();
player.start = false;
startScreen.textContent = "Game Over"
startScreen.style.top = "15rem";
startScreen.classList.remove("hide");
if (player.score > hsVal) {
localStorage.setItem("highScoreVal", player.score);
highScore.textContent = "High-Score : " + player.score;
}
}
if (car.y > 900) {
car.y -= 900;
car.style.left = Math.floor(Math.random() * 580) + "px";
}
car.y += player.speed;
car.style.top = car.y + "px";
})
}
function gameplay(ctime) {
let car = document.querySelector(".car-div");
let roadDimmension = gameArea.getBoundingClientRect();
if (player.start) {
carRunningSound.play();
// ************* MOVING THE ROAD LINES **************
moveLines();
// ************* MOVING THE ENEMY CARS **************
moveEnemyCars(car);
if (keys.ArrowUp && player.y > 150) {
player.y -= player.speed;
}
if (keys.ArrowDown && player.y < (roadDimmension.bottom - 120)) {
player.y += player.speed;
} if (keys.ArrowLeft && player.x > 0) {
player.x -= player.speed;
} if (keys.ArrowRight && player.x < (roadDimmension.width - 78)) {
player.x += player.speed;
}
car.style.top = player.y + "px";
car.style.left = player.x + "px";
if (ctime - lastPaintTime > 300) {
player.score++;
lastPaintTime = ctime;
}
score.textContent = "Score : " + player.score;
if (player.score - lastScore > 100) {
player.speed += 2;
lastScore = player.score;
}
if (pauseGame === false) {
window.requestAnimationFrame(gameplay);
}
else {
carRunningSound.pause();
}
}
}
// ******************* START GAME *****************
function start() {
carIgnitionSound.pause();
startScreen.classList.add("hide");
gameArea.innerHTML = "";
gameArea.appendChild(pauseDiv);
player.start = true;
// ************** ROAD LINES ***************
for (let i = 0; i < 6; i++) {
let roadLine = document.createElement("div");
roadLine.classList.add("road-line");
roadLine.y = i * 180;
roadLine.style.top = roadLine.y + "px";
gameArea.appendChild(roadLine);
}
// ************** PLAYER CAR ***************
let car = document.createElement("div");
car.classList.add("car-div");
gameArea.appendChild(car);
// ************** ENEMY CARS ***************
for (let i = 0; i < 3; i++) {
let enemyCar = document.createElement("div");
enemyCar.classList.add("enemy-car-div");
enemyCar.style.backgroundColor = "rgb(" + generateRandomColour() + ")";
enemyCar.y = i * 300;
enemyCar.style.top = enemyCar.y + "px";
enemyCar.style.left = Math.floor(Math.random() * 580) + "px";
gameArea.appendChild(enemyCar);
}
player.x = car.offsetLeft;
player.y = car.offsetTop;
// ************* INITIALIZING THE SCORE ***********
player.score = 0;
scoreDiv.style.height = "12rem";
window.requestAnimationFrame(gameplay);
}
// *************** WHEN GAME IS PAUSED **************************
document.addEventListener("keydown", (e) => {
if (e.key === " ") {
if (pauseGame === true) {
pauseGame = false;
console.log("Pause is false");
pauseDiv.classList.add("hide");
gameplay();
}
else if (pauseGame === false) {
pauseGame = true;
console.log("Pause is true");
pauseDiv.classList.remove("hide");
}
}
})
// ************** GENERATE RANDOM COLOURS FOR ENEMY CARS **************
function generateRandomColour() {
let r = Math.ceil(Math.random() * 256);
let g = Math.ceil(Math.random() * 256);
let b = Math.ceil(Math.random() * 256);
let rgb = r + "," + b + "," + g;
return (rgb);
}