xxxxxxxxxx
227
// 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() {
background(255);
if (trash.length<1) {
trash.push(new Item(
random(100, width - 100),
-40,
50,
50,
floor(random(1, 4)),
floor(random(1,3))
))
}
for (i = 0; i < trash.length; i++) {
trash[i].display();
trash[i].moveDown();
// if (mouseIsPressed){
// trash[i].checkClicked();
// }
if (mouseIsPressed){
// trash[i].checkClicked();
}
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);
}
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();
trash.splice(i,1);
}
}
}
bin.show();
print(mouseX, mouseY);
//95 475
//180 560
}
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 (mouseIsPressed == 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;
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, 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);
}
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 += 15;
}
}
moveLeft() {
if (this.x > 0 + this.w - this.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)
}
}