Spaces:
Running
Running
swapping to spaceship
Browse files- index.html +106 -173
index.html
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
-
|
3 |
-
<!-- this was mostly written by Claude-3.5-Sonnet btw -->
|
4 |
-
|
5 |
<html lang="en">
|
6 |
<head>
|
7 |
<meta charset="UTF-8">
|
8 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
9 |
-
<title>
|
10 |
<style>
|
11 |
body {
|
12 |
display: flex;
|
@@ -14,218 +11,154 @@
|
|
14 |
align-items: center;
|
15 |
height: 100vh;
|
16 |
margin: 0;
|
17 |
-
background-color: #
|
18 |
}
|
19 |
canvas {
|
20 |
-
border: 2px solid #
|
21 |
}
|
22 |
</style>
|
23 |
</head>
|
24 |
<body>
|
25 |
-
<canvas id="gameCanvas" width="800" height="
|
26 |
<script>
|
27 |
const canvas = document.getElementById('gameCanvas');
|
28 |
const ctx = canvas.getContext('2d');
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
let snake = [
|
34 |
-
{x: 20, y: 20},
|
35 |
-
];
|
36 |
-
let food = {x: 15, y: 15};
|
37 |
-
let dx = 0;
|
38 |
-
let dy = 0;
|
39 |
-
let score = 0;
|
40 |
-
let obstacles = [];
|
41 |
let enemies = [];
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
-
enemies = [];
|
56 |
-
for (let i = 0; i < 3; i++) {
|
57 |
-
enemies.push({
|
58 |
-
x: Math.floor(Math.random() * tileCount),
|
59 |
-
y: Math.floor(Math.random() * tileCount)
|
60 |
-
});
|
61 |
-
}
|
62 |
-
}
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
drawFood();
|
73 |
-
drawObstacles();
|
74 |
-
drawEnemies();
|
75 |
-
checkCollision();
|
76 |
-
drawScore();
|
77 |
}
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
}
|
83 |
|
84 |
-
function
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
snake.pop();
|
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 |
-
function
|
118 |
-
|
119 |
-
ctx.
|
120 |
-
|
121 |
|
122 |
-
|
123 |
-
ctx.fillStyle = '
|
124 |
-
|
125 |
-
ctx.fillRect(obstacle.x * gridSize, obstacle.y * gridSize, gridSize - 2, gridSize - 2);
|
126 |
-
});
|
127 |
-
}
|
128 |
|
129 |
-
|
130 |
-
ctx.fillStyle = '
|
131 |
enemies.forEach(enemy => {
|
132 |
-
ctx.fillRect(enemy.x
|
133 |
});
|
134 |
-
}
|
135 |
-
|
136 |
-
function generateFood() {
|
137 |
-
do {
|
138 |
-
food.x = Math.floor(Math.random() * tileCount);
|
139 |
-
food.y = Math.floor(Math.random() * tileCount);
|
140 |
-
} while (isCollision(food) || isOnSnake(food) || isOnEnemy(food));
|
141 |
-
}
|
142 |
-
|
143 |
-
function isCollision(pos) {
|
144 |
-
return obstacles.some(obstacle => obstacle.x === pos.x && obstacle.y === pos.y);
|
145 |
-
}
|
146 |
-
|
147 |
-
function isOnSnake(pos) {
|
148 |
-
return snake.some(segment => segment.x === pos.x && segment.y === pos.y);
|
149 |
-
}
|
150 |
-
|
151 |
-
function isOnEnemy(pos) {
|
152 |
-
return enemies.some(enemy => enemy.x === pos.x && enemy.y === pos.y);
|
153 |
-
}
|
154 |
-
|
155 |
-
function checkCollision() {
|
156 |
-
const head = snake[0];
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
161 |
}
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
}
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
173 |
}
|
174 |
|
175 |
function resetGame() {
|
176 |
-
|
177 |
-
food = {x: 15, y: 15};
|
178 |
-
dx = 0;
|
179 |
-
dy = 0;
|
180 |
score = 0;
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
}
|
186 |
-
|
187 |
-
function drawScore() {
|
188 |
-
ctx.fillStyle = 'black';
|
189 |
-
ctx.font = '20px Arial';
|
190 |
-
ctx.fillText(`Score: ${score}`, 10, 30);
|
191 |
}
|
192 |
|
193 |
-
|
194 |
-
|
195 |
-
function changeDirection(event) {
|
196 |
-
const key = event.key.toLowerCase();
|
197 |
-
|
198 |
-
const goingUp = dy === -1;
|
199 |
-
const goingDown = dy === 1;
|
200 |
-
const goingRight = dx === 1;
|
201 |
-
const goingLeft = dx === -1;
|
202 |
-
|
203 |
-
if (key === 'a' && !goingRight) {
|
204 |
-
dx = -1;
|
205 |
-
dy = 0;
|
206 |
-
}
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
211 |
}
|
|
|
212 |
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
}
|
217 |
-
|
218 |
-
if (key === 's' && !goingUp) {
|
219 |
-
dx = 0;
|
220 |
-
dy = 1;
|
221 |
-
}
|
222 |
-
}
|
223 |
-
|
224 |
-
function gameLoop() {
|
225 |
-
drawGame();
|
226 |
-
setTimeout(gameLoop, 100);
|
227 |
-
}
|
228 |
|
|
|
229 |
gameLoop();
|
230 |
</script>
|
231 |
</body>
|
|
|
1 |
<!DOCTYPE html>
|
|
|
|
|
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Space Invaders</title>
|
7 |
<style>
|
8 |
body {
|
9 |
display: flex;
|
|
|
11 |
align-items: center;
|
12 |
height: 100vh;
|
13 |
margin: 0;
|
14 |
+
background-color: #000;
|
15 |
}
|
16 |
canvas {
|
17 |
+
border: 2px solid #fff;
|
18 |
}
|
19 |
</style>
|
20 |
</head>
|
21 |
<body>
|
22 |
+
<canvas id="gameCanvas" width="800" height="600"></canvas>
|
23 |
<script>
|
24 |
const canvas = document.getElementById('gameCanvas');
|
25 |
const ctx = canvas.getContext('2d');
|
26 |
|
27 |
+
// Game variables
|
28 |
+
let playerX = 370;
|
29 |
+
let playerY = 480;
|
30 |
+
let playerWidth = 64;
|
31 |
+
let playerHeight = 64;
|
32 |
+
let playerSpeed = 5;
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
let enemies = [];
|
35 |
+
const numEnemies = 6;
|
36 |
+
const enemyWidth = 64;
|
37 |
+
const enemyHeight = 64;
|
38 |
+
|
39 |
+
let bullet = {
|
40 |
+
x: 0,
|
41 |
+
y: 480,
|
42 |
+
width: 8,
|
43 |
+
height: 20,
|
44 |
+
speed: 10,
|
45 |
+
active: false
|
46 |
+
};
|
47 |
|
48 |
+
let score = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
// Initialize enemies
|
51 |
+
for (let i = 0; i < numEnemies; i++) {
|
52 |
+
enemies.push({
|
53 |
+
x: Math.random() * (canvas.width - enemyWidth),
|
54 |
+
y: Math.random() * 100 + 50,
|
55 |
+
speed: 2,
|
56 |
+
direction: 1
|
57 |
+
});
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
59 |
|
60 |
+
// Game loop
|
61 |
+
function gameLoop() {
|
62 |
+
update();
|
63 |
+
draw();
|
64 |
+
requestAnimationFrame(gameLoop);
|
65 |
}
|
66 |
|
67 |
+
function update() {
|
68 |
+
// Move player
|
69 |
+
if (keys.ArrowLeft && playerX > 0) playerX -= playerSpeed;
|
70 |
+
if (keys.ArrowRight && playerX < canvas.width - playerWidth) playerX += playerSpeed;
|
71 |
|
72 |
+
// Move bullet
|
73 |
+
if (bullet.active) {
|
74 |
+
bullet.y -= bullet.speed;
|
75 |
+
if (bullet.y < 0) bullet.active = false;
|
|
|
76 |
}
|
|
|
77 |
|
78 |
+
// Move enemies
|
79 |
+
enemies.forEach(enemy => {
|
80 |
+
enemy.x += enemy.speed * enemy.direction;
|
81 |
+
if (enemy.x <= 0 || enemy.x >= canvas.width - enemyWidth) {
|
82 |
+
enemy.direction *= -1;
|
83 |
+
enemy.y += 20;
|
84 |
+
}
|
85 |
+
|
86 |
+
// Check for collision with bullet
|
87 |
+
if (bullet.active && collision(bullet, enemy)) {
|
88 |
+
bullet.active = false;
|
89 |
+
enemy.y = Math.random() * 100 + 50;
|
90 |
+
enemy.x = Math.random() * (canvas.width - enemyWidth);
|
91 |
+
score++;
|
92 |
+
}
|
93 |
|
94 |
+
// Check for game over
|
95 |
+
if (enemy.y > playerY - enemyHeight) {
|
96 |
+
alert('Game Over! Your score: ' + score);
|
97 |
+
resetGame();
|
98 |
+
}
|
99 |
});
|
100 |
}
|
101 |
|
102 |
+
function draw() {
|
103 |
+
// Clear canvas
|
104 |
+
ctx.fillStyle = 'black';
|
105 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
106 |
|
107 |
+
// Draw player
|
108 |
+
ctx.fillStyle = 'lime';
|
109 |
+
ctx.fillRect(playerX, playerY, playerWidth, playerHeight);
|
|
|
|
|
|
|
110 |
|
111 |
+
// Draw enemies
|
112 |
+
ctx.fillStyle = 'red';
|
113 |
enemies.forEach(enemy => {
|
114 |
+
ctx.fillRect(enemy.x, enemy.y, enemyWidth, enemyHeight);
|
115 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
+
// Draw bullet
|
118 |
+
if (bullet.active) {
|
119 |
+
ctx.fillStyle = 'white';
|
120 |
+
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
|
121 |
}
|
122 |
|
123 |
+
// Draw score
|
124 |
+
ctx.fillStyle = 'white';
|
125 |
+
ctx.font = '24px Arial';
|
126 |
+
ctx.fillText('Score: ' + score, 10, 30);
|
127 |
+
}
|
|
|
128 |
|
129 |
+
function collision(rect1, rect2) {
|
130 |
+
return rect1.x < rect2.x + enemyWidth &&
|
131 |
+
rect1.x + rect1.width > rect2.x &&
|
132 |
+
rect1.y < rect2.y + enemyHeight &&
|
133 |
+
rect1.y + rect1.height > rect2.y;
|
134 |
}
|
135 |
|
136 |
function resetGame() {
|
137 |
+
playerX = 370;
|
|
|
|
|
|
|
138 |
score = 0;
|
139 |
+
enemies.forEach(enemy => {
|
140 |
+
enemy.y = Math.random() * 100 + 50;
|
141 |
+
enemy.x = Math.random() * (canvas.width - enemyWidth);
|
142 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
}
|
144 |
|
145 |
+
// Keyboard input
|
146 |
+
let keys = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
+
document.addEventListener('keydown', (e) => {
|
149 |
+
keys[e.code] = true;
|
150 |
+
if (e.code === 'Space' && !bullet.active) {
|
151 |
+
bullet.active = true;
|
152 |
+
bullet.x = playerX + playerWidth / 2 - bullet.width / 2;
|
153 |
+
bullet.y = playerY;
|
154 |
}
|
155 |
+
});
|
156 |
|
157 |
+
document.addEventListener('keyup', (e) => {
|
158 |
+
keys[e.code] = false;
|
159 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
// Start the game
|
162 |
gameLoop();
|
163 |
</script>
|
164 |
</body>
|