0

Everything in my Breakout code works except for the Lives count. Instead of decrementing from 3 to 0, it continues to decrement into negative values. Is it the "lives--;" line that is wrong, or is it just in the wrong place.

      // Ball goes to bottom, paddle misses, lose life
      if (getY(ball) + getHeight(ball) >= getHeight(window))
      {
          lives--;
          printf("%i\n", lives);

          // Center ball in window
          setLocation(ball, (getWidth(window) - getWidth(ball)) / 2, (getHeight(window) - getHeight(ball))/2);

         // Center paddle in window
         setLocation(paddle, (getWidth(window) - getWidth(paddle)) / 2, (getHeight(window) - getHeight(paddle)) - 100);              

         waitForClick();
      }
1

It's neither. Something else is missing. You are decrementing lives, but unless it is somewhere else in your code that wasn't posted, you don't have any test for when lives hits 0. You need something like:

if(lives < 1)
{
    // post a game over message and end program.
}

If this answers your question, please accept the answer to remove this question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

3
  • why would the lives-- not work if the staff wrote "while (lives > 0 && bricks > 0)" as the main loop? I'm having the same issue but i figured that once lives were less than zero the loop would terminate.
    – Felix
    Jun 29 '15 at 0:38
  • Good point, something I had forgotten about. Without seeing the remaining code, it's going to be very difficult to say with certainty what's happening. However, it's entirely possible that there's an inner loop that never exits, so the test never gets executed. In short, the code must, at some point, get back to the test for lives >0, but apparently is failing to do so.
    – Cliff B
    Jun 29 '15 at 0:57
  • Thanks for the reply cliff, I noticed that what you said was the case in my code after I turned it in. I had done a deliberate "while true" infinite loop within the staffs code of "while (lives > 0 && bricks > 0)" so it would never exit my loop. I ended up writing my own "lives < 0" and displaying a "Game Over" message to terminate so i guess it turned out OK , just with some extra unnecessary code.
    – Felix
    Jun 30 '15 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .