xxxxxxxxxx
159
let missles = [];
let bugs = [];
let xShip = 200;
let yShip = 330;
let score = 0;
let wTextBox = 260;
let hTextBox = 120;
let o;
function setup() {
createCanvas(400, 400);
frameRate(30);
for (let x = 15; x < 350; x = x + 80) {
bugs.push(new Bug(
x,
-200,
10,
20
));
}
}
function draw() {
background(15, 23, 54);
startScreen();
if (keyIsDown(LEFT_ARROW)) {
xShip -= 5;
}
if (keyIsDown(RIGHT_ARROW)) {
xShip += 5;
}
if (keyIsDown(SHIFT)) {
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);
textAlign(LEFT);
text("SCORE: " + (score), 30, 30);
for (let j = 0; j < bugs.length; j = j + 1) {
if (missles[i].x <= bugs[j].x + 40 && missles[i].x >= bugs[j].x &&
missles[i].y <= bugs[j].y + 40 && missles[i].y >= bugs[j].y) {
bugs.splice(j, 1);
score = score + 100;
}
}
}
for (let i = 0; i < bugs.length; i = i + 1) {
bugs[i].update();
}
}
function starship() {
noStroke();
fill(250);
rect(xShip, yShip, 30, 30);
rect(xShip, yShip, 10, 40);
rect(xShip + 30, yShip, 10, 40);
rect(xShip + 15, yShip - 15, 10, 40);
fill(200);
rect(xShip - 10, yShip + 10, 10, 40);
rect(xShip + 40, yShip + 10, 10, 40);
fill(250, random(100, 250), 0);
rect(xShip, yShip - 5, 5, 5);
rect(xShip + 35, yShip - 5, 5, 5);
rect(xShip + 5, yShip + 35, 5, 5);
rect(xShip + 30, yShip + 35, 5, 5);
rect(xShip, yShip + 40, 5, 5);
rect(xShip + 35, yShip + 40, 5, 5);
rect(xShip - 5, yShip + 45, 5, 5);
rect(xShip + 40, yShip + 45, 5, 5);
}
function startScreen() {
strokeWeight(3);
stroke(250, 230, 0, o);
noFill();
rect(width / 2 - wTextBox / 2,
height / 2 - hTextBox / 2 - hTextBox / 16,
wTextBox,
hTextBox);
strokeWeight(2);
textAlign(CENTER);
textSize(30);
fill(0, 250, 0, o);
text("Galaga Knock-off", width / 2, height / 2 - 35);
noStroke();
textSize(20);
fill(200,o);
text("L + R arrows = move ship", width / 2, height / 2 + 10);
text("SHIFT key = shoot", width / 2, height / 2 + 40);
if (keyIsPressed === true) {
o = 0;
}
}
class Missle {
constructor(x, y) {
this.x = x;
this.y = y;
}
update() {
rect(this.x, this.y, 10, 10);
this.y = this.y - 1;
}
}
class Bug {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
update() {
push();
translate(this.x, this.y);
this.bug();
this.y = this.y + 1;
pop();
}
bug() {
noStroke();
fill(54, 0, 19);
rect(random(0, -5), -5, this.w, this.h + 20);
rect(random(40, 45), -5, this.w, this.h + 20);
rect(5, 15, this.w + 30, this.h + 10);
fill(90, 28, 33);
rect(15, 30, this.w + 10, this.h);
rect(10, 15, this.w + 20, this.h);
rect(15, 5, this.w + 10, this.h);
fill(0, random(0, 250), 0);
rect(15, 40, this.w - 5, this.h - 15);
rect(30, 40, this.w - 5, this.h - 15);
}
}
//some ideas from The Coding Train:
//www.youtube.com/watch?v=biN3v3ef-Y0
//and Ben: https://editor.p5js.org/bengrosser/sketches/VoScnbe2n