xxxxxxxxxx
200
//Welcome to Planet Crash.
//The goal of Planet Crash is to successfully push an energy ball on the left of the screen through the meteor field to crash into the planet on the right side of the screen.
//To play move your spaceship using WASD keys.
//Press "r" to respawn energy balls
//Goodluck and Have Fun!
let player;
let goal;
let goalAchieved;
let startGame;
let greenblocks;
let score = 0;
let pickupSound;
let Theme;
let death;
let spaceship;
let rocks;
let starline = [];
let rocksDestroyed = false
var counter;
function preload() {
pickupSound = loadSound("Ding.wav");
asteroid = loadImage("asteroid.png");
spaceship = loadImage("spaceship.png");
planet = loadImage("planet3.png");
energy = loadImage("planet1.png");
}
function bulletsDestroyed(bullet, rocks) {
pickupSound.play();
rocks.remove();
}
function setup() {
new Canvas(800, 400);
counter = 0;
angleMode();
player = new Sprite(20, 200, 25, 25);
goal = new Sprite(725, random(400), 200, "static");
rocks = new Group();
player.visible = false;
goal.visible = false;
player.layer = 2;
goal.layer = 2;
rocks.layer = 3;
goalAchieved = false;
death = false;
startGame = false;
player.rotationLock = true;
asteroid.resize(25, 25);
spaceship.resize(35, 35);
player.rotation = 90;
greenblocks = new Group();
greenblocks.color = "green";
greenblocks.visible = false;
Theme = createAudio("Theme.mp3");
Theme.volume(0.02);
Theme.play();
//filling the starline array
for (h = 0; h < 30; h++) starline[h] = new star();
function pickupCoins(goal, greenblocks) {
pickupSound.play();
greenblocks.remove();
score++;
}
for (let i = 0; i < 5; i++) {
energy.resize(25, 25);
greenblocks.addImage("default", energy);
new greenblocks.Sprite(random(75, 600), random(50, 350), 25);
}
goal.overlaps(greenblocks, pickupCoins);
}
function destroy() {
rocksDestroyed = true;
rocks.remove();
}
function die() {
death = true;
allSprites.remove();
}
function bullet() {
bullets = new Sprite(player.x + 10, player.y, 10, "dynamic");
bullets.vel.x = 15;
bullets.color = "red";
}
function draw() {
background(255);
fill(0);
textSize(48);
textAlign(CENTER);
if (kb.pressed(" ")) {
startGame = true;
}
if (startGame == false) {
text("PLANET CRASH", width / 2 - 25, height / 2 - 25);
text("Press space to play", width / 2 - 30, height / 2 + 25);
} else if (startGame) {
background(31, 45, 81);
for (h = 0; h < starline.length; h++) starline[h].update();
player.visible = true;
player.addImage("default", spaceship);
goal.visible = true;
planet.resize(200, 200);
goal.addImage("default", planet);
greenblocks.visible = true;
//I had issues making a collide with the rocks be recognized. My inital code that did not work is in line 124-128. If i replaced (rocks) with (goal) p5 would initiate the you died screen and would print crash but when i changed it to rocks it would not recognize a crash.
if (counter == 12) {
rocks = new Sprite(random(100, width - 75), -50, 40, 40);
rocks.addImage("default", asteroid);
rocks.velocity.y = 5;
rocks.velocity.x = 1;
counter = 0;
rocks.overlaps(goal);
player.overlaps(rocks, die);
}
counter++;
if (kb.pressed("r")) {
for (let i = 0; i < 5; i++) {
energy.resize(25, 25);
greenblocks.addImage("default", energy);
new greenblocks.Sprite(random(75, 600), random(50, 350), 25);
}
}
if (kb.presses("z")) {
bullet();
}
if (kb.pressing("up") || kb.pressing("ArrowUp")) {
player.vel.y = -6;
} else if (kb.pressing("down") || kb.pressing("ArrowDown")) {
player.vel.y = 6;
} else {
player.vel.y = 0;
}
if (kb.pressing("left") || kb.pressing("ArrowLeft")) {
player.vel.x = -6;
} else if (kb.pressing("right") || kb.pressing("ArrowRight")) {
player.vel.x = 6;
} else {
player.vel.x = 0;
}
if (score == 1) {
goalAchieved = true;
allSprites.remove();
}
if (goalAchieved) {
fill(255);
Theme.stop();
text("You destroyed the planet!", width / 2, height / 2);
text("Press space to restart", width / 2, height / 2 + 60);
}
if (kb.pressed(" ") && goalAchieved) {
setup();
startGame = true;
score = 0;
}
// if (player.overlapping(rocks)) {
// print('crash')
// death = true;
// allSprites.remove();
// }
if (death == true) {
fill(255);
Theme.stop();
text("You died!", width / 2, height / 2);
text("Press space to restart", width / 2, height / 2 + 60);
}
if (kb.pressed(" ") && death) {
setup();
startGame = true;
}
}
}
class star {
constructor() {
this.xPos = random(0, width);
this.yPos = random(0, 400);
this.bWidth = random(2, 6);
this.bHeight = random(2, 6);
}
update() {
fill(255, 255, 255, 180);
ellipse(this.xPos, this.yPos, this.bWidth);
this.xPos -= 0.2;
if (this.xPos < -this.bWidth) {
this.xPos = width;
}
}
}