xxxxxxxxxx
165
let BennyFish;
let bennyWidth;
let bennyHeight;
let Logo;
let logoWidth;
let x;
let y;
let speed = 5;
let bottles = [];
let canPlastic = [];
let bag = [];
function preload() {
BennyFish = loadImage("Media/BennyFish.png");
Logo = loadImage("Media/Logo.PNG");
}
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() {
background(255);
// 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 //////////////////////////////////////////
// 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 ///////////////////////////////////////
// INSTRUCTIONS /////////////////////////////////////
push();
textFont("Courier New");
textSize(20);
textAlign(CENTER);
// text("use the keyboard arrows", width*0.5, height*0.80);
//text(" to help Benny reach the sea", width*0.5, height*0.85);
pop();
// INSTRUCTIONS /////////////////////////////////////
// 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 /////////////////////////////////////
image(BennyFish, x, y, bennyWidth, bennyHeight);
image(Logo, width/2, height*0.35, logoWidth, logoWidth);
}
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() {
push();
if (dist(x, y, this.posX, this.posY) < this.villainWidth / 2 + 25) {
fill(255, 0, 0);
x = width / 2;
y = height*0.75 ;
}
noStroke();
ellipse(x, y, 50);
pop();
}
}