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

**URL:** <https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402>\
**Category:** Coding Questions\
**Created:** [May 20, 2019, 4:46am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402 "2019-05-20T04:46:37Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![anon26872549](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@anon26872549](https://discourse.processing.org/u/anon26872549)\
**Post date:** [May 22, 2019, 8:22am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/21 "2019-05-22T08:22:37Z")

</div>

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.

```auto
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
  }
}

```

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 22, 2019, 8:30am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/22 "2019-05-22T08:30:04Z")

</div>

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?

```auto
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
  }
}

```

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 22, 2019, 8:43am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/23 "2019-05-22T08:43:45Z")

</div>

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?

```auto
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
  }
}

```

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 22, 2019, 8:46am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/24 "2019-05-22T08:46:09Z")

</div>

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?

```auto
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
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2019, 8:50am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/25 "2019-05-22T08:50:02Z")

</div>

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…

Please read the sketch carefully and find solutions instead for asking for them

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2019, 9:12am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/26 "2019-05-22T09:12:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2019, 9:15am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/27 "2019-05-22T09:15:56Z")

</div>

Get familiar with your own program again please…

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 22, 2019, 9:18am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/28 "2019-05-22T09:18:59Z")

</div>

I just came up with an idea of instead of switching screen why not make a drawing or display showing the word game over instead of making another screen. As I’ve used  
`void mousePressed(){ setup(); }`  
this code make me restart the game and the game over text disappeared.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 22, 2019, 9:29am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/29 "2019-05-22T09:29:22Z")

</div>

Good idea

Please proceed

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 22, 2019, 9:46am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/30 "2019-05-22T09:46:48Z")

</div>

Sorry to disappoint you by not make a drawing or display showing the word game over instead of making another screen. But I’ve manage to fix the crashing glitch by changing the

```auto
ballHitsHat();
  if( ballHitsBasket() ){
    hide = false;//I've change this from true to false and the ball re spawn on it correct direction but the ball respawn a bit to high
    xPos = - 100;
    yPos = - 100;

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [May 22, 2019, 12:25pm UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/31 "2019-05-22T12:25:00Z")

</div>

I put in the hide as you asked when the ball hits the basket it disappears and after the ball disappears it position is set to xPos = -100 and yPos = - 100 which means it will stay up there. If you have set the hide condition to false that means the ball won’t go invisible anymore but will only now have it’s xPos = -100 and yPos = -100 set to that and it iwlll start falling from there. You can remove those to get rid of the unfavorable behavior.

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [May 22, 2019, 2:02pm UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/32 "2019-05-22T14:02:41Z")

</div>

fixing scoring, and ‘click to respawn’

```auto
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;
float py;

void setup() {
  size(640, 480); //game size
  py = yPos - ballRadius;
  ballRadius = 25;
  basketX = 500;
  basketY = 100;
  basketW = 100;
  basketH = 150;
  hide = false;
  hitFloor = false;
  score = 0;
}

void draw() {
  background(0);
  ballHitsHat();
  ballHitsBasket();

  if (!hide) {
    updateBall(); //update the ball movement
    checkBall(); //checking ball movement
  }
  drawBill(); //draw Bill
  drawBall(); //draw ball
  drawScore();
  drawBasket();
  gameOver();
}

void mousePressed() {
  if (hide) {
    respawn();
  }
}

void respawn() {
  hide = false;
  score = 0;
  xPos = 0;
  yPos = 0;
  speedX = 3;
  speedY = 0;
  hitFloor = false;
}

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

  xPos += speedX;
  yPos += speedY;
}

boolean scoring = false;
void ballHitsBasket() {
  // ball and basket collision detection
  if (!scoring) {
    if (xPos > basketX && yPos > basketY && xPos < basketX + basketW && yPos < basketY + basketH) {
      scoring = true;
    }
  } else {
    if (yPos - ballRadius > basketY + basketH) {
      scoring = false;
      score++;
    }
  }
}

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;
    hitFloor = true;
    hide = true;
    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);
    textAlign(CENTER);
    text("GAME OVER", width/2, 50); //display the score
    text("Click to respawn", width/2, 60); //display the score
    textAlign(LEFT);
  }
}

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [May 22, 2019, 2:13pm UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/33 "2019-05-22T14:13:16Z")

</div>

You will need an array of ball object to spawn 5 balls

---

<div class="post-metadata">

**Author:** ![grodothefrog](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@grodothefrog](https://discourse.processing.org/u/grodothefrog)\
**Post date:** [May 23, 2019, 7:07am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/34 "2019-05-23T07:07:36Z")

</div>

By the way is there another way for the ball to respawn when the ball collide with the box and I’ve don’t want the ball to say game over when it hit the box, I just simply want the game to keep going until the game is over when the ball hit the ground? And when the ball hit the ground, I’ve want to display the whole screen red and it say game over, then after displaying it. Press the mouse then the game restart normally.

Also I’ve has to leave the box at this location as I’ve want to leave it like that instead of leaving the box in the air. When I’ve change the code the ball won’t score when it hit the box. But thanks for the help.

```auto
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;
float py;

void setup() {
  size(640, 480); //game size
  py = yPos - ballRadius;
  ballRadius = 25;
  basketX = 500;
  basketY = 350;
  basketW = 200;
  basketH = 400;
  hide = false;
  hitFloor = false;
  score = 0;
}

void draw() {
  background(0);
  ballHitsHat();
  ballHitsBasket();

  if (!hide) {
    updateBall(); //update the ball movement
    checkBall(); //checking ball movement
  }
  drawBill(); //draw Bill
  drawBall(); //draw ball
  drawScore();
  drawBasket();
  gameOver();
}

void mousePressed() {
  if (hide) {
    respawn();
  }
}

void respawn() {
  hide = false;
  score = 0;
  xPos = 0;
  yPos = 0;
  speedX = 3;
  speedY = 0;
  hitFloor = false;
}

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

  xPos += speedX;
  yPos += speedY;
}

boolean scoring = false;
void ballHitsBasket() {
  // ball and basket collision detection
  if (!scoring) {
    if (xPos > basketX && yPos > basketY && xPos < basketX + basketW && yPos < basketY + basketH) {
      scoring = true;
    }
  } else {
    if (yPos - ballRadius > basketY + basketH) {
      scoring = false;
      score++;
    }
  }
}

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;
    hitFloor = true;
    hide = true;
    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);
    textAlign(CENTER);
    text("GAME OVER", width/2, 50); //display the score
    text("Click to respawn", width/2, 60); //display the score
    textAlign(LEFT);
  }
}

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [May 23, 2019, 10:09am UTC](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402/35 "2019-05-23T10:09:46Z")

</div>

Well you should figure it out by looking at the code ! Think about mess around and really **understand**  
how your code is **behaving**. We can’ t give you all the answers !

[Previous page](https://discourse.processing.org/t/how-to-fix-my-scoring-code-and-how-to-input-a-game-over-feature-screen-when-the-ball-hit-the-ground/11402.md?page=1)
