Just fixed the ball glitch for your character now your other problem is your game crash when the ball collides with the box. Sorry for the auto correction.
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;
int basketH;
int basketW;
int basketX;
int basketY;
boolean hide;
boolean hitFloor;
void setup() {
size(640, 480); //game size
ballRadius = 25;
basketX = 500;
basketY = 100;
basketW = 100;
basketH = 150;
hide = false;
hitFloor = false;
score = 0;
}
void draw() {
ballHitsHat();
if( ballHitsBasket() ){
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
drawBasket();
gameOver();
}
void updateBall() {
//ball speed
speedY += gravity;
xPos += speedX;
yPos += speedY;
}
boolean ballHitsBasket() {
// ball and basket collision detection
if (xPos + ballRadius >= basketX && xPos <= basketX + basketW &&
yPos + ballRadius >= basketY && yPos <= basketY + basketH) {
score++;
return true;
}
return false;
}
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;
}
}
void drawBall() {//ball
fill(180);
stroke(0);
ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}
void drawBill() {
hatX = mouseX - 45;//bouncing hat
background(44, 56, 245);//sky
fill(2, 188, 34);
noStroke();
rect(0, 420, width, height);//grass
fill(255, 224, 189);
rect(mouseX, 400, 60, 50);//head
fill(210, 105, 30);
rect(mouseX+10, 420, 15, 5); //left eye
rect(mouseX+35, 420, 15, 5); //right eye
fill(0);
ellipse(mouseX+30, 436, 10, 2); //mouth
fill(0);
rect(mouseX-15, 390, 90, 10); //hat
fill(165, 42, 42);
rect(mouseX-3, 400, 65, 10); //hair
fill(255, 0, 0);
rect(mouseX+10, 450, 40, 20); //body
fill(0);
rect(mouseX+20, 470, 5, 10); //left leg
rect(mouseX+35, 470, 5, 10); //right leg
}
void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
void drawBasket() {
fill(165, 42, 42);//basket
noStroke();
rect(basketX, basketY, basketW, basketH);
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}