/*
catch ball
this is a toy where you shoot a ball towards a bin that is moving based on your mouse position
interact by clicking the mouse and aiming for the bin
Conor Norman 991477800
*/
float binX;
float binY;
float ballX;
float ballY;
float speedY = 0;
float speedX = 0;
float gravity = 1;
boolean shot = false;
boolean reset = true;
int stateX = 0;
int stateY = 0;
float mousePressedX;
float mousePressedY;
void setup() {
size(400, 400);
initializeDrawings();
}
void draw() {
background(0);
updateBallPos();
updateBinPos();
drawLoop();
drawBin();
drawBall();
drawPlatform();
}
//places shapes into original position
void initializeDrawings() {
ballX = 60;
ballY = 290;
binX = 250;
binY = 250;
}
//resets ball to original position with no speed to it
void resetBall() {
ballX = 60;
ballY = 290;
shot = false;
reset = true;
speedX = 0;
speedY = 0;
}
//constantly updates ball speed and position
void updateBallPos() {
//allows player to shoot once until reset
if (mousePressed == true) {
shot = true;
}
if (shot == true) {
//gives my ball a velocity
ballX += speedX;
ballY -= speedY;
//checks if ball is in the bin
inBin();
}
//update the speed every frame
speedY = speedY - gravity;
}
//constantly updates bin position and keep it on screen
void updateBinPos() {
///////////////////
//building off learning processing, example 5-8
//////////////////
if (stateX == 0) {
binX = binX + 0.35;
if (binX > width-70) {
binX = width - 70;
stateX = 1;
}
} else if (stateX == 1) {
binX = binX - 0.35;
if (binX < 150) {
binX = 150;
stateX = 0;
}
}
if (stateY == 0) {
binY = binY + 0.7;
if (binY > 350) {
binY = 350;
stateY = 1;
}
} else if (stateY == 1) {
binY = binY - 0.7;
if (binY < 100) {
binY = 100;
stateY = 0;
}
}
}
//checks if the ball is in the bin or off screen
void inBin() {
if (ballX >= binX+15 && ballX <= binX+65 && ballY <= binY && ballY > binY-20) {
println("good shot");
resetBall();
} else if (ballX > width || ballY > height) {
println("miss");
resetBall();
}
}
//when the mouse is pressed grab position of where it was clicked
void mousePressed() {
if (shot == false && reset == true) {
reset = false;
mousePressedX = (mouseX-ballX)/24;
mousePressedY = (ballY-mouseY)/10;
speedX = mousePressedX;
speedY = mousePressedY;
}
}
//draws bin shape
void drawBin() {
rectMode(CORNER);
fill(135, 61, 0);
noStroke();
rect(binX, binY, 80, 15);
rect(binX, binY-30, 15, 40);
rect(binX+65, binY-30, 15, 40);
}
//draws ball shape
void drawBall() {
fill(20, 204, 147);
ellipseMode(CENTER);
ellipse(ballX, ballY, 15, 15);
}
//draws platform shape
void drawPlatform() {
fill(0, 152, 178);
quad(50, 360, 70, 360, 70, 300, 50, 300);
}
//creates a background that changes color with every loop
void drawLoop() {
strokeWeight(30);
for (int x=0; x<height; x=x+30) {
for (int y=255; y>0; y=y-30) {
stroke(x+110, x, y);
line(0, x, width, x);
}
}
}