-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (169 loc) · 5.87 KB
/
index.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
194
195
196
197
198
199
200
201
202
203
204
205
206
const ship = document.querySelector("#ship");
const bulletsContainer = document.querySelector("#bullets");
const obstaclesContainer = document.querySelector("#obstacles");
const playerScore = document.getElementById("score");
let score = 0;
let shipLeft;
let bulletTopPos = 700;
let obstIntervalCreat;
let goSound = new Audio("game-over-arcade-6435.mp3");
let startSound = new Audio("game-music-loop-7-145285.mp3");
// to move the space ship
window.addEventListener("mousemove", function (movement) {
shipLeft = Math.min(window.innerWidth - ship.offsetWidth, movement.x);
ship.style.left = shipLeft + "px";
});
//creat shooting with space botton
window.addEventListener("keydown", function (press) {
// making sure that the space botton was pressed
if (press.key === " ") {
//calling the fire function
fire();
}
});
window.addEventListener("load", function () {
//load means when the game starts
loadObs();
});
function fire() {
const shipLocation = ship.getBoundingClientRect();
const newBullet = document.createElement("div");
newBullet.className = "bullet";
bulletsContainer.appendChild(newBullet);
newBullet.style.display = "block";
newBullet.style.left =
shipLocation.left + shipLocation.width / 2 - 2.5 + "px";
// newBullet.style.top = bulletTop + "px";
let bulletTop = shipLocation.top;
// move bullets...
let timer = setInterval(() => {
bulletTop -= 20;
newBullet.style.top = bulletTop + "px";
//distroy the bullets
if (bulletTop < 0) {
newBullet.remove();
//clear the interval
clearInterval(timer);
}
ifCollision(newBullet);
}, 50);
}
function loadObs() {
let obstacles = document.querySelectorAll(".obstacle");
obstIntervalCreat = setInterval(() => {
if (obstacles.length < 5) {
creatObstacles();
}
ifCollisionWithShip();
}, 1000);
}
function creatObstacles() {
if(creatObstacles){
startSound.play();
}
const newObstacle = document.createElement("div");
newObstacle.className = "obstacle";
newObstacle.style.left = Math.random() * (window.innerWidth - 50) + "px";
//newObstacle.style.bottom = "100%";
newObstacle.style.display = "block";
obstaclesContainer.appendChild(newObstacle);
let obstacleTop = 0;
let speed = 1;
let obstimer = setInterval(() => {
speed = Math.floor(Math.random() * (80 - 5 + 1));
obstacleTop = obstacleTop + speed;
newObstacle.style.top = obstacleTop + "px";
//distroy the bullets
if (obstacleTop >= 700) {
newObstacle.remove();
//clear the interval
clearInterval(obstimer);
}
}, 70);
setInterval(ifCollisionWithShip, 50);
}
function ifCollision(bullet) {
const bulletPosition = bullet.getBoundingClientRect();
const allObstacles = document.querySelectorAll(".obstacle");
allObstacles.forEach((obstacle) => {
const obstaclePosition = obstacle.getBoundingClientRect();
//collision conditios
if (
bulletPosition.top < obstaclePosition.bottom &&
bulletPosition.bottom > obstaclePosition.top &&
bulletPosition.left < obstaclePosition.right &&
bulletPosition.right > obstaclePosition.left
) {
//removing the bullet and the obstacle
bullet.remove();
obstacle.remove();
triggerExplosion(obstacle);
score++;
playerScore.textContent = "your score : " + score;
}
});
}
function triggerExplosion(obstacle) {
const explosion = document.createElement("div");
explosion.className = "explosion";
explosion.style.left = obstacle.style.left;
explosion.style.top = obstacle.style.top;
//i hade to append it to the body so that it will show
document.body.appendChild(explosion);
setTimeout(() => {
explosion.remove();
}, 500);
}
function ifCollisionWithShip() {
const shipPosition = ship.getBoundingClientRect();
const allObstacles = document.querySelectorAll(".obstacle");
allObstacles.forEach((obstacle) => {
const obstaclePosition = obstacle.getBoundingClientRect();
if (
shipPosition.top < obstaclePosition.bottom &&
shipPosition.bottom > obstaclePosition.top &&
shipPosition.left < obstaclePosition.right &&
shipPosition.right > obstaclePosition.left
) {
gameOver();
}
});
}
function gameOver() {
if (gameOver) {
startSound.pause();
goSound.play();
}
const gameOverScreen = document.getElementById("game-over-wrapper");
gameOverScreen.classList.remove("hidden");
const finalScore = document.getElementById("final-score");
finalScore.textContent = `your score is: ${score}`;
clearInterval(obstIntervalCreat);
// hide the elements ship and obstacles
document.querySelector("#ship").style.display = "none";
document
.querySelectorAll(".obstacle")
.forEach((obstacle) => (obstacle.style.display = "none"));
document
.querySelectorAll(".bullet")
.forEach((bullet) => (bullet.style.display = "none"));
const restartButton = document.getElementById("restart-button");
restartButton.addEventListener("click", restartGame);
}
function restartGame() {
score = 0;
playerScore.textContent = "Your score: 0";
loadObs();
if(restartGame){
startSound.play();
}
// Show the ship
const ship = document.querySelector("#ship");
ship.style.display = "block";
// Clear all obstacles and bullets
obstaclesContainer.innerHTML = "";
bulletsContainer.innerHTML = "";
// Hide the game-over screen
const gameOverScreen = document.getElementById("game-over-wrapper");
gameOverScreen.classList.add("hidden");
}