# How to fix my scoring code and how to input a game over feature/screen when the ball hit the ground?

All good

and say (pseudo code)

``````if( ballX > basketX &&
goal++;
state=1;
}
``````

Does a boolean will work as a new fuction for the checkBall?

You can also use a void function

The if clause is implemented in the function as I‘ve shown above

(OR use a Boolean function and use the if in draw())

I’ve decided to put in my void drawBasket code and this is the total update but I’ve know I got my whole code not operational. I’m just using it as an example.

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;
int ballX;
int ballY;

void setup() {
size(640, 480);

}

void draw() {

updateBall();
checkBall();
drawBill();
drawBall();
drawScore();

}

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
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){

}
collide = true;
}
{
collide = false;
}
}

}

void drawBall() {//ball

fill(180);
stroke(0);
}

void drawScore(){
fill(0);
textSize(12);
text("Score: "+score, 10, 30); //display the score

}
noStroke();
rect(490,350,500,500);

score++;
score=1;
}
}

``````

What doesn’t work?

Chrisir

Hi! @grodothefrog i understand debugging is frustrating
is it the entire sketch? i cannot find `drawBill()` in there. please post the working sketch so everyone can run it.

this >The project has ended

is it the `drawBill()` there? sorry for random guessing

1 Like

Hi there! From what you have told us here I fixed up your collision with the ‘basket’ and the ball disappears after collision. I also added the Game Over screen you wanted when it collides with the floor for further questions let me know!

Quick note: Conventionally you should create a class for a ball object and basket object this will make multiple instances of them easier as each object will inherit all of its logic and variables from the class it has been created in. This will allow you multiple balls colliding with multiple baskets easily where you will only be calling each ball and basket with their appropriate methods and making hiding them. This will only take a few lines rather than make the logic and position for each and every single ball and basket.

Here’s the code:

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480);
hide = false;
hitFloor = false;
score = 0;
}

void draw() {
background(0);
if ( ballHitsHat() ) {
hide = true;
xPos = -100;//Put the ball off screen
yPos = -100;
}
//When ball hits the box it disappears
if (!hide) {
drawBall();
updateBall();
checkBall();
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;
xPos   += speedX;
yPos   += speedY;
}

boolean ballHitsHat() {
// ball and hat collision detection
score++;
return true;
}
return false;
}

void checkBall() {

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;
hitFloor = true;
}
}

void drawBall() {//ball

fill(255, 0, 0);
stroke(0);
}

void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````
1 Like

Thanks you very much for the code. But I’ve actually forgot to send you my character code as it meant to bounce the ball on his head and make the ball bounce into the basket. I’m very sorry about that and when I try to implement him into your code. It end up crashing. The reason of how I don’t have my character as I’ve put it in a separate tab.

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
hide = false;
hitFloor = false;
score = 0;

}

void draw() {

if ( ballHitsHat() ) {
hide = true;
xPos = -100;//Put the ball off screen
yPos = -100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

boolean ballHitsHat() {
// ball and hat collision detection
score++;
return true;
}
return false;
}

void checkBall() {

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;
hitFloor = true;
}
}

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);

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 drawBall() {//ball

fill(180);
stroke(0);
}

void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}

``````

@grodothefrog Yea I saw that was missing and no problem ! Again I highly recommend making classes for this kind of problem to simplify code for yourself but here’s the finished product. ( P.s. The ball can get under the paddle causing it to bug out and total up many scores. )

Anyways here’s the code:

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
//
hatW = 90;
hatH = 10;
hatX = mouseX - 15;
hatY = 390;
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
return true;
}
return false;
}
boolean ballHitsHat() {
// ball and basket collision detection
if (xPos + ballRadius >= hatX && xPos <= hatX + hatW &&
yPos + ballRadius >= hatY && yPos <= hatY + hatH) {
score++;
speedY*= - friction;
return true;
}
return false;
}

void checkBall() {

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;
hitFloor = true;
}
}

