xxxxxxxxxx
204
var ship;
var shipSpeed;
shipSpeed = 5;
var invaders = [];
var rockets = [];
var explosions = [];
var particles = [];
function setup() {
createCanvas(600, 400);
background(35, 35, 35);
ship = new ship();
for (var i = 0; i < 10; i++) {
invaders[i] = new Invader(i * 60 + 30, 50);
}
}
//constructor function ship
function ship() {
this.x = width / 2
this.show = function() {
fill(255, 255, 255);
noStroke();
rectMode(CENTER);
rect(this.x, height - 15, 40, 20);
}
this.move = function(rightleft) {
this.x = this.x + shipSpeed * rightleft;
}
}
// constructor for invader
function Invader(x, y) {
this.x = x
this.y = y
this.remove = false
this.show = function() {
fill(255, 255, 255);
noStroke();
rectMode(CENTER);
rect(this.x, this.y, 50, 15);
}
}
//constructor for rockets
function Rocket(x, y) {
this.x = x
this.y = y
this.r = 5
this.remove = false
this.show = function() {
fill(255);
ellipse(this.x, this.y, 2 * this.r, 2 * this.r)
}
this.move = function() {
this.y = this.y - 3;
}
this.check = function(invader) {
var dx = dist(this.x, this.y, invader.x, invader.y)
var dy = abs(this.y) - abs(invader.y);
if (dx < 25) {
if (dy < 0) {
return true;
}
} else {
return false;
}
}
}
//TEST// start ///////////////////////////////////////////
function Explosion(x, y) {
this.x = x
this.y = y
this.particles = 1 // number of particles in one explosion
this.hit = false
this.boom = function() {
for (i = 0; i < particles.length; i++) {
particle = new Particle(this.x, this.y);
particles.push(particle);
console.log ("particles ", particles.length)
}
}
}
function Particle(x, y) {
this.x = x
this.y = y
this.r = 2
this.dx = random(-3, 3)
this.dy = random(-3, 3)
this.show = function() {
fill(255)
ellipse(this.x, this.y, this * 2, this.r * 2)
this.move = function() {
this.x += this.dx
this.y += this.dy
}
}
}
//TEST// end
function draw() {
background(35, 35, 35)
ship.show();
noStroke();
fill(35)
rectMode(CORNER);
if (mouseX > ship.x + 5) {
ship.move(1); //ship moves right
} else if (mouseX < ship.x - 5) {
ship.move(-1); // ship moves left
}
for (var i = 0; i < invaders.length; i++) {
invaders[i].show()
}
for (i = 0; i < rockets.length; i++) {
rockets[i].show();
rockets[i].move();
if (rockets[i].y < 0) { // remove rockets when they leave the canvas
rockets[i].remove = true
}
for (var j = 0; j < invaders.length; j++) { // remove rocket and invader when they hit
if (rockets[i].check(invaders[j])) {
console.log("hit")
//TEST// begin //////////////////////////
var explosion = new Explosion(invaders[j].x, invaders[j].y)
explosions.push(explosion)
console.log("explosions ", explosions.length)
// TEST // end
rockets[i].remove = true
invaders[j].remove = true
} else {}
}
}
// TEST // begin ///////////////////////
for (n = 0; n < explosions.length; n++) {
if (explosions[n].hit) {
explosions[n].boom ();
console.log("boom")
console.log ("explosions2 ", explosions.length)
explosions[n].hit = false
}
}
// TEST // end
for (i = 0; i < rockets.length; i++) {
if (rockets[i].remove) {
rockets.splice(i, 1)
}
}
for (j = 0; j < invaders.length; j++) {
if (invaders[j].remove) {
invaders.splice(j, 1)
}
}
}
function mousePressed() {
{
var rocket = new Rocket(ship.x, 375);
rockets.push(rocket);
console.log("rockets ", rockets.length) //log number of rockets
}
}