xxxxxxxxxx
113
function setup() {
createCanvas(400, 400);
ellipseMode(CORNER);
trashY = 0
trashIsFalling = false
basketWidth = 75
basketHeight = 275
trashCaught = false;
}
function draw() {
background(220);
//floor
fill(175);
rect(-10, 300, 500, 150)
//add sprites
addBasket(mouseX, basketHeight);
addWindow(0, 0);
basketX = mouseX + basketWidth
if (trashIsFalling) {
//add trash and speed
addTrash();
trashY = trashY + trashSpeed
//test for collision
let trashIsAtBasketHeight = (trashY >= basketHeight);
let trashIsLeftOfBasket = (trashX < basketX - basketWidth);
let trashIsRightOfBasket = (trashX > basketX);
if (trashIsAtBasketHeight && !trashIsLeftOfBasket && !trashIsRightOfBasket) {
trashCaught = true;
}
//if missed, trash resets
}
if (trashY > 360) {
trashIsFalling = false;
trashY = 0;
}
}
function mousePressed() {
if (!trashIsFalling) {
trashIsFalling = true;
trashX = random(20, 380);
trashSpeed = random(4, 12);
}
}
function addBasket(x, y) {
push();
translate(x, y);
noStroke();
//Basket Body Shapes
fill(138, 114, 91);
push();
drawingContext.shadowOffsetX = 0;
drawingContext.shadowOffsetY = 2;
drawingContext.shadowBlur = 10;
drawingContext.shadowColor = 'black';
ellipse(15, 77, 45, 6);
pop();
quad(0, 8, 15, 80, 60, 80, 75, 8);
//Basket Opening Ellipse
stroke(0);
if (trashCaught) {
fill(255, 239, 97);
ellipse(0, 0, basketWidth, 16);
trashCaught = false;
trashIsFalling = false;
trashY = 0;
} else {
fill(54, 45, 38); //brown
ellipse(0, 0, basketWidth, 16);
}
pop();
}
function addWindow(x, y) {
push();
translate(x, y);
strokeWeight(1);
//background gray
fill(130);
rect(75, 75, 250, 125);
//outside sky
fill('skyblue');
rect(85, 80, 230, 105);
//corner lines
line(75, 75, 85, 80);
line(325, 75, 315, 80);
line(75, 200, 85, 185);
line(315, 185, 325, 200);
//cross rect
fill(130);
rect(85, 130, 230, 8);
//window shine
strokeWeight(4);
stroke(240);
line(100, 120, 120, 95);
line(110, 120, 123, 105);
pop();
}
function addTrash() {
push();
ellipseMode(CENTER);
strokeWeight(2);
fill(240);
if (trashY >= 350) {
ellipse(trashX, 350, 60, 15);
} else {
ellipse(trashX, trashY, 20, 20);
}
pop();
}