Code

void setup(){
  size(300,300);
  smooth();
  noStroke();
}

float px;
float py;
float vx;
float vy;
float startx;
float starty;
int lives = 5;

void draw(){
  background(0);
  px = px+vx;
  py = py+vy;
  startx = px+width/2;
  starty = py+height/2;
  bounce();
  ellipse(startx,starty,15,15); //I did this so you have to click to make the puck move
  rect(5,mouseY-30,12,60);
  
  if((startx < 0) && (lives > 0)){
    reset();
    lives = lives - 1;
  } 
  
  for(int i = 0; i<lives; i++){
    ellipse(25+i*12,10,8,8);
  }
  
  if (lives <= 0){
    background(255,0,0);
  }
  
}

void mousePressed(){
  if(lives > 0){
  reset();
  } else{
    noLoop();
  }
}

void reset(){
  px = 0;
  py = 0;
  vx = random(-4,4);
  vy = random(-4,4);
}

void bounce(){
  if (((startx <= 23) && (starty <= mouseY+30) && (starty >+mouseY-30)) 
  || (startx >= width - 7)){
    vx = -vx;
  }
  if ((starty <= 7) || (starty >= height - 7)){
   vy = -vy;
  }
}

0405 - Pong IV: Towards a simple game

Statement:

  • Add a "life counter" to your applet. Initialize this counter at the start of the program so that the player starts out with 5 lives.
  • Indicate the player's current number of lives with a small display in the upper corner. Your indicator could be built either with a series of small pips, or with rendered text. (I haven't demonstrated how to render text in class yet, but there are plenty of good examples.)
  • Each time the puck leaves the left edge of the screen, diminish the user's life count by 1.
  • If the life-count hits zero, turn the screen red and disable further play.

    hide statement