-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.js
280 lines (236 loc) · 6.97 KB
/
space.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Board size and position
let tileSize = 32;
let rows = 16;
let columns = 16;
let board;
let boardWidth = tileSize * columns; // 32 * 16
let boardHeight = tileSize * rows; // 32 * 16
let context;
// Ship size and position
let shipWidth = tileSize * 4;
let shipHeight = tileSize * 2;
let shipX = tileSize * columns / 2 - tileSize;
let shipY = tileSize * rows - tileSize * 3;
let ship = {
x: shipX,
y: shipY,
width: shipWidth,
heigth: shipHeight
}
let shipImg;
// Ship moving speed
let shipVelocityX = tileSize;
// Tweeters size and position
let tweeterArray = [];
let tweeterWidth = tileSize * 2;
let tweeterHeight = tileSize;
let tweeterX = tileSize;
let tweeterY = tileSize * 2;
let tweeterImg;
let tweeterRows = 2;
let tweeterColumns = 3;
// Number of tweeters to defet
let tweeterCount = 0;
// Tweeter moving speed
let tweeterVelocityX = 1;
// Number of bullets bullets
let bulletArray = [];
// Bullet moving speed
let bulletVelocityY = -10;
let score = 0;
let gameOver = false;
let gameStarted = false;
window.onload = function () {
// Select the board and set its dimensions
board = document.querySelector(".board");
board.width = boardWidth;
board.height = boardHeight;
context = board.getContext("2d"); // use for drawing on the board
// Load images and draw the ship
shipImg = new Image();
shipImg.src = "./public/imgs/musketeer.png"
shipImg.onload = function () {
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.heigth);
}
tweeterImg = new Image();
tweeterImg.src = "./public/imgs/tweeter.png";
createtweeters();
// Start the game loop
requestAnimationFrame(update);
// Add event listeners for user input
document.addEventListener("keydown", moveShip);
document.addEventListener("keyup", shoot);
}
// The update function is called on each animation frame
function update() {
requestAnimationFrame(update);
// Start the game on any key press
document.addEventListener("keydown", function () {
if (!gameStarted) {
gameStarted = true;
}
});
// Clear the canvas
context.clearRect(0, 0, board.width, board.height);
// Display a start message if the game hasn't started yet
if (!gameStarted) {
context.fillStyle = "white";
context.font = "20px Courier";
context.textAlign = "center";
context.fillText("- Press any key to start -", board.width / 2, board.height / 2);
return;
}
// Reset the game if it's over
if (gameOver) {
resetGame();
return;
}
// Draw the ship
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.heigth);
// Draw the tweeters
for (let i = 0; i < tweeterArray.length; i++) {
let tweeter = tweeterArray[i];
if (tweeter.alive) {
tweeter.x += tweeterVelocityX;
// Move all tweeters up one row
if (tweeter.x + tweeter.width >= board.width || tweeter.x <= 0) {
tweeterVelocityX *= -1;
tweeter.x += tweeterVelocityX * 2;
// Move all tweeters up one row
for (let j = 0; j < tweeterArray.length; j++) {
tweeterArray[j].y += tweeterHeight;
}
}
// Draw the tweeter
context.drawImage(tweeterImg, tweeter.x, tweeter.y, tweeter.width, tweeter.height);
// Check for collision with the ship
if (tweeter.y >= ship.y) {
gameOver = true;
resetGame();
}
}
}
// Draw the bullets
for (let i = 0; i < bulletArray.length; i++) {
let bullet = bulletArray[i];
bullet.y += bulletVelocityY;
context.fillStyle = "white";
context.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
// Check for collision between bullet and tweeter
for (let j = 0; j < tweeterArray.length; j++) {
let tweeter = tweeterArray[j];
if (!bullet.used && tweeter.alive && detectCollision(bullet, tweeter)) {
bullet.used = true;
tweeter.alive = false;
tweeterCount--;
score += 100;
}
}
}
// Clear the used bullets
while (bulletArray.length > 0 && (bulletArray[0].used || bulletArray[0].y < 0)) {
bulletArray.shift(); // removes the first element of the array
}
// Check if all tweeters have been defeated
if (tweeterCount == 0) {
// Increase the number of tweeters in columns and rows by 1
// Cap the columns at 6 and the rows at 12
tweeterColumns = Math.min(tweeterColumns + 1, columns / 2 - 2);
tweeterRows = Math.min(tweeterRows + 1, rows - 4);
// Increase the tweeter movement speed
tweeterVelocityX *= 1.2;
tweeterArray = [];
bulletArray = [];
// Create new tweeters
createtweeters();
}
// Display the score
context.fillStyle = " white";
context.font = "20px courier";
context.textAlign = "left";
context.fillText(score, 20, 30);
}
// Reset the game
function resetGame() {
// Display "Game Over" on the canvas
context.fillStyle = "white";
context.font = "20px Courier";
context.textAlign = "center";
context.fillText("- Game Over -", board.width / 2, board.height / 2);
// Listen for keydown event to reset the game
document.addEventListener("keydown", function () {
if (gameOver) {
gameOver = false;
gameStarted = false;
score = 0;
ship.x = shipX;
ship.y = shipY;
bulletArray = [];
tweeterArray = [];
tweeterRows = 2;
tweeterColumns = 3
tweeterCount = 0;
tweeterVelocityX = 1;
// Create new tweeters
createtweeters();
}
});
}
// Function to move the spaceship
function moveShip(e) {
// Reset game if it's over
if (gameOver) {
resetGame();
return;
}
// Check if the input arrow key is "ArrowLeft" and the spaceship's x position is greater than 0
if (e.code == "ArrowLeft" && ship.x - shipVelocityX >= 0) {
ship.x -= shipVelocityX;
// Check if the input arrow key is "ArrowRight" and the spaceship's x position + width is less than or equal to the board's width
} else if (e.code == "ArrowRight" && ship.x + shipVelocityX + ship.width <= board.width) {
ship.x += shipVelocityX;
}
}
// Function to create tweeters
function createtweeters() {
for (let c = 0; c < tweeterColumns; c++) {
for (let r = 0; r < tweeterRows; r++) {
let tweeter = {
img: tweeterImg,
x: tweeterX + c * tweeterWidth,
y: tweeterY + r * tweeterHeight,
width: tweeterWidth,
height: tweeterHeight,
alive: true
}
tweeterArray.push(tweeter);
}
}
tweeterCount = tweeterArray.length
}
// Function to shoot a bullet when space key is pressed
function shoot(e) {
// Reset game if it's over
if (gameOver) {
resetGame();
return;
}
// Check if the input key is the space key
if (e.code == "Space") {
let bullet = {
x: ship.x + shipWidth * 15 / 32,
y: ship.y,
width: tileSize / 8,
height: tileSize / 2,
used: false
}
bulletArray.push(bullet);
}
}
// Function to detect collision between two objects
function detectCollision(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}