Skip to content

Commit 6f2d9ed

Browse files
committed
mistral-vibe-devstral-2
> make me a space invaders game as HTML with three.js loaded from a CDN
1 parent 8cec12b commit 6f2d9ed

1 file changed

Lines changed: 280 additions & 0 deletions

File tree

‎mistral-vibe-devstral-2/index.html‎

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
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 with Three.js</title>
7+
<style>
8+
body {
9+
margin: 0;
10+
overflow: hidden;
11+
background-color: #000;
12+
}
13+
canvas {
14+
display: block;
15+
}
16+
#score {
17+
position: absolute;
18+
top: 10px;
19+
left: 10px;
20+
color: white;
21+
font-family: Arial, sans-serif;
22+
font-size: 24px;
23+
z-index: 100;
24+
}
25+
#gameOver {
26+
position: absolute;
27+
top: 50%;
28+
left: 50%;
29+
transform: translate(-50%, -50%);
30+
color: white;
31+
font-family: Arial, sans-serif;
32+
font-size: 48px;
33+
z-index: 100;
34+
display: none;
35+
}
36+
</style>
37+
</head>
38+
<body>
39+
<div id="score">Score: 0</div>
40+
<div id="gameOver">Game Over</div>
41+
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
42+
<script>
43+
// Game state
44+
let score = 0;
45+
let gameOver = false;
46+
let invaders = [];
47+
let bullets = [];
48+
let invaderBullets = [];
49+
let invaderSpeed = 0.05;
50+
let invaderDirection = 1;
51+
let invaderMoveDown = false;
52+
let lastInvaderShotTime = 0;
53+
let invaderShotInterval = 1000;
54+
55+
// Scene setup
56+
const scene = new THREE.Scene();
57+
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
58+
const renderer = new THREE.WebGLRenderer({ antialias: true });
59+
60+
renderer.setSize(window.innerWidth, window.innerHeight);
61+
document.body.appendChild(renderer.domElement);
62+
63+
// Player
64+
const playerGeometry = new THREE.BoxGeometry(1, 0.5, 1);
65+
const playerMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
66+
const player = new THREE.Mesh(playerGeometry, playerMaterial);
67+
player.position.y = -8;
68+
scene.add(player);
69+
70+
// Create invaders
71+
function createInvaders() {
72+
const rows = 5;
73+
const cols = 10;
74+
const spacing = 1.5;
75+
76+
for (let row = 0; row < rows; row++) {
77+
for (let col = 0; col < cols; col++) {
78+
const invaderGeometry = new THREE.BoxGeometry(0.8, 0.5, 0.8);
79+
const invaderMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
80+
const invader = new THREE.Mesh(invaderGeometry, invaderMaterial);
81+
82+
invader.position.x = (col - cols / 2) * spacing;
83+
invader.position.y = 5 - row * spacing;
84+
invader.position.z = 0;
85+
86+
scene.add(invader);
87+
invaders.push(invader);
88+
}
89+
}
90+
}
91+
92+
// Player movement
93+
const keyboard = {};
94+
document.addEventListener('keydown', (event) => {
95+
keyboard[event.key] = true;
96+
});
97+
98+
document.addEventListener('keyup', (event) => {
99+
keyboard[event.key] = false;
100+
});
101+
102+
// Shooting
103+
document.addEventListener('keydown', (event) => {
104+
if (event.code === 'Space' && !gameOver) {
105+
shoot();
106+
}
107+
});
108+
109+
function shoot() {
110+
const bulletGeometry = new THREE.BoxGeometry(0.1, 0.3, 0.1);
111+
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
112+
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
113+
114+
bullet.position.copy(player.position);
115+
bullet.position.y += 0.6;
116+
117+
scene.add(bullet);
118+
bullets.push(bullet);
119+
}
120+
121+
// Invader shooting
122+
function invaderShoot() {
123+
if (invaders.length === 0 || gameOver) return;
124+
125+
const currentTime = Date.now();
126+
if (currentTime - lastInvaderShotTime < invaderShotInterval) return;
127+
128+
lastInvaderShotTime = currentTime;
129+
130+
const randomInvader = invaders[Math.floor(Math.random() * invaders.length)];
131+
132+
const bulletGeometry = new THREE.BoxGeometry(0.1, 0.3, 0.1);
133+
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
134+
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
135+
136+
bullet.position.copy(randomInvader.position);
137+
bullet.position.y -= 0.6;
138+
139+
scene.add(bullet);
140+
invaderBullets.push(bullet);
141+
}
142+
143+
// Collision detection
144+
function checkCollisions() {
145+
// Player bullets vs invaders
146+
for (let i = bullets.length - 1; i >= 0; i--) {
147+
const bullet = bullets[i];
148+
149+
for (let j = invaders.length - 1; j >= 0; j--) {
150+
const invader = invaders[j];
151+
152+
if (Math.abs(bullet.position.x - invader.position.x) < 0.5 &&
153+
Math.abs(bullet.position.y - invader.position.y) < 0.5) {
154+
155+
scene.remove(invader);
156+
invaders.splice(j, 1);
157+
158+
scene.remove(bullet);
159+
bullets.splice(i, 1);
160+
161+
score += 10;
162+
document.getElementById('score').textContent = 'Score: ' + score;
163+
164+
break;
165+
}
166+
}
167+
}
168+
169+
// Invader bullets vs player
170+
for (let i = invaderBullets.length - 1; i >= 0; i--) {
171+
const bullet = invaderBullets[i];
172+
173+
if (Math.abs(bullet.position.x - player.position.x) < 0.5 &&
174+
Math.abs(bullet.position.y - player.position.y) < 0.5) {
175+
176+
scene.remove(bullet);
177+
invaderBullets.splice(i, 1);
178+
179+
gameOver = true;
180+
document.getElementById('gameOver').style.display = 'block';
181+
182+
break;
183+
}
184+
}
185+
186+
// Invaders reaching bottom
187+
for (const invader of invaders) {
188+
if (invader.position.y < -7) {
189+
gameOver = true;
190+
document.getElementById('gameOver').style.display = 'block';
191+
break;
192+
}
193+
}
194+
}
195+
196+
// Game loop
197+
function animate() {
198+
if (gameOver) return;
199+
200+
requestAnimationFrame(animate);
201+
202+
// Player movement
203+
if (keyboard['ArrowLeft'] && player.position.x > -10) {
204+
player.position.x -= 0.1;
205+
}
206+
if (keyboard['ArrowRight'] && player.position.x < 10) {
207+
player.position.x += 0.1;
208+
}
209+
210+
// Move bullets
211+
for (let i = bullets.length - 1; i >= 0; i--) {
212+
const bullet = bullets[i];
213+
bullet.position.y += 0.2;
214+
215+
if (bullet.position.y > 10) {
216+
scene.remove(bullet);
217+
bullets.splice(i, 1);
218+
}
219+
}
220+
221+
// Move invader bullets
222+
for (let i = invaderBullets.length - 1; i >= 0; i--) {
223+
const bullet = invaderBullets[i];
224+
bullet.position.y -= 0.1;
225+
226+
if (bullet.position.y < -10) {
227+
scene.remove(bullet);
228+
invaderBullets.splice(i, 1);
229+
}
230+
}
231+
232+
// Move invaders
233+
let moveDown = false;
234+
235+
for (const invader of invaders) {
236+
invader.position.x += invaderSpeed * invaderDirection;
237+
238+
// Check if any invader reached the edge
239+
if (invader.position.x > 9 || invader.position.x < -9) {
240+
moveDown = true;
241+
}
242+
}
243+
244+
if (moveDown) {
245+
invaderDirection *= -1;
246+
247+
for (const invader of invaders) {
248+
invader.position.y -= 0.5;
249+
}
250+
}
251+
252+
// Invader shooting
253+
invaderShoot();
254+
255+
// Check collisions
256+
checkCollisions();
257+
258+
// Increase difficulty
259+
if (invaders.length < 10) {
260+
invaderSpeed = 0.1;
261+
invaderShotInterval = 500;
262+
}
263+
264+
renderer.render(scene, camera);
265+
}
266+
267+
// Start game
268+
camera.position.z = 15;
269+
createInvaders();
270+
animate();
271+
272+
// Handle window resize
273+
window.addEventListener('resize', () => {
274+
camera.aspect = window.innerWidth / window.innerHeight;
275+
camera.updateProjectionMatrix();
276+
renderer.setSize(window.innerWidth, window.innerHeight);
277+
});
278+
</script>
279+
</body>
280+
</html>

0 commit comments

Comments
 (0)