Hello, I’ve wondering of how I want my ball to score into the brown box and how to make a ball disappear upon collision with the brown box to score a goal. And how to place a game over feature/screen when the ball it the ground.
int xPos; //position
int yPos;
int gravity = 1; //gravity
int speedX = 3; //speed
int speedY; //speed
int friction = 1; //friction
int ballRadius = 25; //ball radius
int hatX; //hat collision
int hatY = 390; //hat collision
int hatW = 150; //hat collision
int hatH = 4; //hat collision
boolean collide;
int score;
void setup() {
size(640, 480); //game size
}
void draw() {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
drawScore();
drawBasket();
}
void updateBall() {
//ball speed
speedY += gravity;
xPos += speedX;
yPos += speedY;
}
boolean ballHitsHat() {
// ball and hat collision detection
if (yPos + ballRadius > hatY && xPos + ballRadius > hatX && xPos + ballRadius < hatX + hatW) {
return true;
}
return false;
}
void checkBall() {
if (ballHitsHat()) { //ball bouncing losing gravity
yPos = hatY - ballRadius;
speedY *= -1;
}
if (xPos > width - ballRadius){//ball speed and collision
if (speedX > 0) speedX *= -1;
}
else if (xPos < ballRadius) {
if (speedX < 0) speedX *= -1;
}
if (yPos > height - ballRadius - 34) {//ball speed and collision
yPos = height - ballRadius - 34;
speedY *= -friction;
{
if(collide==false){
++score;
}
collide = true;
}
{
collide = false;
}
}
}
void drawBall() {//ball
fill(180);
stroke(0);
ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}
void drawScore(){
fill(0);
textSize(12);
text("Score: "+score, 10, 30); //display the score
}
void drawBasket(){
fill(165, 42, 42);//basket
noStroke();
rect(490,350,500,500);
}