xxxxxxxxxx
let spaceship;
let aliens = [];
let hazards = [];
let score = 0;
let gameOver = false;
let gameStarted = false;
let startButton, restartButton;
let spaceshipImg, alienImg, asteroidImg;
let gameOverSound, collectSound;
function preload() {
//Load images and sounds
spaceshipImg = loadImage('spaceship.png');
alienImg = loadImage('alien.png');
asteroidImg = loadImage('astreoid.png');
gameOverSound = loadSound('game.over.mp3');
collectSound = loadSound('collected.mp3');
}
function setup() {
createCanvas(800, 600);
//Initialize the spaceship
spaceship = new Spaceship();
frameRate(60);
//Create and configure the start button
startButton = createButton('Start');
startButton.position(width / 2 - 50, height / 2);
startButton.size(100, 40);
startButton.mousePressed(startGame);
//Create and configure the restart button
restartButton = createButton('Restart');
restartButton.position(width / 2 - 50, height / 2 + 100);
restartButton.size(100, 40);
restartButton.mousePressed(restartGame);
restartButton.hide();
}
function draw() {
background(0, 0, 50); //Dark blue background
if (!gameStarted) {
//Display the start screen
fill(255);
textSize(48);
textAlign(CENTER);
text('Welcome to Space Collector!', width / 2, height / 2 - 50);
textSize(24);
text('Click Start to Play', width / 2, height / 2 - 10);
return;
}
if (gameOver) {
restartButton.show(); //Show the restart button if the game is over
} else {
spaceship.update(mouseX, mouseY); //Update spaceship position with mouse
spaceship.show(); //Display the spaceship
//Spawn aliens and hazards randomly
if (random() < 0.02) aliens.push(new Alien());
if (random() < 0.01) hazards.push(new Hazard());
//Update and display aliens
for (let i = aliens.length - 1; i >= 0; i--) {
aliens[i].show();
if (aliens[i].collect(spaceship)) {
score++; //Increase score when an alien is collected
collectSound.play();
aliens.splice(i, 1); //Remove collected alien
}
}
//Update and display hazards
for (let i = hazards.length - 1; i >= 0; i--) {
hazards[i].update();
hazards[i].show();
if (hazards[i].collide(spaceship)) {
endGame(); //End the game if a hazard collides with the spaceship
break;
}
}
//Display the current score
fill(255);
textSize(24);
text(`Score: ${score}`, 650, 30);
}
}
class Spaceship {
constructor() {
this.radius = 30; //Size of the spaceship
this.x = width / 2; //Initialize position at the center of the canvas
this.y = height / 2;
}
update(mx, my) {
//Update position based on mouse coordinates
this.x = mx;
this.y = my;
}
show() {
//Display the spaceship image
imageMode(CENTER);
image(spaceshipImg, this.x, this.y, 60, 80);
}
}
class Alien {
constructor() {
this.x = random(width); //Random initial position
this.y = random(height);
this.radius = 25; //Size of the alien
}
show() {
//Display the alien image
imageMode(CENTER);
image(alienImg, this.x, this.y, 50, 50);
}
collect(spaceship) {
//Check if the alien is close enough
return dist(this.x, this.y, spaceship.x, spaceship.y) < this.radius + spaceship.radius;
}
}
class Hazard {
constructor() {
this.radius = 35; //Size of the hazard
this.speed = random(2, 4); //Random movement speed
this.angle = random(TWO_PI); //Random movement direction
//Spawn the hazard at random edges of the canvas
const edge = floor(random(4));
if (edge === 0) { this.x = random(width); this.y = 0; }
else if (edge === 1) { this.x = width; this.y = random(height); }
else if (edge === 2) { this.x = random(width); this.y = height; }
else { this.x = 0; this.y = random(height); }
}
show() {
//Display the hazard image
imageMode(CENTER);
image(asteroidImg, this.x, this.y, 70, 70);
}
update() {
//Move the hazard in its designated direction
this.x += cos(this.angle) * this.speed;
this.y += sin(this.angle) * this.speed;
//Wrap around the canvas edges
if (this.x > width) this.x = 0;
if (this.x < 0) this.x = width;
if (this.y > height) this.y = 0;
if (this.y < 0) this.y = height;
}
collide(spaceship) {
//Check if the hazard collides with the spaceship
return dist(this.x, this.y, spaceship.x, spaceship.y) < this.radius + spaceship.radius;
}
}
function endGame() {
gameOver = true;
noLoop();
gameOverSound.play();
fill(255, 0, 0);
textSize(48);
textAlign(CENTER);
text('Game Over', width / 2, height / 2);
textSize(24);
text(`Score: ${score}`, width / 2, height / 2 + 50);
restartButton.show();
}
function restartGame() {
gameOver = false;
score = 0;
aliens = [];
hazards = [];
spaceship.x = width / 2;
spaceship.y = height / 2;
loop();
restartButton.hide();
}
function startGame() {
gameStarted = true;
startButton.hide(); //Hide the start button
}