xxxxxxxxxx
170
let playerImg;
let enemyImg;
let backgroundImage;
let player;
let bullets = [];
let enemies = [];
let enemySpawnTimer = 0;
let gameOver = false;
let score = 0;
let startScreen = true;
function preload() {
playerImg = loadImage('https://raw.githubusercontent.com/CodingWith-Adam/space-invaders/main/images/player.png');
enemyImg = loadImage('https://raw.githubusercontent.com/CodingWith-Adam/space-invaders/main/images/enemy1.png');
backgroundImage = loadImage('https://raw.githubusercontent.com/CodingWith-Adam/space-invaders/main/images/space.png');
}
function setup() {
createCanvas(600, 600);
player = new Player();
}
function draw() {
if (mouseIsPressed === true) {
startScreen = false;
}
if (startScreen === true) {
background(backgroundImage);
fill("white");
text("Click to Play", 225, 300);
textSize(32)
image(playerImg, 500, 500, 50, 50);
image(enemyImg, 500, 100, 50, 40);
image(enemyImg, 450, 150, 50, 40);
image(enemyImg, 250, 100, 50, 40);
image(enemyImg, 100, 210, 50, 40);
image(enemyImg, 10, 80, 50, 40);
fill('yellow')
startscreenbullets(500, 450);
startscreenbullets(525, 390);
startscreenbullets(500, 330);
startscreenbullets(525, 270);
return;
}
if (!gameOver) {
background(backgroundImage);
player.update(mouseX);
player.display();
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].move();
bullets[i].display();
if (bullets[i].offscreen()) {
bullets.splice(i, 1);
} else {
for (let j = enemies.length - 1; j >= 0; j--) {
if (bullets[i].hits(enemies[j])) {
bullets.splice(i, 1);
enemies.splice(j, 1);
score += 150;
break;
}
}
}
}
for (let i = enemies.length - 1; i >= 0; i--) {
let enemy = enemies[i];
enemy.move();
enemy.display();
if (enemy.hits(player)) {
gameOver = true;
}
if (enemy.y > height) {
enemies.splice(i, 1);
score -= 100;
}
}
if (enemySpawnTimer <= 0) {
enemies.push(new Enemy(random(width - 40), 0));
enemySpawnTimer = 30;
} else {
enemySpawnTimer--;
}
textAlign(LEFT);
textSize(20);
fill(255);
text("Score: " + score, 20, 30);
} else {
textAlign(CENTER);
textSize(32);
fill(255);
text("Game Over", width / 2, height / 2);
}
}
function startscreenbullets(x, y){
rect(x, y, 10, 20)
}
function mousePressed() {
bullets.push(new Bullet(player.x + player.width / 2, player.y));
}
class Player {
constructor() {
this.width = 50;
this.height = 20;
this.y = height - this.height - 10;
this.x = width / 2 - this.width / 2;
}
display() {
image(playerImg, this.x, this.y, this.width, this.height);
}
update(mouseX) {
this.x = constrain(mouseX - this.width / 2, 0, width - this.width);
}
}
class Bullet {
constructor(x, y) {
this.width = 5;
this.height = 20;
this.x = x - this.width / 2;
this.y = y;
}
display() {
fill('yellow');
rect(this.x, this.y, this.width, this.height);
}
move() {
this.y -= 7;
}
offscreen() {
return this.y < 0;
}
hits(enemy) {
return collideRectCircle(enemy.x, enemy.y, enemy.width, enemy.height, this.x, this.y, this.width);
}
}
class Enemy {
constructor(x, y) {
this.width = 40;
this.height = 20;
this.x = x;
this.y = y;
}
display() {
image(enemyImg, this.x, this.y, this.width, this.height);
}
move() {
this.y += 2;
}
hits(player) {
return collideRectRect(player.x, player.y, player.width, player.height, this.x, this.y, this.width, this.height);
}
}