-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
272 lines (239 loc) · 8.28 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flappy Bird + Space Invaders (Night Version)</title>
<style>
body {
background-color: #000;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #0ff;
}
#soundtrackButton {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
background-color: #0ff;
color: #000;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<button id="soundtrackButton">Soundtrack: Off</button>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const soundtrackButton = document.getElementById('soundtrackButton');
// Game objects
const bird = {
x: 50,
y: canvas.height / 2,
width: 30,
height: 30,
velocity: 0,
gravity: 0.5,
jump: -8
};
const invaders = [];
let laser = null;
// Game variables
let score = 0;
let gameOver = false;
let frames = 0;
let countdown = 3;
let gameStarted = false;
// Audio
const jumpSound = new Audio('https://freesound.org/data/previews/341/341695_5858296-lq.mp3');
const laserSound = new Audio('https://freesound.org/data/previews/151/151022_1838182-lq.mp3');
const explosionSound = new Audio('https://freesound.org/data/previews/587/587183_7641788-lq.mp3');
const soundtrack = new Audio('https://freesound.org/data/previews/452/452641_5121236-lq.mp3');
soundtrack.loop = true;
let isSoundtrackPlaying = false;
soundtrackButton.addEventListener('click', () => {
if (isSoundtrackPlaying) {
soundtrack.pause();
soundtrackButton.textContent = 'Soundtrack: Off';
} else {
soundtrack.play();
soundtrackButton.textContent = 'Soundtrack: On';
}
isSoundtrackPlaying = !isSoundtrackPlaying;
});
// Star background
const stars = [];
for (let i = 0; i < 100; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 2
});
}
// Event listeners
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
if (gameOver) {
resetGame();
} else if (gameStarted) {
bird.velocity = bird.jump;
jumpSound.play();
}
}
});
document.addEventListener('mousedown', (e) => {
if (!gameOver && gameStarted && !laser) {
laser = { x: bird.x + bird.width, y: bird.y + bird.height / 2 };
laserSound.play();
}
});
document.addEventListener('mouseup', () => {
laser = null;
});
function resetGame() {
bird.y = canvas.height / 2;
bird.velocity = 0;
invaders.length = 0;
laser = null;
score = 0;
gameOver = false;
frames = 0;
countdown = 3;
gameStarted = false;
}
function createInvader() {
invaders.push({
x: canvas.width,
y: Math.random() * (canvas.height - 60),
width: 30,
height: 30,
speed: Math.random() * 2 + 1
});
}
function drawInvader(x, y, width, height) {
ctx.fillStyle = '#0f0';
ctx.fillRect(x, y, width, height);
// Eyes
ctx.fillStyle = '#f00';
ctx.fillRect(x + width * 0.2, y + height * 0.2, width * 0.2, height * 0.2);
ctx.fillRect(x + width * 0.6, y + height * 0.2, width * 0.2, height * 0.2);
// Mouth
ctx.fillRect(x + width * 0.1, y + height * 0.6, width * 0.8, height * 0.2);
}
function update() {
if (gameOver) return;
if (!gameStarted) {
if (frames % 60 === 0) {
countdown--;
if (countdown === 0) {
gameStarted = true;
}
}
frames++;
return;
}
frames++;
// Update bird
bird.velocity += bird.gravity;
bird.y += bird.velocity;
// Create new invaders
if (frames % 60 === 0) {
createInvader();
}
// Move invaders
invaders.forEach((invader, index) => {
invader.x -= invader.speed;
if (invader.x + invader.width < 0) {
invaders.splice(index, 1);
score++;
}
});
// Check laser collision
if (laser) {
invaders.forEach((invader, index) => {
if (laser.y > invader.y && laser.y < invader.y + invader.height &&
laser.x < invader.x + invader.width) {
invaders.splice(index, 1);
score += 5;
explosionSound.play();
}
});
}
// Check collisions
invaders.forEach(invader => {
if (collision(bird, invader)) {
gameOver = true;
explosionSound.play();
}
});
if (bird.y + bird.height > canvas.height || bird.y < 0) {
gameOver = true;
explosionSound.play();
}
}
function collision(rect1, rect2) {
return rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y;
}
function draw() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw stars
ctx.fillStyle = '#fff';
stars.forEach(star => {
ctx.fillRect(star.x, star.y, star.size, star.size);
});
// Draw bird
ctx.fillStyle = '#ff0';
ctx.fillRect(bird.x, bird.y, bird.width, bird.height);
// Draw invaders
invaders.forEach(invader => {
drawInvader(invader.x, invader.y, invader.width, invader.height);
});
// Draw laser
if (laser) {
ctx.strokeStyle = '#f00';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(bird.x + bird.width, bird.y + bird.height / 2);
ctx.lineTo(canvas.width, laser.y);
ctx.stroke();
}
// Draw score
ctx.fillStyle = '#fff';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
if (!gameStarted) {
ctx.fillStyle = '#fff';
ctx.font = '40px Arial';
ctx.fillText(`${countdown}`, canvas.width / 2 - 20, canvas.height / 2);
}
if (gameOver) {
ctx.fillStyle = '#fff';
ctx.font = '40px Arial';
ctx.fillText('Game Over', canvas.width / 2 - 100, canvas.height / 2);
ctx.font = '20px Arial';
ctx.fillText('Press Space to Restart', canvas.width / 2 - 100, canvas.height / 2 + 40);
}
}
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>