xxxxxxxxxx
124
//some ideas from The Coding Train: www.youtube.com/watch?v=biN3v3ef-Y0
//and Ben
let missles = [];
let bugs = [];
let xShip = 200;
let yShip = 300;
function setup() {
createCanvas(400, 400);
frameRate(50);
for (let x = 0; x < 350; x = x + 100) {
bugs.push(new Bug(
x,
0
));
}
}
function draw() {
background(15, 23, 54);
if (keyIsDown(LEFT_ARROW)) {
xShip -= 5;
}
if (keyIsDown(RIGHT_ARROW)) {
xShip += 5;
}
if (keyIsDown(UP_ARROW)) {
if (frameCount % 15 == 0) {
missles.push(new Missle(xShip + 15, yShip - 25));
}
}
starship();
for (let i = 0; i < missles.length; i = i + 1) {
missles[i].update();
noStroke();
fill(255);
text(missles[i].a, 30, 30);
if (missles[i].y < 100) {
if (missles[i].x > 10 && missles[i].x < 400 / 4 * 0.75) {
bugs[0].o = 0;
}
if (missles[i].x > 110 && missles[i].x < 400 / 4 * 1.75) {
bugs[1].o = 0;
}
if (missles[i].x > 220 && missles[i].x < 400 / 4 * 2.75) {
bugs[2].o = 0;
}
if (missles[i].x > 310 && missles[i].x < 400 / 4 * 3.75) {
bugs[3].o = 0;
}
}
}
for (let i = 0; i < bugs.length; i = i + 1) {
bugs[i].update();
}
}
function starship() {
fill(200);
noStroke();
rect(xShip + 15, yShip - 20, 10, 40);
rect(xShip, yShip, 30, 30);
rect(xShip, yShip, 10, 40);
rect(xShip + 30, yShip, 10, 40);
rect(xShip - 10, yShip + 10, 10, 40);
rect(xShip + 40, yShip + 10, 10, 40);
fill(250, 250, 0);
rect(xShip, yShip - 5, 5, 5);
rect(xShip + 35, yShip - 5, 5, 5);
}
class Missle {
constructor(x, y) {
this.x = x;
this.y = y;
this.a = 100;
}
update() {
rect(this.x, this.y, 10, 10);
this.y = this.y - 1;
}
}
class Bug {
constructor(x, y) {
this.x = x;
this.y = y;
this.o = 255;
}
update() {
push();
translate(this.x, this.y);
this.bug();
pop();
}
bug() {
fill(90, 28, 33, this.o);
noStroke();
rect(40, 50, 20, 20);
rect(30, 60, 40, 30);
rect(40, 80, 20, 20);
fill(0, 250, 0, this.o);
rect(40, 90, 5, 5);
rect(55, 90, 5, 5);
fill(54, 10, 19, this.o);
rect(25, 45, 10, 40);
rect(65, 45, 10, 40);
}
}