Code

float px;
float py;
float vx;
float vy;

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

void draw(){
  background(0);

  edge();

  puck();

  paddle();
  
  count();

}
void mousePressed(){
  reset();
}

void puck(){
  ellipse(px=px+vx, py=py+vy, 20,20);
}
void reset(){
  px=150;
  py=150;
  vx=random(-4,4);
  vy=random(-4,4);
}

void edge(){
  if ((px<0)|| (px>width)){
    vx=-vx; 
  }
}
if ((py<0)|| (py>height)){
  vy=-vy; 
}

void paddle(){
  fill(255);
  rect(10,mouseY, 5, 35); 
  if ((px < 15) && (py < mouseY + 35) && (vy > mouseY - 35)){
    vx = -vx; 
  }
  void counter(){
  stroke(0,255,0);
  if(vx<5){
    a = a -1;
    start();
  }
  for(int x = 0; x<a; x = x+1){
    line(width -((1+x) * 5), 10,width -((1+x) * 5), 20);
  }
  if(a == 0){
    noFill();
    background(255,0,0);
    start();
}

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