xxxxxxxxxx
299
//Declaring all variables to be used
let bullets = [];
let enemies = [];
let stars = [];
let powerups = [];
let gamestate;
let posterImage;
let fighter;
let player;
let laser;
let enemy;
let score = 0;
let title_screen_audio;
let playing_screen_audio;
let end_screen_audio;
let laser_sound_effect;
let enemySpawnRate =1;
let timeElapsed = 0;
let slowPowerUp;
function preload() {
posterImage = loadImage('SpaceShooterBackground.png'); //Loading main menu
fighter = loadImage('NicePng_spaceship-png_138961.png'); //Loading spaceship
laser = loadImage('LaserBullet.png'); //Loading laser bullet
enemy = loadImage('invader.png'); //Loading enemies
slowPowerUp = loadImage('hourglass.png');
font = loadFont('GameOverFont.ttf'); //Loading Game Over Screen Font
title_screen_audio = loadSound("SkyFire (Title Screen).mp3");
playing_screen_audio = loadSound("Battle in the Stars.mp3"); //Loading menu music
end_screen_audio = loadSound("gameEndMusic.mp3"); //Load Game Over Screen Music
laser_sound_effect = loadSound("lasershot.wav"); //Load laser sound effect
}
function setup() {
createCanvas(400, 600);
gamestate = 'menu'; //Setting gamestate menu
player = new Spaceship(67, 61); //Initializing Sapceship class as the player
for (let i = 0; i < 100; i++) {
stars[i] = new Star(random(width), random(height), random(1, 5)); //Randomly generating stars
}
for (let i = 0; i < 5; i++) {
let powerup = new Powerup(random(width), -50, 20);
powerups.push(powerup);
}
}
function draw() {
if (gamestate == 'menu') {
image(posterImage, 0, 0, width, height); //Displaying menu screen if gamestate is menu
if (!title_screen_audio.isPlaying())
{
title_screen_audio.play(); //Plays menu music
}
}
if (gamestate == 'playing') {
title_screen_audio.stop(); //Stops menu music when gamestate is playing
if (!playing_screen_audio.isPlaying())
{
playing_screen_audio.play(); //Plays battle music
}
background(1, 22, 64);
player.show();
for (let star of stars) {
star.show(); //Displaying stars
}
for (let i = 0; i < powerups.length; i++) {
powerups[i].update(); // update powerup position
powerups[i].show(); // display powerup
for (let j = 0; j < player.length; j++) {
powerups[i].checkCollision(player); // check if powerup collides with enemy
}
if (powerups[i].y > height) {
powerups.splice(i, 1); // remove powerup if it falls off the bottom of the screen
}
}
// Add enemies randomly
if (frameCount % (60 / enemySpawnRate) == 0) {
let enemy = new Enemy(random(width-50), -50, random(1, 4));
enemies.push(enemy);
}
for (let i = 0; i < bullets.length; i++) {
bullets[i].update(); //Adding velocity to bullets
bullets[i].show(); //Displaying bullets
for (let j = 0; j < enemies.length; j++) {
let enemyHitbox = enemies[j].getHitbox(); //Initializing enemies with hitbox
if (bullets[i].hits(enemyHitbox)) {
bullets.splice(i, 1); //Remove bullets when it hits an enemy
enemies[j].hits(); //Registers hit to enemy
score += 1; //Incremements score on hit
enemies.splice(j, 1); // remove the enemy object from the array
break;
}
}
}
for (let i = 0; i < enemies.length; i++) {
enemies[i].update(); //Makes enemies fall
enemies[i].show(); //Displays enemies
}
let anyEnemyReachedBottom = false; // flag to indicate if any enemy has reached the bottom
for (let i = 0; i < enemies.length; i++) {
if (enemies[i].reachedBottomFlag) {
anyEnemyReachedBottom = true; //Turns true when enemy reaches bottom
break;
}
}
if (anyEnemyReachedBottom) {
gamestate = 'gameover'; //Sets gamestate to gameover once enemy reaches the bottom
}
textSize(20);
strokeWeight(1);
textFont(font);
fill(255);
text("Score: " + score, 10, 30); //Displays score at top left
}
if (gamestate == 'gameover') {
playing_screen_audio.stop(); //Stops battle music
if (!end_screen_audio.isPlaying())
{
end_screen_audio.play(); //Plays defeat music
}
background(1, 22, 64);
for (let star of stars) {
star.show(); //Displays stars
}
textSize(30);
strokeWeight(1);
fill(255);
textFont(font);
text("Game Over", width / 2 - 80, height / 2 - 20);
text("Score: " + score, width / 2 - 65, height / 2 + 20);
text("Press Space to retry!", width / 2 - 150, height / 2 + 60);
}
timeElapsed += deltaTime / 1000;
if (timeElapsed >= 25) { // increase spawn rate every 25 seconds
enemySpawnRate++;
timeElapsed = 0;
}
}
function mouseClicked() {
if (gamestate == 'menu') {
gamestate = 'playing';
}
if (gamestate == 'playing') {
let bullet = new Bullet(mouseX + 3, height - 20, 10);
bullets.push(bullet);
laser_sound_effect.play();
}
}
function keyPressed(){
if (key == ' ' && gamestate == 'gameover'){
score = 0;
bullets = [];
enemies = [];
restartGame();
end_screen_audio.stop();
}
}
function restartGame() {
gamestate = 'playing';
enemySpawnRate = 1;
}
class Bullet {
constructor(x, y, velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
this.size = 20;
}
update() {
this.y -= this.velocity;
}
show() {
image(laser, this.x - this.size/2, this.y - this.size/2, this.size, this.size);
}
hits(hitbox) {
let d = dist(this.x, this.y, hitbox.x + hitbox.width/2, hitbox.y + hitbox.height/2);
if (d < (this.size + hitbox.width)/2) {
return true;
} else {
return false;
}
}
}
class Spaceship {
constructor(x,y) {
this.x = x;
this.y = y;
}
show(){
if (mouseX - 30 < 0)
{
image(fighter,0,height-50,this.x,this.y);
}
else if (mouseX - 30 > width)
{
image(fighter,width - this.x,height-50,this.x,this.y);
}
else
{
image(fighter,mouseX-30,height-50,this.x,this.y);
}
}
}
class Star {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
}
show() {
stroke(255);
strokeWeight(this.size);
point(this.x, this.y);
}
}
class Enemy {
constructor(x, y, velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
this.size = 30;
this.reachedBottomFlag = false; // flag to indicate if this enemy has reached the bottom
}
update() {
this.y += this.velocity;
if (this.y >= height) {
this.reachedBottomFlag = true;
}
}
show() {
image(enemy, this.x, this.y, this.size, this.size);
}
hits() {
this.velocity = 0;
}
getHitbox() {
return {
x: this.x,
y: this.y,
width: this.size,
height: this.size
}
}
}
class Powerup {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.speedMultiplier = 0.5; // factor by which to slow down enemies
this.isCollected = false; // flag to track whether powerup has been collected
}
show() {
noStroke();
image(slowPowerUp,this.x, this.y, this.size,this.size);
}
update() {
this.y += 3; // powerup falls down the screen
}
checkCollision(player) {
// check if powerup collides with enemy
let distance = dist(this.x, this.y, player.x, player.y);
if (distance < (this.size + player.size) / 2) {
this.isCollected = true; // mark powerup as collected
player.speed *= this.speedMultiplier; // slow down enemy
}
}
}