Score: 0
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const spaceship = {
x: 50,
y: canvas.height / 2,
speed: 8,
width: 30,
height: 20,
isLanded: false,
};
const meteors = [];
const planets = [];
let score = 0;
let isGameOver = false;
let isTransitioning = false;
function drawSpaceBackground() {
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#fff";
for (let i = 0; i < 50; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
ctx.fillRect(x, y, 2, 2);
}
}
function drawSpaceship() {
ctx.beginPath();
ctx.rect(spaceship.x, spaceship.y, spaceship.width, spaceship.height);
ctx.fillStyle = "#3498db";
ctx.fill();
ctx.closePath();
}
function drawMeteors() {
ctx.fillStyle = "#ff0000";
for (const meteor of meteors) {
ctx.beginPath();
ctx.arc(meteor.x, meteor.y, meteor.radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
function drawPlanets() {
for (const planet of planets) {
ctx.beginPath();
ctx.arc(planet.x, planet.y, planet.radius, 0, Math.PI * 2);
ctx.fillStyle = planet.color;
ctx.fill();
ctx.closePath();
}
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function updateGameArea() {
if (isGameOver) {
ctx.font = "30px Arial";
ctx.fillStyle = "#fff";
ctx.fillText("Game Over", canvas.width / 2 - 100, canvas.height / 2);
return;
}
drawSpaceBackground();
drawSpaceship();
drawMeteors();
drawPlanets();
for (const meteor of meteors) {
meteor.y += meteor.speed;
checkCollision(meteor);
}
for (const planet of planets) {
checkCollision(planet);
}
meteors.forEach((meteor, index) => {
if (meteor.y > canvas.height) {
meteors.splice(index, 1);
spawnMeteor(); // Respawn meteor after it goes out of screen
increaseScore();
}
});
if (spaceship.isLanded && !isTransitioning) {
isTransitioning = true;
transitionToPlanetScreen();
}
}
function moveSpaceship(direction) {
if (isGameOver || isTransitioning) return;
if (spaceship.isLanded) {
spaceship.isLanded = false;
updateGameArea();
return;
}
switch (direction) {
case "left":
if (spaceship.x > 0) spaceship.x -= spaceship.speed;
break;
case "up":
if (spaceship.y > 0) spaceship.y -= spaceship.speed;
break;
case "right":
if (spaceship.x {
isTransitioning = false;
*This prompt didn't get me into the game this time. Plz try to generate it a few times.