-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarship.js
263 lines (223 loc) · 6.79 KB
/
starship.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
import {
ammoValue,
blastValue,
isPaused,
isGame,
blastExplosion,
laserShot,
score,
Scores
} from './game.js';
import {
keys,
onKeyDown,
onKeyUp
} from './keys.js';
import {
sound,
music,
darkMode
} from './nav-buttons.js';
import {
toReduceSound
} from './utils.js';
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let blinkInterval;
const starship = {
x: canvas.width / 2 - 20, // X-axis starting position
y: canvas.height - 60, // Y-axis starting position
width: 35,
height: 40,
color: 'lightblue',
dx: 5, // move step by X
dy: 5, // move step by Y
//drawinhg
flameWidth: 10,
flameHeight: 3,
flameColor: 'tomato',
widthFuselage: 10,
heightFuselage: 20,
colorFuselage: 'grey',
widthCockpit: 8,
heightCockpit: 10,
colorCockpit: 'blue',
hp: 2,
ammo: 20,
blast: 1
};
function drawStarshipFuselage() {
ctx.fillStyle = starship.colorFuselage;
ctx.fillRect( starship.x + ((starship.width / 2) - (starship.widthFuselage / 2)),
starship.y + starship.height / 2,
starship.widthFuselage,
starship.heightFuselage);
}
function drawStarshipCockpit() {
ctx.fillStyle = starship.colorCockpit;
ctx.fillRect( starship.x + ((starship.width / 2) - (starship.widthCockpit / 2)),
starship.y + starship.height / 2.5,
starship.widthCockpit,
starship.heightCockpit);
}
function drawStarshipFlame() {
ctx.fillStyle = starship.flameColor;
ctx.fillRect( starship.x + ((starship.width / 2) - (starship.flameWidth / 2)),
starship.y + starship.height,
starship.flameWidth,
starship.flameHeight);
}
function drawStarship() {
// let gradient = ctx.createLinearGradient(starship.x, starship.y, starship.x + starship.width, starship.y + starship.height);
// gradient.addColorStop(0, 'lightblue');
// gradient.addColorStop(1, 'blue');
// ctx.fillStyle = gradient;
ctx.beginPath();
ctx.fillStyle = starship.color;
ctx.moveTo( starship.x + starship.width / 2,
starship.y); // top
ctx.lineTo( starship.x,
starship.y + starship.height); // left
ctx.lineTo( starship.x + starship.width,
starship.y + starship.height); // right
ctx.closePath();
ctx.fill();
drawStarshipFuselage();
drawStarshipCockpit();
drawStarshipFlame();
}
// starshipstates
function startBlinkingHp() {
blinkInterval = setInterval(() => {
starship.color = starship.color === 'transparent' ? 'green' : 'transparent';
}, 75);
}
function stopBlinkingHp() {
clearInterval(blinkInterval);
starship.color = 'lightblue';
}
function startBlinkingAmmo() {
blinkInterval = setInterval(() => {
starship.color = starship.color === 'transparent' ? 'blue' : 'transparent';
}, 75);
}
function stopBlinkingAmmo() {
clearInterval(blinkInterval);
starship.color = 'lightblue';
}
function startBlinkingInvulnerable() {
blinkInterval = setInterval(() => {
starship.color = starship.color === 'transparent' ? 'red' : 'transparent';
}, 75);
}
function stopBlinkingInvulnerable() {
clearInterval(blinkInterval);
starship.color = 'lightblue';
}
///////////////////////////////////
function updateStarship() { // Update starship position based on key states
if (keys.ArrowLeft && starship.x > 0) {
starship.x -= starship.dx;
}
if (keys.ArrowRight && starship.x + starship.width < canvas.width) {
starship.x += starship.dx;
}
if (keys.ArrowUp && starship.y > 0) {
starship.y -= starship.dy;
}
if (keys.ArrowDown && starship.y + starship.height < canvas.height) {
starship.y += starship.dy;
starship.flameHeight = 1;
};
if (keys.ArrowRight || keys.ArrowLeft) {
starship.width = 28;
}
else { starship.width = 35; };
if (keys.ArrowUp) {
starship.flameHeight = 8;
starship.flameColor = 'red';
}
else if (keys.ArrowDown) {
starship.flameHeight = 2;
starship.flameColor = 'orange';
}
else {
starship.flameHeight = 3;
starship.flameColor = 'tomato';
};
}
export {
starship,
drawStarship,
startBlinkingHp,
stopBlinkingHp,
startBlinkingAmmo,
stopBlinkingAmmo,
startBlinkingInvulnerable,
stopBlinkingInvulnerable,
updateStarship,
onKeyDown,
onKeyUp
};
///////////////////////////////////////////////////////////////////////////////////////
//SHOTING
let bullets = []; // Array to store multiple bullets
function drawBullet(bullet) {
ctx.fillStyle = bullet.color;
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
function updateBullet(bullet, index) {
bullet.y -= bullet.speed;
if (bullet.y + bullet.height < 0) { // If the bullet reaches the top of the screen
bullets.splice(index, 1); // Remove the bullet from the array
}
}
function Shot(event) {
if (isGame && !isPaused && keys.Space) {
if (event.key === ' ' && bullets.length < 5 && starship.ammo > 0) { // Check if the spacebar is pressed
const newBullet = {
width: 4,
height: 8,
x: starship.x + starship.width / 2 - 2,
y: starship.y,
color: 'blue',
speed: 10
};
if (sound) { toReduceSound(laserShot, 0.4) };
bullets.push(newBullet); // Add the new bullet to the array
starship.ammo--;
ammoValue.innerText = starship.ammo;
}
}
}
export {
bullets,
drawBullet,
updateBullet,
Shot
};
///////////////////////////////////////////////////////////////////////////////////////
// blast
function Blast(objects) {
if (isGame && !isPaused && starship.blast > 0) {
if (sound) {
blastExplosion.currentTime = 1.5;
blastExplosion.play()
};
for (let ob = objects.length - 1; ob >= 0; ob--) { // for all objects
const object = objects[ob];
Scores (object.pts); // score counter
}
objects.length = 0; // delete all obstacles
canvas.style.backgroundColor = 'white';
setTimeout(() => {
clearInterval(blinkInterval);
canvas.style.backgroundColor = darkMode ?
'var(--canvas-background-color-dark-mode)' :
'var(--canvas-background-color-light-mode)';
}, 2000);
};
}
export {
Blast
};