xxxxxxxxxx
236
let mode = 0;
let BennyFish;
let bennyWidth;
let bennyHeight;
let Logo;
let logoWidth;
let x;
let y;
let speed = 5;
let lives = 3;
let bottles = [];
let canPlastic = [];
let bag = [];
function preload() {
BennyFish = loadImage("Media/BennyFish.png");
Logo = loadImage("Media/Logo.PNG");
BackgroundSea = loadImage("Media/Bowl.jpg");
BackgroundIntro = loadImage("Media/Empty.jpg");
}
function setup() {
createCanvas(600, 500);
imageMode(CENTER);
bennyWidth = 70;
bennyHeight = 50;
x = width / 2;
y = height * 0.65;
logoWidth = 450;
// VILLAINS SET UP ///////////////////////////////////
//BOTTLES
for (let i = 0; i < 5; i++) {
let y = random(0, height);
bottles.push(new Villain(width, y, 0, 30, random(2.3, 3.2)));
}
//CAN PLASTIC
for (let i = 0; i < 7; i++) {
let y = random(0, height);
canPlastic.push(new Villain(width, y, 150, 50, random(0.1, 0.5)));
}
// VILLAINS SET UP ///////////////////////////////////
}
function draw() {
let s;
switch (mode) {
case 0:
intro();
s = "Intro Screen";
break;
case 1:
sea();
s = "Sea";
break;
case 2:
lose();
s = "Lose";
break;
case 3:
win();
s = "Win";
break;
}
// BOUNDS //////////////////////////////////////////
//LEFT BOUND
if (x < bennyWidth / 2 + 4) {
x += speed + 0.2;
//BUMP SOUND HERE
}
//RIGHT BOUND
if (x > width - (bennyWidth / 2 + 4)) {
x -= speed + 0.2;
//BUMP SOUND HERE
}
//UPPER BOUND
if (y < bennyHeight / 2 + 4) {
y += speed + 0.2;
//BUMP SOUND HERE
}
//RIGHT BOUND
if (y > height - (bennyHeight / 2 + 4)) {
y -= speed + 0.2;
//BUMP SOUND HERE
}
// BOUNDS //////////////////////////////////////////
image(BennyFish, x, y, bennyWidth, bennyHeight); // THIS IS WHERE I DRAW THE MAIN CHARACTER
}
class Villain {
constructor(x, y, c, w, s) {
this.posX = x;
this.posY = y;
this.speed = s;
this.villainWidth = w;
this.villainColor = c;
}
run() {
this.drawVillain();
this.moveVillain();
this.killBenny();
}
drawVillain() {
noFill();
stroke(this.villainColor); // TO CHANGE
circle(this.posX, this.posY, this.villainWidth);
}
moveVillain() {
this.posX -= this.speed;
if (this.posX < 0 - this.villainWidth / 2) {
this.posX = width + this.villainWidth / 2;
this.posY = random(0, width);
}
}
killBenny() { // WHEN BENNY TOUCHES VILLAINS
push();
if (dist(x, y, this.posX, this.posY) < this.villainWidth / 2 + 25) {
fill(255, 0, 0);
lives -= 1; // LIVES COUNTER
mode = 2;
x = width / 2;
y = height / 2 ;
}
noStroke();
ellipse(x, y, 50);
pop();
}
}
function intro() {
// AESTHETICS /////////////////////////////////////
background(255);
image(BackgroundIntro, width / 2, height / 2, width, height);
image(Logo, width / 2, height * 0.35, logoWidth, logoWidth);
// AESTHETICS /////////////////////////////////////
// INSTRUCTIONS /////////////////////////////////////
push();
textFont("Courier New");
textSize(20);
textAlign(CENTER);
text("use the keyboard arrows", width * 0.5, height * 0.8);
text(" to help Benny grow & reach the sea", width * 0.5, height * 0.85);
pop();
// INSTRUCTIONS /////////////////////////////////////
x = width / 2;
y = height * 0.65;
}
function sea() {
background(255);
image(BackgroundSea, width / 2, height / 2, width, height);
push();
textFont("Courier New");
textSize(30);
textAlign(CENTER);
fill(0);
text(lives, width * 0.95, height * 0.08);
pop();
// CONTROLS ///////////////////////////////////////
if (isKeyPressed == true) {
if (keyCode == UP_ARROW) {
y -= speed;
}
if (keyCode == DOWN_ARROW) {
y += speed;
}
if (keyCode == LEFT_ARROW) {
x -= speed;
}
if (keyCode == RIGHT_ARROW) {
x += speed;
}
}
// CONTROLS ///////////////////////////////////////
// VILLAIN DRAW /////////////////////////////////////
for (let i = 0; i < bottles.length; i++) {
bottles[i].run();
}
for (let i = 0; i < canPlastic.length; i++) {
canPlastic[i].run();
}
// VILLAIN DRAW /////////////////////////////////////
}
function lose() {
print("lose");
background(27, 43, 102, 5);
if (lives < 1) {
mode = 0;
lives = 3;
}
}
function win() {
1;
print("win");
}
function keyPressed() {
if (
keyCode === LEFT_ARROW ||
keyCode === RIGHT_ARROW ||
keyCode === UP_ARROW ||
keyCode === DOWN_ARROW
) {
mode = 1;
}
}