Meta Description" name="description" />
<!DOCTYPE html>
<html>
<head>
<title>3D Rocket Game</title>
<style>
body { margin: 0; overflow: hidden; }
button {
position: absolute;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#engineBtn { top: 10px; left: 10px; }
#fuelBtn { top: 10px; left: 130px; }
#launchBtn { top: 10px; left: 250px; }
#altitude {
position: absolute;
top: 50px;
left: 10px;
font-size: 20px;
color: white;
}
</style>
</head>
<body>
<button id="engineBtn">Add Engine</button>
<button id="fuelBtn">Add Fuel</button>
<button id="launchBtn">Launch</button>
<div id="altitude">Height: 0 m</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
let scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
let camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 10;
camera.position.y = 5;
let renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Ground
let groundGeometry = new THREE.PlaneGeometry(50, 50);
let groundMaterial = new THREE.MeshBasicMaterial({color: 0x228B22});
let ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
// Rocket Body
let rocketGeometry = new THREE.CylinderGeometry(0.5, 0.5, 3, 32);
let rocketMaterial = new THREE.MeshBasicMaterial({color: 0xff0000});
let rocket = new THREE.Mesh(rocketGeometry, rocketMaterial);
rocket.position.y = 1.5;
scene.add(rocket);
// Variables
let thrust = 0;
let fuel = 0;
let velocity = 0;
let gravity = 0.01;
let launched = false;
// Buttons
document.getElementById("engineBtn").onclick = function() {
thrust += 0.02;
alert("Engine Added π");
};
document.getElementById("fuelBtn").onclick = function() {
fuel += 5;
alert("Fuel Added β½");
};
document.getElementById("launchBtn").onclick = function() {
launched = true;
};
// Animation Loop
function animate() {
requestAnimationFrame(animate);
if (launched) {
if (fuel > 0) {
velocity += thrust;
fuel -= 0.05;
}
velocity -= gravity;
rocket.position.y += velocity;
if (rocket.position.y < 1.5) {
rocket.position.y = 1.5;
velocity = 0;
}
}
document.getElementById("altitude").innerText =
"Height: " + (rocket.position.y - 1.5).toFixed(2) + " m";
camera.position.y = rocket.position.y + 3;
camera.lookAt(rocket.position);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>2
2
122KB
592KB
421.0ms
100.0ms
421.0ms