-
Notifications
You must be signed in to change notification settings - Fork 11
/
gameHandler.js
147 lines (121 loc) · 4.2 KB
/
gameHandler.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
const score = document.querySelector('.score');
const startScreen = document.querySelector('.startScreen');
const gameArea = document.querySelector('.gameArea');
const level = document.querySelector('.level');
// loading audio files
let gameStart = new Audio();
let gameOver = new Audio();
gameStart.src = "assets/audio/game_theme.mp3";
gameOver.src = "assets/audio/gameOver_theme.mp3";
const levelSpeed = {easy: 7, moderate: 10, difficult: 14};
let keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false
}
let player = { speed: 7, score: 0 };
level.addEventListener('click', (e)=> {
player.speed = levelSpeed[e.target.id];
});
startScreen.addEventListener('click', () => {
// gameArea.classList.remove('hide');
startScreen.classList.add('hide');
gameArea.innerHTML = "";
player.start = true;
gameStart.play();
gameStart.loop = true;
player.score = 0;
window.requestAnimationFrame(gamePlay);
for(let i=0; i<5; i++){
let roadLineElement = document.createElement('div');
roadLineElement.setAttribute('class', 'roadLines');
roadLineElement.y = (i*150);
roadLineElement.style.top = roadLineElement.y + "px";
gameArea.appendChild(roadLineElement);
}
let carElement = document.createElement('div');
carElement.setAttribute('class', 'car');
gameArea.appendChild(carElement);
player.x = carElement.offsetLeft;
player.y = carElement.offsetTop ;
for(let i=0; i<3; i++){
let enemyCar = document.createElement('div');
enemyCar.setAttribute('class', 'enemyCar');
enemyCar.y = ((i+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();
}
function onCollision(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 onGameOver() {
player.start = false;
gameStart.pause();
gameOver.play();
startScreen.classList.remove('hide');
startScreen.innerHTML = "Game Over <br> Your final score is " + player.score + "<br> Press here to restart the game.";
}
function moveRoadLines(){
let roadLines = document.querySelectorAll('.roadLines');
roadLines.forEach((item)=> {
if(item.y >= 700){
item.y -= 750;
}
item.y += player.speed;
item.style.top = item.y + "px";
});
}
function moveEnemyCars(carElement){
let enemyCars = document.querySelectorAll('.enemyCar');
enemyCars.forEach((item)=> {
if(onCollision(carElement, item)){
onGameOver();
}
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 gamePlay() {
let carElement = document.querySelector('.car');
let road = gameArea.getBoundingClientRect();
if(player.start){
moveRoadLines();
moveEnemyCars(carElement);
if(keys.ArrowUp && player.y > (road.top + 70)) player.y -= player.speed;
if(keys.ArrowDown && player.y < (road.bottom - 85)) player.y += player.speed;
if(keys.ArrowLeft && player.x > 0) player.x -= player.speed;
if(keys.ArrowRight && player.x < (road.width - 70)) player.x += player.speed;
carElement.style.top = player.y + "px";
carElement.style.left = player.x + "px";
window.requestAnimationFrame(gamePlay);
player.score++;
const ps = player.score - 1;
score.innerHTML = 'Score: ' + ps;
}
}
document.addEventListener('keydown', (e)=>{
e.preventDefault();
keys[e.key] = true;
});
document.addEventListener('keyup', (e)=>{
e.preventDefault();
keys[e.key] = false;
});