void drawBill() {

//hatX = mouseX - 45;//bouncing hat
hatX = mouseX - 15;
background(44, 56, 245);//sky
fill(2, 188, 34);
noStroke();
rect(0, 420, width, height);//grass

fill(255, 224, 189);

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(hatX, hatY, hatW, hatH); //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 drawBall() {//ball

fill(180);
stroke(0);
}

void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````
2 Likes

After many attempt on fixing the code. Is there a possible way for the ball not to glitch and make the game not crash upon colliding with the brown box. Also I want the ball to disappear when it collide with the box and respawn at the same spot where the ball spawn.
And there another step to input a game over screen and the entire game end and in order to activate the game is by press the screen and the game start over again. I’ve also change the location of the box to the position I’ve desired but the game crash when it collided in the air on top of the box and also the ball run under my character and I’ve wondered how to repaired it. Thanks.

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
//
hatW = 90;
hatH = 10;
hatX = mouseX - 15;
hatY = 390;
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
score++;
return true;
}
return false;
}
boolean ballHitsHat() {
// ball and basket collision detection
if (xPos + ballRadius >= hatX && xPos <= hatX + hatW &&
yPos + ballRadius >= hatY && yPos <= hatY + hatH) {

speedY*= - friction;
return true;
}
return false;
}

void checkBall() {

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;
hitFloor = true;
}
}

void drawBill() {

//hatX = mouseX - 45;//bouncing hat
hatX = mouseX - 15;
background(44, 56, 245);//sky
fill(2, 188, 34);
noStroke();
rect(0, 420, width, height);//grass

fill(255, 224, 189);

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(hatX, hatY, hatW, hatH); //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 drawBall() {//ball

fill(180);
stroke(0);
}

void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
noStroke();
rect(490,350,500,500);
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````

Here the code by the way.
After many attempt on fixing the code. Is there a possible way for the ball not to glitch and make the game not crash upon colliding with the brown box. Also I want the ball to disappear when it collide with the box and respawn at the same spot where it spawn.
There another step I’ve want is by input a game over screen and the entire game end and in order to activate the game is by press the screen and the game start over again. I’ve also change the location of the box to the position I’ve desired but the game crash when it collided in the air on top of the box and also the ball run under my character and I’ve wondered how to repaired it. Thanks.

``````int xPos;
int yPos;

int gravity = 1;
int speedX = 3;
int speedY;

int friction = 1;

int hatX;
int hatY = 390;
int hatW = 150;
int hatH = 4;

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
//
hatW = 90;
hatH = 10;
hatX = mouseX - 15;
hatY = 390;
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
score++;
return true;
}
return false;
}
boolean ballHitsHat() {
// ball and basket collision detection
if (xPos + ballRadius >= hatX && xPos <= hatX + hatW &&
yPos + ballRadius >= hatY && yPos <= hatY + hatH) {

speedY*= - friction;
return true;
}
return false;
}

void checkBall() {

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;
hitFloor = true;
}
}

void drawBill() {

//hatX = mouseX - 45;//bouncing hat
hatX = mouseX - 15;
background(44, 56, 245);//sky
fill(2, 188, 34);
noStroke();
rect(0, 420, width, height);//grass

fill(255, 224, 189);

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(hatX, hatY, hatW, hatH); //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 drawBall() {//ball

fill(180);
stroke(0);
}

void drawScore() {
fill(255, 255, 0);
textSize(12);
text("Score: "+score, 10, 50); //display the score
}
noStroke();
rect(490,350,500,500);
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````
1 Like

Thanks for the code by the way but I’m having trouble adjusting it and there still problems. But many thanks.

In which line is that? Which line is responsible? Spontaneously I‘d say that is because of using *-1 because this can lead to stuttering also. Instead of checking both sides together, better check separately and then say speed=abs(speed); // always positive
when you need positive value.

When you need negative:

speed=abs(speed)*-1; // always negative

Explanation: usage of *-1 can turn the sign in minus and plus fast when the ball is still in the Return Zone where the condition is true.

