|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>2D Rocket Launch with Expanding Orbit</title> |
|
<style> |
|
body { margin: 0; background-color: black; overflow: hidden; } |
|
canvas { display: block; } |
|
</style> |
|
</head> |
|
<body> |
|
<canvas id="canvas"></canvas> |
|
|
|
<script> |
|
|
|
const canvas = document.getElementById("canvas"); |
|
const ctx = canvas.getContext("2d"); |
|
|
|
|
|
canvas.width = window.innerWidth; |
|
canvas.height = window.innerHeight; |
|
|
|
|
|
const earth = { |
|
x: canvas.width / 2, |
|
y: canvas.height - 100, |
|
radius: 50 |
|
}; |
|
|
|
|
|
let rocket = { |
|
x: earth.x, |
|
y: earth.y - earth.radius - 10, |
|
radius: 5, |
|
velocityY: 0, |
|
isLaunching: false, |
|
inOrbit: false, |
|
orbitAngle: 0, |
|
orbitRadius: 80 |
|
}; |
|
|
|
|
|
const launchSpeed = -3.0; |
|
const gravity = 0.05; |
|
const orbitSpeed = 0.02; |
|
const orbitExpansionRate = 0.05; |
|
|
|
let boosting = false; |
|
|
|
|
|
document.addEventListener("keydown", (event) => { |
|
if (event.code === "Space" && !rocket.isLaunching && !rocket.inOrbit) { |
|
rocket.isLaunching = true; |
|
rocket.velocityY = launchSpeed; |
|
} |
|
|
|
|
|
if (event.code === "KeyB" && rocket.inOrbit) { |
|
boosting = true; |
|
} |
|
}); |
|
|
|
document.addEventListener("keyup", (event) => { |
|
if (event.code === "KeyB") { |
|
boosting = false; |
|
} |
|
}); |
|
|
|
|
|
function animate() { |
|
ctx.clearRect(0, 0, canvas.width, canvas.height); |
|
|
|
|
|
ctx.fillStyle = "blue"; |
|
ctx.beginPath(); |
|
ctx.arc(earth.x, earth.y, earth.radius, 0, Math.PI * 2); |
|
ctx.fill(); |
|
|
|
|
|
if (rocket.isLaunching && !rocket.inOrbit) { |
|
rocket.y += rocket.velocityY; |
|
rocket.velocityY += gravity; |
|
|
|
|
|
if (rocket.y < earth.y - 150) { |
|
rocket.isLaunching = false; |
|
rocket.inOrbit = true; |
|
rocket.orbitAngle = 0; |
|
} |
|
} |
|
|
|
else if (rocket.inOrbit) { |
|
rocket.orbitAngle += orbitSpeed; |
|
|
|
|
|
rocket.orbitRadius += orbitExpansionRate; |
|
|
|
|
|
if (boosting) { |
|
rocket.orbitRadius += 0.5; |
|
} |
|
|
|
|
|
rocket.x = earth.x + rocket.orbitRadius * Math.cos(rocket.orbitAngle); |
|
rocket.y = earth.y - rocket.orbitRadius * Math.sin(rocket.orbitAngle); |
|
} |
|
|
|
|
|
ctx.fillStyle = "red"; |
|
ctx.beginPath(); |
|
ctx.arc(rocket.x, rocket.y, rocket.radius, 0, Math.PI * 2); |
|
ctx.fill(); |
|
|
|
requestAnimationFrame(animate); |
|
} |
|
|
|
animate(); |
|
</script> |
|
</body> |
|
</html> |
|
|