Problem with my game

I am having trouble with my int missed I can’t get it to work like I want it to count as a missed as soon as it touches the line before the scoreboard and every time missed plus one the score would decrease by one. If anyone can help that would be great
This is what I got so far.

PImage p;
int missed = 0;
int hits = 0;
int score = 0;
int y = 25;
int x = (int)random(25,475);
void setup(){
  p = loadImage("projectBackground.png");
  size(500,700);
  background(255);
}

void draw(){
  background(p);
  fill(#DFF218);
  ellipse(x,y,50,50);
  y+=5;
  if(y >= height){
    y = 25;
    x = (int)random(25,475);}
    fill(0);
    textSize(25);
    textAlign(CENTER);
    text("Score= " + score, 100,650);
    text("Missed= " + missed, 250,650);
    text("Hits= " + hits, 400,650);
    if(y > 575){
     y = 25;
     x = (int)random(25,475);
    }
   
  }
  
void mousePressed() {
  if( dist(mouseX, mouseY, x, y) < 25 ){
    hits++;
    score++;
    y = 25;
    x = (int)random(25,475);
  }
  if(y > 575){
    y = 25;
  }
  if(hits == 0){
    missed++;
  }
}
  
void keyPressed(){
  if(key == 'r'){
    loop();
  }
  if(key == 's'){
    noLoop();
  }
}
  
1 Like

Your code has some fundamental structural problems. That is, you have a lot of code in places where it just shouldn’t be.

Let’s review how an if statement works. The basic if statement looks like this:

if( [condition] ){
    [code]
}

That is, if the [condition] evaluates to true, then the [code] in the body of the if statement executes.

Now consider what your code says:

 if(y >= height){
    y = 25;
    x = (int)random(25,475);}
    fill(0);
    textSize(25);
    textAlign(CENTER);
    text("Score= " + score, 100,650);
    text("Missed= " + missed, 250,650);
    text("Hits= " + hits, 400,650);
    if(y > 575){
     y = 25;
     x = (int)random(25,475);
    }
  }

If the y value is greater than height, ALL that code is executed. If not, NONE of it is.

This is a problem, because drawing the text is something you ALWAYS want to have happen. So let’s move that to a better place:

 if(y >= height){
    y = 25;
    x = (int)random(25,475);}
    if(y > 575){
     y = 25;
     x = (int)random(25,475);
    }
  }
  fill(0);
  textSize(25);
  textAlign(CENTER);
  text("Score= " + score, 100,650);
  text("Missed= " + missed, 250,650);
  text("Hits= " + hits, 400,650);

But now look again at the if statement. If y is greater than 575, then y is set to 25. So is the inner condition ever going to be true? NO! By the time it gets to that second if statement, y is 25… and so it will never be greater than 575 again!

I highly suggest you add comments to your code. Add a comment for every line that describes, in plain English, what that line of code does. Here’s a sample of comments I would expect to see for your program:

// Detect is a miss has occurred.
// Detect if a hit has occurred.
// Increase the hit counter.
// Increase the miss counter.
// Draw the ball.
// Update the ball's position.
// Draw the background.
// Draw the text that shows the counters.
// Load the background.
// Setup other sketch settings.
// These variables track the ball's position.
// This is the background image.
// These are the counters.
// Pause the game.
// Unpause the game.

IMPORTANT NOTE: This list of comments is NOT in the right order (hopefully this is obvious)!

1 Like

thanks, I have made the changes but I’m still having trouble with my scoreboard because the hits and score work but the missed doesn’t work like I’m trying to get it to count as a miss as soon as it hits the line before the scoreboard I don’t really know how to do that part. I have implemented this into the void mousePressed function but it doesn’t work.

 if(y > 575){
    missed++;
  }
  if(hits == 0){
    missed++;
  }