xxxxxxxxxx
230
// A Senyan game
let sceneNum = 0;
let motherSpider = [];
let spiders = [];
let spiderCount;
let timestamp = 0;
let timestamp2 = 1000;
let GameTime;
let d;
let needRepopulate = false;
let bigSpiderImg;
let smallSpiderImg;
let slipper;
function preload() {
bigSpiderImg = loadImage('assets/bigSpider.png');
smallSpiderImg = loadImage("assets/smallSpider.png");
slipper = loadImage("assets/slipper.png");
}
function setup() {
createCanvas(600, 400);
motherSpider.push(new BigSpider());
for (i = 0; i < 10; i++) {
spiders[i] = new Spider(motherSpider[0].x, motherSpider[0].y);
}
}
function draw() {
background(255);
//determine which screen to show.
switch (sceneNum) {
case 0:
titleScreen();
break;
case 1:
spiderGame();
break;
case 2:
endScreen();
}
}
function titleScreen() {
background(0);
fill(255);
textAlign(CENTER);
textSize(20);
text('Spiders vs. Me', width / 2, height / 2)
textSize(13);
text('win by exterminating every spider', width / 2, height / 2 + 20);
text('click on spider to slap. Can you stop the infestation?', width / 2, height / 2 + 40);
textSize(20);
fill(81, 255, 0);
text('START', width / 2, height -30);
//reset();
// click anywhere to advance to first game
if (mouseIsPressed) {
sceneNum = 1;
}
}
function endScreen() {
fill(255);
textAlign(CENTER);
if (spiderCount > 100) {
background(0);
fill(255, 0, 0);
// x spiders have taken over this apartment!!
text(`too late, 100 spiders took over the apartment in ${GameTime} seconds!!`, width / 2, height/2)
} else if (spiderCount == 0) {
// you eradicated x spiders in x minutes and y seconds!
background(0);
fill(149, 255, 0);
text(`Amazing! You eradicated all the spiders from the apartment in ${GameTime} seconds!`, width / 2, height / 2)
}
text('Press ANY KEY to try again', width / 2, height -50);
if (spiderCount != 0) {
spiders.splice(0, spiders.length)
motherSpider.splice(0, motherSpider.length)
}
// press any key to restart game
if (keyIsPressed) {
//repopulate the motherSpider and spiders arrays
// empty the arrays first.
needRepopulate = true;
sceneNum = 1;
}
}
//first game depicts my anachrophobia. I am afraid that spiders will take over my apartment..
function spiderGame() {
spiderCount = spiders.length + motherSpider.length;
if (spiderCount == 0 && needRepopulate) {
console.log("repopulate spiders!")
motherSpider.push(new BigSpider());
for (i = 0; i < 10; i++) {
spiders[i] = new Spider(motherSpider[0].x, motherSpider[0].y);
}
needRepopulate = false;
}
rectMode(CENTER);
//add 1 mother Spider every 4 seconds
if (millis() - timestamp2 > 4000) {
timestamp2 = millis();
motherSpider.push(new BigSpider())
}
//check if mother spider is still alive: if so,
//every second, add 2 more small spidesrs to the array
if (millis() - timestamp > 1000) {
timestamp = millis();
//if there is any mother spider on the screen, it will give birth to baby spiders
if (motherSpider[0] != null) {
for (i = 0; i < motherSpider.length; i++) {
spiders.push(new Spider(motherSpider[i].x, motherSpider[i].y));
}
}
}
//every four seconds, add a big spider
for (let i = 0; i < spiders.length; i++) {
// locationVector = createVector(width/10, height/10);
//console.log(locationVector)
spiders[i].scurry();
spiders[i].distToSwap(i);
}
for (let i = 0; i < motherSpider.length; i++) {
motherSpider[i].keepOrRemove(i);
}
//mouse cursor turns to a swap
fill(24, 43, 133);
//rect(mouseX, mouseY, 30, 40);
image(slipper, mouseX, mouseY, slipper.width*0.1, slipper.height*0.1);
//check how many spiders are on canvas, if more than 100, game over and the spiders have taken over.
keepScore();
}
//second game reflects on my fear of covid
//third game is built on my claustrophobia
class BigSpider {
constructor() {
this.x = random(20, width - 20);
this.y = random(20, height - 20);
this.c = color(0);
}
keepOrRemove(index) {
d = dist(mouseX, mouseY, motherSpider[index].x, motherSpider[index].y);
if (d < 20 && mouseIsPressed) {
motherSpider.splice(index, 1);
} else {
fill(0);
}
if (motherSpider[index] != null) {
motherSpider[index].body();
}
}
body() {
fill(this.c);
// ellipse(this.x, this.y, 20, 20);
imageMode(CENTER);
image(bigSpiderImg, this.x, this.y, bigSpiderImg.width*0.3, bigSpiderImg.height*0.3);
}
}
class Spider {
constructor(x, y) {
this.x = x;
this.y = y;
this.c = color(0, 0, 0);
this.w = 5;
this.h = 5;
this.xSpeed = random(-1, 2);
this.ySpeed = random(-1, 1.5);
}
scurry() {
if (this.x < 30 || this.x > width - 30) {
this.xSpeed *= -1;
}
if (this.y < 30 || this.y > height - 30) {
this.ySpeed *= -1;
}
this.x += random(this.xSpeed * 2.2);
this.y += random(this.ySpeed * 2.5);
}
show() {
fill(0);
stroke(0);
//ellipse(this.x, this.y, this.w, this.h);
image(smallSpiderImg, this.x, this.y, smallSpiderImg.width*0.2, smallSpiderImg.height*0.2);
}
distToSwap(index) {
d = dist(mouseX, mouseY, spiders[index].x, spiders[index].y);
if (d < 20 && mouseIsPressed) {
spiders.splice(index, 1);
} else {
fill(0);
}
if (spiders[index] != null) {
spiders[index].show();
}
}
}
function keepScore() {
textSize(15);
text(`there are now ${spiderCount} freaking spiders!`, width / 5, height - 10);
// if the spiderCount variable
if ((spiderCount > 100 || spiderCount == 0) && !needRepopulate) {
GameTime = floor(millis()/1000);
sceneNum = 2;
}
}
// function reset(){
// for (i = 0; i < 10; i++) {
// spiders[i] = new Spider();
// }
// motherSpider.push(new BigSpider(10, 20, 30, 30));
// sceneNum = 1;
// }