xxxxxxxxxx
390
// At least one shape
// At least one image
// At least one sound
// At least one on-screen text
let gameState = 0;
let speed = 1.5;
let trash = [];
let score = 0;
let soundFiles = [];
let MAX = 2;
let timer = 60;
let triggerTime;
function preload() {
gameBG = loadImage("gameBG.png");
menuBG = loadImage("menuBG.png");
myFont = loadFont("SpecialElite-Regular.ttf");
paper = loadImage("assets/paper.png");
pizza_box = loadImage("assets/pizza_box.png");
letter = loadImage("assets/letter.png");
binImg = loadImage("assets/trashbins.png");
can = loadImage("assets/can.png");
chocolate_wrap = loadImage("assets/chocolate_wrap.png");
lid=loadImage("assets/lid.png")
banana = loadImage("assets/banana.png");
apple = loadImage("assets/apple.png");
pBottle = loadImage("assets/pbottle.png");
glass = loadImage("assets/tbottle.png");
note = loadImage("assets/note.png");
cup = loadImage("assets/coffee_cup.png");
soundBG = loadSound("sounds/BGsound.mp3");
click=loadSound("sounds/click.wav");
correct=loadSound("sounds/yay.mp3")
}
function setup() {
soundBG.loop();
soundBG.setVolume(0.5);
createCanvas(600, 600);
triggerTime = 300; //floor(random(70, 500));
for (i = 0; i < 1; i++) {
trash.push(
new Item(
random(100, width - 100),
0,
50,
50,
floor(random(1, 4)),
floor(random(1, 5))
)
);
}
bin = new Bin(0, 0, width, height);
}
function draw() {
print(mouseX, mouseY);
background(menuBG);
textFont(myFont);
textSize(40);
fill(243, 247, 101);
rect(width, height / 10, width / 2 - 260, height / 5 + 10);
fill(229, 235, 52);
rect(0, 10, width, height / 4);
fill(0);
text("Welcome to CLEAN & GREEN", width / 2 - 260, height / 5 - 20);
textSize(20);
// text("please read the instructions before starting the game", width/2-280, height/5+10)
textSize(30);
//menu
fill(229, 235, 52);
push();
rect(width / 2 - 120, height / 2 - 80, 240, 95, 25);
fill(0);
text("PLAY", width / 2 - 35, height / 2 - 20);
pop();
push();
rect(width / 2 - 120, height / 2 + 20, 240, 95, 25);
fill(0);
text("INSTRUCTIONS", width / 2 - 105, height / 2 + 80);
pop();
textSize(50);
fill(255);
textSize(26);
//play
if (gameState == 1) {
background(gameBG);
fill(0);
textSize(20);
text("0:" + timer, 50, 50);
text("score: " + score, 50, 70);
textSize(15);
text("Right Click to return to MENU", 350, 30);
if (trash.length < 1) {
trash.push(
new Item(
random(100, width - 100),
-40,
50,
50,
floor(random(1, 4)),
floor(random(1, 5))
)
);
}
for (i = 0; i < trash.length; i++) {
trash[i].display();
trash[i].moveDown();
if (trash[i].y > height + 100) {
trash.splice(i, 1);
}
if (trash.length > 0) {
if (
trash[i].x > 95 &&
trash[i].x < 180 &&
trash[i].y > 475 &&
trash[i].y < 560
) {
checkTrash();
trash.splice(i, 1);
}
if (trash.length > 0) {
if (
trash[i].x > 95 &&
trash[i].x < 180 &&
trash[i].y > 475 &&
trash[i].y < 560
) {
checkTrash();
trash.splice(i, 1);
} else if (
trash[i].x > 245 &&
trash[i].x < 365 &&
trash[i].y > 475 &&
trash[i].y < 560
) {
checkTrash();
trash.splice(i, 1);
} else if (
trash[i].x > 413 &&
trash[i].x < 560 &&
trash[i].y > 475 &&
trash[i].y < 560
) {
checkTrash();
score += 100;
trash.splice(i, 1);
}
}
}
}
bin.show();
if (frameCount % 60 == 0 && timer > 0) {
timer--;
}
if (timer == 0) {
gameState = 3;
}
}
if (mouseButton == RIGHT) {
gameState = 0;
}
// instructions
if (gameState == 2) {
background(88, 191, 84);
fill(229, 235, 52);
// rect(50,200,width-100,200);
image(note, 50, 100, width - 60, 400);
textSize(15);
fill(23, 102, 20);
text("Right Click to return to MENU", 350, 30);
textSize(20);
text(
" 1. Trash will fall from the top of the screen.",
60,
height / 2 - 60
);
text(
" 2. You should move the trash using arrow keys",
60,
height / 2 - 30
);
text(" to place it into the correct bin.", 60, height / 2);
text(" 3. You'll be given 1 minute to sort out", 60, height / 2 + 30);
text(" as much trash as you can.", 60, height / 2 + 60);
text(" Learn and Enjoy!", width / 2 - 80, height / 2 + 120);
if (mouseButton == RIGHT) {
gameState = 0;
}
}
// exit
if (gameState == 3) {
background(245, 114, 66);
textSize(50);
text("GAME OVER", width / 2 - 150, height / 2);
textSize(30);
text("Your final score is: " + score, width / 2 - 200, height / 2 + 50);
textSize(25);
// text(
// "Thank you for being interested in building a sustainable future!",
// width / 2 - 200,
// height / 2 + 100
// );
textSize(15);
text("Right Click to return to MENU", 350, 30);
}
}
function mouseClicked() {
if (gameState == 0) {
if (mouseX < 415 && mouseX > 191) {
if (mouseY < 309 && mouseY > 221) {
click.play()
reset();
gameState = 1;
}
if (mouseY < 411 && mouseY > 321) {
click.play()
gameState = 2;
}
// if (mouseY < 442 && mouseY > 379) {
// gameState = 3;
// }
}
}
}
function checkTrash() {
if (trash[0].x <= width / 3 && trash[0].randomImg == 3) {
score += 100;
print("correct Waste");
correct.play();
} else if (
trash[0].x > width / 3 &&
trash[0].x <= (2 * width) / 3 &&
trash[0].randomImg == 1
) {
print("correct Paper");
score += 100;
correct.play();
}
else if (trash[0].x > (2 * width) / 3 && trash[0].randomImg == 2) {
correct.play();
print("correct Cans");
score += 100;
}
else if(trash[0].x < 84 && trash[0].x > 197 && trash[0].x > 364 && trash[0].x < 247 && trash[0].x > 414 && trash[0].x < 532) {
score -= 100;
}
else {
print("incorrect bin");
if (score>0){
score -= 100;
}
}
}
function keyPressed() {
if (keyCode == RIGHT_ARROW) {
trash[0].moveRight();
} else if (keyCode == LEFT_ARROW) {
trash[0].moveLeft();
} else if (key == " ") {
speed = 0;
} else if (keyCode == DOWN_ARROW && trash.length > 0) {
trash[0].pressDown();
}
return false;
}
class Item {
constructor(posX, posY, w, h, itemType, itemNum) {
this.x = posX;
this.y = posY;
this.w = w;
this.h = h;
this.num = itemNum;
this.randomImg = itemType;
this.speed = random(1.5, 3);
this.gravity = true;
}
display() {
push();
imageMode(CENTER);
if (this.randomImg == 1 && this.num == 1) {
image(paper, this.x, this.y, this.w, this.h);
}
if (this.randomImg == 1 && this.num == 2) {
image(letter, this.x, this.y, this.w, this.h);
}
if (this.randomImg == 1 && this.num == 3) {
image(pizza_box, this.x, this.y, 80, 100);
}
if (this.randomImg == 2 && this.num == 1) {
image(can, this.x, this.y, 40, 30);
}
if (this.randomImg == 2 && this.num == 2) {
image(pBottle, this.x, this.y, 60, 50);
}
if (this.randomImg == 3 && this.num == 1) {
image(apple, this.x, this.y, this.w, 40);
}
if (this.randomImg == 3 && this.num == 2) {
image(banana, this.x, this.y, this.w, this.h);
}
if (this.randomImg == 3 && this.num == 3) {
image(glass, this.x, this.y, 70, 50);
}
if (this.randomImg == 3 && this.num == 4) {
image(chocolate_wrap, this.x, this.y, 90, 40);
}
pop();
}
moveDown() {
if (this.gravity == true) {
this.y += this.speed;
}
}
moveRight() {
if (this.x < width - this.w - this.speed) {
this.x += 50;
}
}
moveLeft() {
if (this.x > 0 + this.w - this.speed) {
this.x -= 50;
}
}
pressDown() {
if (this.gravity == true) {
this.y += speed * 45;
}
}
}
function reset() {
score = 0;
gameState = 0;
timer=60;
}
class Bin {
constructor(xLoc, yLoc, w, h) {
this.x = xLoc;
this.y = yLoc;
this.w = w;
this.h = h;
}
show() {
image(binImg, this.x, this.y, this.w, this.h);
}
}