-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaxian.html
273 lines (239 loc) · 8.02 KB
/
galaxian.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
273
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Galaxian - Legendary 1 Bit Games</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
overflow: hidden;
}
canvas {
background-color: #111;
}
#rotate-message {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.9);
color: white;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 1000;
}
#rotate-message h2 {
font-size: 1.5rem;
padding: 1rem;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 10px;
}
</style>
</head>
<body>
<canvas id="galaxianCanvas" width="600" height="800"></canvas>
<div id="rotate-message">
<h2>Please rotate your device to landscape for the best experience.</h2>
</div>
<script>
const canvas = document.getElementById('galaxianCanvas');
const context = canvas.getContext('2d');
const player = {
x: canvas.width / 2 - 20,
y: canvas.height - 40,
width: 40,
height: 20,
speed: 7,
bullets: []
};
const enemies = [];
const enemySpeed = 1; // Slower enemies
const bulletSpeed = 5; // Slower bullets
const cooldownTime = 300; // 300ms shooting cooldown
let canShoot = true; // Shooting cooldown state
let leftPressed = false;
let rightPressed = false;
let spacePressed = false;
// Create enemies
function createEnemies() {
const rows = 4;
const cols = 6;
const spacingX = 80; // Increased horizontal spacing
const spacingY = 60; // Increased vertical spacing
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
enemies.push({
x: 50 + j * spacingX,
y: 50 + i * spacingY,
width: 40,
height: 20
});
}
}
}
// Draw player
function drawPlayer() {
context.fillStyle = 'white';
context.fillRect(player.x, player.y, player.width, player.height);
}
// Draw enemies
function drawEnemies() {
context.fillStyle = 'red';
enemies.forEach(enemy => {
context.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
});
}
// Draw bullets
function drawBullets() {
context.fillStyle = 'yellow';
player.bullets.forEach(bullet => {
context.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
}
// Move player
function movePlayer() {
if (leftPressed && player.x > 0) {
player.x -= player.speed;
}
if (rightPressed && player.x + player.width < canvas.width) {
player.x += player.speed;
}
}
// Move enemies
function moveEnemies() {
enemies.forEach(enemy => {
enemy.y += enemySpeed;
});
}
// Move bullets
function moveBullets() {
player.bullets.forEach(bullet => {
bullet.y -= bulletSpeed;
});
// Remove off-screen bullets
player.bullets = player.bullets.filter(bullet => bullet.y > 0);
}
// Handle shooting with cooldown
function shootBullet() {
if (spacePressed && canShoot) {
player.bullets.push({
x: player.x + player.width / 2 - 2,
y: player.y - 10,
width: 4,
height: 10
});
canShoot = false;
setTimeout(() => canShoot = true, cooldownTime); // Apply cooldown
}
}
// Check for collisions
function checkCollisions() {
for (let i = player.bullets.length - 1; i >= 0; i--) {
const bullet = player.bullets[i];
for (let j = enemies.length - 1; j >= 0; j--) {
const enemy = enemies[j];
if (
bullet.x < enemy.x + enemy.width &&
bullet.x + bullet.width > enemy.x &&
bullet.y < enemy.y + enemy.height &&
bullet.y + bullet.height > enemy.y
) {
// Collision detected, remove enemy and bullet
enemies.splice(j, 1);
player.bullets.splice(i, 1);
break;
}
}
}
}
// Game over detection
function checkGameOver() {
for (let enemy of enemies) {
if (enemy.y + enemy.height >= player.y) {
alert("Game Over!");
resetGame();
break;
}
}
}
// Reset game
function resetGame() {
player.bullets = [];
enemies.length = 0;
createEnemies();
}
// Main game loop
function gameLoop() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawEnemies();
drawBullets();
movePlayer();
moveEnemies();
moveBullets();
checkCollisions();
checkGameOver();
shootBullet();
requestAnimationFrame(gameLoop);
}
// Event listeners
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') leftPressed = true;
if (e.key === 'ArrowRight') rightPressed = true;
if (e.key === ' ') spacePressed = true;
});
document.addEventListener('keyup', (e) => {
if (e.key === 'ArrowLeft') leftPressed = false;
if (e.key === 'ArrowRight') rightPressed = false;
if (e.key === ' ') spacePressed = false;
});
// Handle touch events for mobile
let touchStartX = 0;
let touchEndX = 0;
document.addEventListener('touchstart', (e) => {
touchStartX = e.touches[0].clientX;
});
document.addEventListener('touchmove', (e) => {
touchEndX = e.touches[0].clientX;
const diffX = touchEndX - touchStartX;
if (diffX < -10) {
leftPressed = true;
rightPressed = false;
} else if (diffX > 10) {
rightPressed = true;
leftPressed = false;
}
});
document.addEventListener('touchend', () => {
leftPressed = false;
rightPressed = false;
});
// Orientation detection
function checkOrientation() {
const rotateMessage = document.getElementById('rotate-message');
if (window.innerHeight > window.innerWidth) {
rotateMessage.style.display = 'flex';
} else {
rotateMessage.style.display = 'none';
}
}
window.addEventListener('resize', checkOrientation);
window.addEventListener('orientationchange', checkOrientation);
document.addEventListener('DOMContentLoaded', checkOrientation);
// Start the game
createEnemies();
gameLoop();
</script>
</body>
</html>