xxxxxxxxxx
316
let gameState = 0;
let speed = 1.5;
let trash = [];
let score = 0;
let MAX = 2;
let timer=10;
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");
banana = loadImage("assets/banana.png");
apple = loadImage("assets/apple.png");
pBottle=loadImage("assets/pbottle.png");
glass=loadImage("assets/tbottle.png");
}
function setup() {
createCanvas(600, 600);
triggerTime = 300;
for (i = 0; i < 1; i++) {
trash.push(new Item( random(100, width - 100), 0, 50, 50, floor(random(1, 4)), floor(random(1, 4))));
}
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);
fill(0);
rect()
text("Welcome to CLEAN & GREEN", width/2-260, height/5)
textSize(20)
text("please read the instructions before starting the game", width/2-280, height/5+30)
textSize(25)
//menu
fill(229, 235, 52);
push()
rect(width / 2 - 120, height/4+40, 240, 75);
fill(0)
text("PLAY", width/2-30, height/4+90);
pop()
push()
rect(width / 2 - 120,height/2-20, 240, 75);
fill(0)
text("INSTRUCTIONS", width/2-90, height/2+25);
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),-10,50,50, floor(random(1, 4)), floor(random(1,4))))
}
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();
score+=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();
score+=100;
trash.splice(i,1);
}
else if (trash[i].x>245 && trash[i].x<365 && trash[i].y>475 && trash[i].y<560){
checkTrash();
score+=100;
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(width/2,200,200,200)
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.", 20, width/2-30);
text("2. You should move the trash to place it into the correct trash bin using arrow keys", 20, width/2);
text("You'll be given 60 seconds to collect as much trash as you can.", 20, width/2+30);
text("3. Thank you for being interested in building a sustainable future! Learn and Enjoy!.", 20, width/2+60)
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(15);
text("Right Click to return to MENU", 350, 30);
}
}
function mouseClicked() {
if (gameState == 0) {
if (mouseX < 418 && mouseX > 182) {
if (mouseY < 264 && mouseY > 189) {
reset()
gameState = 1;
}
if (mouseY < 353 && mouseY > 281) {
gameState = 2;
}
}
}
}
function checkTrash() {
if (trash[0].x <= width / 3 && trash[0].randomImg == 3) {
print("correct Waste");
} else if (
trash[0].x > width / 3 &&
trash[0].x <= (2 * width) / 3 &&
trash[0].randomImg == 1
) {
print("correct Paper");
} else if (trash[0].x > (2 * width) / 3 && trash[0].randomImg == 2) {
print("correct Cans");
} else {
print("incorrect bin");
}
}
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[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, 70, 50);
}
if (this.randomImg == 2 && this.num == 3) {
image(chocolate_wrap, this.x, this.y, 80, 40);
}
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, 50, 50);
}
pop()
}
// checkClicked(){
// if (dist(mouseX, mouseY, this.x, this.y)<30){
// this.gravity == false;
// this.x=mouseX;
// this.y=mouseY;
// }
// }
moveDown() {
if (this.gravity == true) {
this.y = 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=20;
}
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);
}
}