-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
162 lines (137 loc) · 4.06 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
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
const score = document.querySelector(".score");
const startScreen = document.querySelector(".startScreen");
const gameArea = document.querySelector(".gameArea");
startScreen.addEventListener("click", start);
let player = { speed: 5, score: 0, carspeed: 6 };
let keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false,
};
var backgroundmusic = new Audio();
backgroundmusic.src = "pendulum.mp3";
document.addEventListener("keydown", keyDown);
document.addEventListener("keyup", keyUp);
function keyDown(e) {
e.preventDefault();
keys[e.key] = true;
//console.log(e.key);
console.log(keys);
}
function keyUp(e) {
e.preventDefault();
keys[e.key] = false;
// console.log(keys);
}
function isCollide(a, b) {
aRect = a.getBoundingClientRect();
bRect = b.getBoundingClientRect();
return !(
aRect.top > bRect.bottom ||
aRect.bottom < bRect.top ||
aRect.right < bRect.left ||
aRect.left > bRect.right
);
}
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");
startScreen.style.background = "#900";
backgroundmusic.pause();
startScreen.innerHTML = "FAILED <br> click to try again";
}
function moveEnemy(car) {
let enemy = document.querySelectorAll(".enemy");
enemy.forEach(function (item) {
if (isCollide(car, item)) {
console.log("HITTT");
var music = new Audio();
music.src = "crash.mp3";
music.play();
endGame();
}
if (item.y >= 750) {
item.y = -300;
item.style.left = Math.floor(Math.random() * 350) + "px";
}
//item.y += player.carspeed;
item.y += Math.floor(Math.random() * 7 + 5);
item.style.top = item.y + "px";
});
}
function gamePlay() {
let car = document.querySelector(".car");
let road = gameArea.getBoundingClientRect();
if (player.start) {
moveLines();
moveEnemy(car);
if (keys.ArrowUp && player.y > road.top) {
player.y -= player.speed;
}
if (keys.ArrowDown && player.y < road.bottom - 90) {
player.y += player.speed;
}
if (keys.ArrowLeft && player.x > 0) {
player.x -= player.speed;
}
if (keys.ArrowRight && player.x < road.width - 90) {
player.x += player.speed;
}
car.style.top = player.y + "px";
car.style.left = player.x + "px";
window.requestAnimationFrame(gamePlay);
// console.log("Score is" + player.score++);
console.log(score.innerText);
player.score++;
score.innerHTML = "Score = " + player.score;
}
}
function start() {
backgroundmusic.play();
//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");
gameArea.appendChild(car);
player.x = car.offsetLeft;
player.y = car.offsetTop;
// console.log("offset top is" + player.y)
for (x = 0; x < 3; x++) {
let enemyCar = document.createElement("div");
enemyCar.setAttribute("class", "enemy");
enemyCar.y = (x + 1) * 350 * -1;
enemyCar.style.top = enemyCar.y + "px";
enemyCar.style.backgroundColor = randomColor();
enemyCar.style.left = Math.floor(Math.random() * 350) + "px";
gameArea.appendChild(enemyCar);
}
function randomColor() {
function c() {
let hex = Math.floor(Math.random() * 256).toString(16);
return ("0" + String(hex)).substr(-2);
}
return "#" + c() + c() + c();
}
}