xxxxxxxxxx
192
// 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 MAX = 2;
let triggerTime;
function preload() {
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");
}
function setup() {
createCanvas(600, 600);
gameState = "START";
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,3))
))
}
bin = new Bin(0, 0, width, height);
}
function draw() {
if (gameState == "START") {
startGame();
} else if (gameState == "PLAY") {
playGame();
} else if (gameState == "WIN") {
winGame();
} else if (gameState == "OVER") {
overGame();
}
background(255);
if (trash.length < 1) {
trash.push(new Item(
random(100, width - 100),
0,
50,
50,
floor(random(1, 4)),
floor(random(1,3))
))
}
for (i = 0; i < trash.length; i++) {
trash[i].display();
trash[i].moveDown();
if (trash[i].y > height - 150) {
checkTrash();
trash.pop();
}
}
bin.show();
}
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 startGame() {}
function playGame() {}
function winGame() {}
function overGame() {
textAlign(CENTER);
textSize(20);
text("CLICK TO START GAME", width / 2, height / 2);
if (mousePressed == true) {
gameState = "PLAY";
}
}
function keyPressed() {
if (keyCode == RIGHT_ARROW) {
trash[0].moveRight();
}
// else if (keyCode == DOWN_ARROW) {
// y+=speed;
// }
else if (keyCode == LEFT_ARROW) {
trash[0].moveLeft();
} else if (key == " ") {
speed = 0;
}
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;
}
display() {
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, 90, 100);
}
if (this.randomImg == 2 && this.num == 1) {
image(can, this.x, this.y, 40, 30);
}
if (this.randomImg == 2 && this.num == 2) {
image(chocolate_wrap, this.x, this.y, 80, 40);
}
if (this.randomImg == 3 && this.num == 1) {
image(apple, this.x, this.y, this.w, this.h);
}
if (this.randomImg == 3 && this.num == 2) {
image(banana, this.x, this.y, this.w, this.h);
}
}
moveDown() {
this.y = this.y + speed;
}
moveRight() {
if (this.x < width - this.w - speed) {
this.x += 15;
}
}
moveLeft() {
if (this.x > 0 + this.w - speed) {
this.x -= 15;
}
}
// pressDown(){
// if (keyCode == DOWN_ARROW) {
// this.y+=speed;
// }
}
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);
// fill(255);
// rect(this.x,this.y, this.w,this.h)
}
}