xxxxxxxxxx
176
let phage;
let aliens = [];
let pews = [];
let score, startFrame;
let gameover;
function preload() {
bg = loadImage("bg.png");
sprites = loadImage("sprite_sheet.png");
}
function setup() {
createCanvas(300, 400);
bg = loadImage("bg.png");
phage = new Phage();
for (let i = 0; i < 18; i++) {
aliens[i] = new Alien(10 + (i % 6) * 40, 120 + int(i / 6) * 40);
}
gameover = false;
score = 0;
pews = [];
}
function draw() {
imageMode(CORNER);
background(bg);
if (!focused) {
fill(255);
textAlign(CENTER, CENTER);
textSize(30);
text("Click to start", width / 2, height / 2);
textSize(20);
text("LEFT/RIGHT: move\nUP: shoot", width / 2, (3 * height) / 4);
startFrame = frameCount;
return;
}
if (!gameover) {
if (keyCode === LEFT_ARROW) {
phage.move(-5);
} else if (keyCode === RIGHT_ARROW) {
phage.move(5);
}
} else {
fill(255);
textAlign(CENTER, CENTER);
textSize(30);
text("Score: " + score, width / 2, height / 2);
textSize(20);
text("Type ' r ' to restart", width / 2, (3 * height) / 4);
startFrame = frameCount;
}
phage.display(gameover);
for (let i = 0; i < aliens.length; i++) {
aliens[i].move();
aliens[i].display(i % 6, int(i / 6));
}
for (let i = pews.length - 1; i >= 0; i--) {
pews[i].display();
if (pews[i].y < 30 || pews[i].hasHit) pews.splice(i, 1);
}
textSize(30);
textAlign(RIGHT, CENTER);
if (!gameover) {
text("" + score, (19 * width) / 20, height / 10);
if (score == 18) {
fill(255);
textAlign(CENTER, CENTER);
textSize(30);
text("You Win!", width / 2, height / 2);
textSize(20);
text("Type ' r ' to restart", width / 2, (3 * height) / 4);
}
}
}
class Alien {
constructor(x, y) {
this.x = x;
this.y = y;
this.gamespeed = 0.4;
this.died = -1;
}
move() {
if ((((frameCount - startFrame) % 100) * 0.4) / this.gamespeed == 0) {
this.gamespeed *= -1;
this.y += 3;
}
this.x += this.gamespeed;
this.y += 0;
this.y = min(this.y, height - 20);
}
display(row, col) {
if (this.died < 0) {
image(sprites, this.x, this.y, 48, 48, 24 * (2 - col), 0, 24, 24);
} else {
if (frameCount - this.died < 30 && frameCount % 10 < 5) {
image(sprites, this.x, this.y, 48, 48, 24 * (2 - col), 24, 24, 24);
}
}
}
}
class Phage {
constructor() {
this.x = random(width);
this.y = 330;
this.gamespeed = 1;
}
move(d) {
this.x += d;
this.x = constrain(this.x, 20, width - 10);
}
display(over) {
push();
translate(this.x, this.y);
scale(1, 1.0 - 2 * over);
imageMode(CENTER);
image(sprites, 0, 0, 48, 72, 24, 48, 24, 36);
pop();
for (let i = aliens.length - 1; i >= 0; i--) {
if (
aliens[i].died < 0 &&
dist(this.x, this.y, aliens[i].x, aliens[i].y) < 80
) {
gameover = true;
}
}
}
}
class Pew {
constructor(x) {
this.x = x;
this.y = height - 100;
this.gamespeed = -2.5;
this.hasHit = false;
}
display() {
this.y += this.gamespeed;
fill(255, 255, 0);
textAlign(CENTER, CENTER);
textSize(20);
text("|", this.x, this.y);
for (let i = aliens.length - 1; i >= 0; i--) {
if (dist(this.x, this.y, aliens[i].x + 24, aliens[i].y + 24) < 10) {
if (aliens[i].died < 0) {
aliens[i].died = frameCount;
score++;
this.hasHit = true;
}
}
}
}
}
function keyPressed() {
if (keyCode === UP_ARROW) {
if (pews.length < 6 && !gameover) {
pews.push(new Pew(phage.x));
}
} else if (key === "r") setup();
}
function keyReleased() {
keyCode = 0;
return false;
}