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

Hello, I’ve wondering of how I want my ball to score into the brown box and how to make a ball disappear upon collision with the brown box to score a goal. And how to place a game over feature/screen when the ball it the ground.

int xPos; //position
int yPos;

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

int friction = 1; //friction
int ballRadius = 25; //ball radius

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

boolean collide;
int score;

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

void draw() {
  
  
  updateBall(); //update the ball movement
  checkBall(); //checking ball movement
  drawBill(); //draw Bill
  drawBall(); //draw ball
  drawScore();
  drawBasket();
  
}


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

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

void checkBall() {

  if (ballHitsHat()) { //ball bouncing losing gravity
    yPos = hatY - ballRadius;
    speedY *= -1; 
  }
  
  if (xPos > width - ballRadius){//ball speed and collision
    if (speedX > 0) speedX *= -1;
  } 
  
  else if (xPos < ballRadius) {
    if (speedX < 0) speedX *= -1;
  }

  if (yPos > height - ballRadius - 34) {//ball speed and collision
    yPos = height - ballRadius - 34;
    speedY *= -friction;
     {
      if(collide==false){
      ++score; 
      }         
collide = true; 
}
{
collide = false;
}
  } 
 
}

void drawBall() {//ball
  
  fill(180);
  stroke(0);
  ellipse(xPos, yPos, ballRadius*2, ballRadius*2);
}

void drawScore(){
  fill(0);
  textSize(12);
  text("Score: "+score, 10, 30); //display the score
  
}
void drawBasket(){
fill(165, 42, 42);//basket
noStroke();
rect(490,350,500,500);
}
1 Like

For different screens search the word state

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

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

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

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

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

All good

Make a new function checkBallAgainstBasket()

and say (pseudo code)

if( ballX > basketX && 
ballX < basketX + basket.width && 
ballY > basketY && ballY < basketY + basketHeight) {
  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 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;
}
}

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

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

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

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 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
  }
}
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.

Apply to your situation

1 Like