xxxxxxxxxx
340
// 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() {
font=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");
}
function setup() {
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, 3))
)
);
}
bin = new Bin(0, 0, width, height);
}
function draw() {
//print(mouseX, mouseY)
background(255);
//menu
fill(0, 255, 0);
rect(width / 2 - 100, 150, 200, 75);
text("START", 70, 106);
fill(255, 0, 255);
rect(width / 2 - 100, 250, 200, 75);
text("INSTRUCTIONS", 52, 248);
fill(255, 0, 0);
rect(width / 2 - 100, 350, 200, 75);
text("EXIT", 94, 406);
textSize(50);
fill(255);
textSize(26);
//start
if (gameState == 1) {
background(0, 255, 0);
fill(0);
textSize(20);
text("Right Click to return to MENU", 525, 30);
if (mouseButton == RIGHT) {
gameState = 0;
}
}
// instructions
if (gameState == 2) {
background(255, 0, 255);
textSize(20);
text("Right Click to return to MENU", 525, 30);
textSize(30);
text("1. Rocks will fall from the top of the screen.", 50, 150);
text("2. Move your character using arrow keys", 50, 200);
text("<- and -> to avoid being crushed.", 80, 240);
text("3. The game is over when a rock hits you.", 50, 290);
if (mouseButton == RIGHT) {
gameState = 0;
}
} // exit
if (gameState == 3) {
background(255, 0, 0);
textSize(75);
text("COME AGAIN SOON!", 25, height / 2);
} // EXIT
}
function mouseClicked() {
if (gameState == 0) {
if (mouseX < 400 && mouseX > 200) {
if (mouseY < 222 && mouseY > 148) {
gameState = 1;
}
if (mouseY < 325 && mouseY > 250) {
gameState = 2;
}
if (mouseY < 420 && mouseY > 350) {
gameState = 3;
}
}
}
}
// 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 (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();
// trash.splice(i,1);
// }
// }
// }
// }
// bin.show();
// print(mouseX, mouseY);
// //95 475
// //180 560
// if (mousePressed=true){
// gameState="PLAY"
// }
function startGame() {
textMode(CENTER);
textSize(50);
fill(255, 0, 0);
text("Click to PLay!", width / 2, height / 2);
textSize(20);
fill(100, 0, 0);
text(
"use arrow keys to throw trash into the right bin",
width / 2,
height / 2 + 30
);
if (mousePressed == true) {
gameState = "PLAY";
}
// textMode(CENTER);
// fill(0, 255, 0);
// rect(50, 50, 200, 75);
// text('START', 70, 106);
// fill(255, 0, 255);
// rect(50, 200, 200, 75);
// text('INSTRUCTIONS', 52, 248);
// fill(255, 0, 0);
// rect(50, 350, 200, 75);
// text('EXIT', 94, 406);
// textSize(50);
// fill(255,0,0)
}
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)
}
}