Here's a full implementation of a 3D version of Skyroads Level 1 using Three.js, featuring a spaceship navigating through space obstacles. This version includes:
- A 3D space environment with stars.
- A player-controlled spaceship.
- Obstacles (asteroids) moving toward the player.
- Shooting mechanics.
- Collision detection.
- Game over logic.
🚀 Live Demo Instructions
- Save the code below as
index.html.
- Open it in a browser.
- Use
WASD to move, Space to shoot, and avoid obstacles.
🌌 Full Code: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>3D Skyroads - Level 1</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
// Stars background
const starGeometry = new THREE.BufferGeometry();
const starMaterial = new THREE.PointsMaterial({ color: 0xffffff });
const starVertices = [];
for (let i = 0; i < 10000; i++) {
const x = (Math.random() - 0.5) * 2000;
const y = (Math.random() - 0.5) * 2000;
const z = (Math.random() - 0.5) * 2000;
starVertices.push(x, y, z);
}
starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
const stars = new THREE.Points(starGeometry, starMaterial);
scene.add(stars);
// Player ship
const shipGeometry = new THREE.BoxGeometry(1, 1, 2);
const shipMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const ship = new THREE.Mesh(shipGeometry, shipMaterial);
ship.position.set(0, 0, 0);
scene.add(ship);
// Obstacles (asteroids)
const obstacles = [];
const obstacleGeometry = new THREE.SphereGeometry(0.5, 16, 16);
const obstacleMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
function createObstacle() {
const obstacle = new THREE.Mesh(obstacleGeometry, obstacleMaterial);
obstacle.position.set(
(Math.random() - 0.5) * 10,
(Math.random() - 0.5) * 10,
50
);
scene.add(obstacle);
obstacles.push(obstacle);
}
// Bullets
const bullets = [];
const bulletGeometry = new THREE.BoxGeometry(0.2, 0.2, 0.5);
const bulletMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
function shootBullet() {
const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
bullet.position.copy(ship.position);
scene.add(bullet);
bullets.push(bullet);
}
// Controls
const keys = {};
window.addEventListener('keydown', (e) => keys[e.key] = true);
window.addEventListener('keyup', (e) => keys[e.key] = false);
// Game variables
let gameOver = false;
const shipSpeed = 0.1;
const obstacleSpeed = 0.05;
// Spawn obstacles at intervals
setInterval(createObstacle, 1000);
// Animation loop
function animate() {
if (gameOver) return;
// Move player
if (keys['w']) ship.position.y += shipSpeed;
if (keys['s']) ship.position.y -= shipSpeed;
if (keys['a']) ship.position.x -= shipSpeed;
if (keys['d']) ship.position.x += shipSpeed;
// Shoot bullets
if (keys[' ']) {
shootBullet();
keys[' '] = false; // Prevent rapid fire
}
// Update bullets
for (let i = bullets.length - 1; i >= 0; i--) {
const bullet = bullets[i];
bullet.position.z -= 0.2;
if (bullet.position.z < -50) {
scene.remove(bullet);
bullets.splice(i, 1);
}
}
// Update obstacles
for (let i = obstacles.length - 1; i >= 0; i--) {
const obstacle = obstacles[i];
obstacle.position.z -= obstacleSpeed;
// Check collision with player
const dx = ship.position.x - obstacle.position.x;
const dy = ship.position.y - obstacle.position.y;
const dz = ship.position.z - obstacle.position.z;
if (Math.sqrt(dx * dx + dy * dy + dz * dz) < 1.5) {
gameOver = true;
alert("Game Over! Press F5 to restart.");
return;
}
// Check collision with bullets
for (let j = bullets.length - 1; j >= 0; j--) {
const bullet = bullets[j];
const bx = bullet.position.x;
const by = bullet.position.y;
const bz = bullet.position.z;
const dx = bx - obstacle.position.x;
const dy = by - obstacle.position.y;
const dz = bz - obstacle.position.z;
if (Math.sqrt(dx * dx + dy * dy + dz * dz) < 0.7) {
scene.remove(obstacle);
scene.remove(bullet);
obstacles.splice(i, 1);
bullets.splice(j, 1);
break;
}
}
// Remove off-screen obstacles
if (obstacle.position.z < -50) {
scene.remove(obstacle);
obstacles.splice(i, 1);
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
🎮 Controls
- WASD: Move the ship left/right/up/down.
- Spacebar: Shoot bullets.
- Game Over: If the ship collides with an asteroid.
🧠 Features
- 3D Space Environment: Stars, asteroids, and a spaceship.
- Dynamic Obstacles: Asteroids move toward the player.
- Shooting: Bullets travel forward to destroy obstacles.
- Collision Detection: Checks for player-asteroid and bullet-asteroid collisions.
🧩 Next Steps (Optional)
- Add scoring.
- Add levels with increasing difficulty.
- Add sound effects.
- Improve ship graphics (e.g., using a 3D model).
Let me know if you'd like to expand this into a full game! 🚀