# 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:** 20\
**Page:** 1

<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 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/1 "2019-05-20T04:46:37Z")

</div>

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.

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

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

```

---

<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 20, 2019, 7:45am 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/2 "2019-05-20T07:45:02Z")

</div>

> [@grodothefrog](#):
>
> how to place a game over feature/screen when the ball it the ground.

For different screens search the word state

---

<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 20, 2019, 8:03am 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/3 "2019-05-20T08:03:20Z")

</div>

Anyway type of sample to fix my scoring code with the collision with the brown box?

---

<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 20, 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/4 "2019-05-20T08:22:10Z")

</div>

When the ball position is greater than upper left corner of the box and also…

---

<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 20, 2019, 8:35am 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/5 "2019-05-20T08:35:59Z")

</div>

I’m sorry but I’m confused about your respond?

---

<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 20, 2019, 8:57am 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/6 "2019-05-20T08:57:40Z")

</div>

I didn’t have time, sorry

So sentence continues

and it’s smaller than right lower corner, you got a collision

So then say goal++;

You can just change the state to 1 eg. then and in draw when state is 1 display a goal message. Then reset the ball

---

<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 20, 2019, 9:04am 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/7 "2019-05-20T09:04:41Z")

</div>

No need to apologise it all good, it just I’m just having a problem understanding your logic. It just me.

---

<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 20, 2019, 9:23am 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/8 "2019-05-20T09:23:19Z")

</div>

All good

Make a new function checkBallAgainstBasket()

and say (pseudo code)

```auto
if( ballX > basketX && 
ballX < basketX + basket.width && 
ballY > basketY && ballY < basketY + basketHeight) {
  goal++;
  state=1;
}

```

---

<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 20, 2019, 9:31am 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/9 "2019-05-20T09:31:02Z")

</div>

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

---

<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 20, 2019, 9:35am 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/10 "2019-05-20T09:35:43Z")

</div>

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

---

<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 20, 2019, 9:39am 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/11 "2019-05-20T09:39:56Z")

</div>

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.

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

boolean collide;
int score;
int ballX;
int ballY;
int basket;
int basketX;
int basketY;

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

void draw() {
  
  
  updateBall(); 
  checkBall();
  drawBill(); 
  drawBall(); 
  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){
      
      }         
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);

if(ballX > basketX &&
ballX < basketX + basket.width &&
ballY > basketY && ballY < basketY + basketHeight) {

score++;
score=1;
}
}

```

---

<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 20, 2019, 11:23am 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/12 "2019-05-20T11:23:53Z")

</div>

What doesn’t work?

Chrisir

---

<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 20, 2019, 1:46pm 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/13 "2019-05-20T13:46:59Z")

</div>

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](https://discourse.processing.org/t/the-project-has-ended/9808)

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

---

<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 20, 2019, 2:23pm 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/14 "2019-05-20T14:23:32Z")

</div>

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:

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

boolean collide;
int score;

int basketH;
int basketW;
int basketX;
int basketY;
boolean hide;
boolean hitFloor;

void setup() {
  size(640, 480);
  ballRadius = 25;
  basketX = width/2;
  basketY = 100;
  basketW = 100;
  basketH = 150;
  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();
  drawBasket();
  gameOver();
}

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

boolean ballHitsHat() {
  // ball and hat collision detection
  if (xPos + ballRadius >= basketX && xPos <= basketX + basketW &&
    yPos + ballRadius >= basketY && yPos <= basketY + basketH) {
    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);
  ellipse(xPos, yPos, ballRadius, ballRadius);
}

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 21, 2019, 6: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/15 "2019-05-21T06:18:58Z")

</div>

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.

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

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 = width/2;
  basketY = 100;
  basketW = 100;
  basketH = 150;
  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();
  drawBasket();
  gameOver();
}

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

boolean ballHitsHat() {
  // ball and hat collision detection
  if (xPos + ballRadius >= basketX && xPos <= basketX + basketW &&
    yPos + ballRadius >= basketY && yPos <= basketY + basketH) {
    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);
  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 drawBall() {//ball
  
  fill(180);
  stroke(0);
  ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}

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:** ![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 21, 2019, 10:24am 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/16 "2019-05-21T10:24:54Z")

</div>

@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:

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

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;
  //
  hatW = 90;
  hatH = 10;
  hatX = mouseX - 15;
  hatY = 390;
  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) {
    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);
  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(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);
  ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}

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 21, 2019, 11:52pm 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/17 "2019-05-21T23:52:51Z")

</div>

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.

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

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;
  //
  hatW = 90;
  hatH = 10;
  hatX = mouseX - 15;
  hatY = 390;
  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 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);
  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(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);
  ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}

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(490,350,500,500);
}
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, 1:08am 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/18 "2019-05-22T01:08:07Z")

</div>

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.

```auto
int xPos; 
int yPos;

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

int friction = 1; 
int ballRadius = 25; 

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

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;
  //
  hatW = 90;
  hatH = 10;
  hatX = mouseX - 15;
  hatY = 390;
  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 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);
  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(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);
  ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}

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(490,350,500,500);
}
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, 5:28am 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/19 "2019-05-22T05:28:17Z")

</div>

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

---

<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:02am 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/20 "2019-05-22T08:02:16Z")

</div>

> [@grodothefrog](#):
>
> the ball run under my character and I’ve wondered how to repaired it

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.

Apply to your situation

[Next 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=2)