1 Like

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 hatX; //hat collision
int hatY = 390; //hat collision
int hatW = 150; //hat collision
int hatH = 4; //hat collision

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
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
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);
}

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);

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
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````

Thanks for the help Robin. But still the ball make the game crash when it hit the basket and also how to fix the basket by putting it on the ground and also how to make another screen by just saying game over. And press the mouse to activate the game again. One last request is by spawning 5 balls and bounced them to the basket and make them into a loop. Also
The initial x-velocity of each pudding must be a random number between 1.15 and 1.2
The initial y-velocity of each pudding must be a random number between 0.2 and 1.2
I wonder how to put in those?

``````int xPos; //position
int yPos;

int gravity = 1; //gravity
int speedX = 3; //speed
int speedY; //speed

int friction = 1; //friction

int hatX; //hat collision
int hatY = 390; //hat collision
int hatW = 150; //hat collision
int hatH = 4; //hat collision

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
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
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);
}

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);

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
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````

Just eliminated the problem but having another problem is the collision with the basket. The ball make the game crash when it hit the basket and also how to fix the basket by putting it on the ground and also how to make another screen by just saying game over. And press the mouse to activate the game again. One last request is by spawning 5 balls and bounced them to the basket and make them into a loop. Also
The initial x-velocity of each balls must be a random number between 1.15 and 1.2
The initial y-velocity of each balls must be a random number between 0.2 and 1.2
I wonder how to put in those?

``````int xPos; //position
int yPos;

int gravity = 1; //gravity
int speedX = 3; //speed
int speedY; //speed

int friction = 1; //friction

int hatX; //hat collision
int hatY = 390; //hat collision
int hatW = 150; //hat collision
int hatH = 4; //hat collision

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
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
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);
}

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);

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
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````

Just eliminated the problem but having another problem is the collision with the basket. The ball make the game crash when it hit the basket and also how to fix the basket by putting it on the ground and also how to make another screen by just saying game over. And press the mouse to activate the game again. One last request is by spawning 5 balls and bounced them to the basket and make them into a loop. Also
The initial x-velocity of each ball must be a random number between 1.15 and 1.2
The initial y-velocity of each ball must be a random number between 0.2 and 1.2
I wonder how to put in those?

``````int xPos; //position
int yPos;

int gravity = 1; //gravity
int speedX = 3; //speed
int speedY; //speed

int friction = 1; //friction

int hatX; //hat collision
int hatY = 390; //hat collision
int hatW = 150; //hat collision
int hatH = 4; //hat collision

boolean collide;
int score;

boolean hide;
boolean hitFloor;

void setup() {
size(640, 480); //game size
hide = false;
hitFloor = false;
score = 0;
}

void draw() {

ballHitsHat();
hide = true;
xPos = - 100;
yPos = - 100;
}
if (!hide) {
updateBall(); //update the ball movement
checkBall(); //checking ball movement
drawBill(); //draw Bill
drawBall(); //draw ball
}
drawScore();
gameOver();
}

void updateBall() {
//ball speed
speedY += gravity;

xPos += speedX;
yPos += speedY;
}

// ball and basket collision detection
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
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);
}

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);

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
}
noStroke();
}
void gameOver() {
if (hitFloor) {
fill(0, 255, 0);
textSize(14);
text("GAME OVER", width/2, 50); //display the score
}
}
``````
1 Like

It would be better when you find your own solution

People should not post full sketches but give advice and snippets instead

I think we are now in a situation where you don’t fully understand your program anymore because other people wrote so much in it…

As for the different screens, like the game over screen :

Make a global variable int state that says on what screen you are. Each screen has a number like 0 for welcome screen, 1 for game screen etc.

In draw evaluate state with switch () or if(state==0) {…} else if…

and in each section draw the according screen

Nothing is allowed in draw() outside this if…else if… section

In keypressed and mousePressed also make such a section (nothing outside of it in each function allowed) and make the checking of key and mouse appropriate to state