Adding a restart button, and showing score

Hi there! Just a quick question. So in my game of pong, when the ball falls through, i am trying to change it so that your score is shown after the "Text" of (Your Score:). Also, underneath that, I am attempting to implement a button that asks the user if they'd like to play again. Could someone please help me out?

                 int state=0;
    import ddf.minim.*; 
    Minim minim; 
    AudioPlayer song; 
    AudioPlayer song2;
    AudioPlayer song3;

    float x, y, diameter, vx, vy, a;
    int score = 0;
    float playSong = 0;

    PFont font;
    PFont font2;
    PImage img;
    color c = (#1BCE3A);
    color d = (#1BCE3A);
    boolean b1, b2, b3;
    color c1= (#1BCE3A);
    color c2 = (#1BCE3A); 
    color c3 = (#1BCE3A);



    void setup() {
      font = createFont("Harrigton-48", 48);//Title font
      font2 = createFont("ArialNarrow-48", 48);//Sound button font
      size(1300, 800);
      state = 1;
      textAlign(CENTER);
      minim = new Minim(this);
      song = minim.loadFile("stilldre.mp3");
      song2 = minim.loadFile("FIRE.mp3");
      song3 = minim.loadFile("Spongebob.mp3");
      x = width/2;
      y = 0;
      diameter = 20;
      vx = 2;
      vy = 0;
      a = 0.1;
      frameRate(45);


      b1 = false;
      b2 = false;
      b3 = false;
    }

    void draw() {




      if (state == 1) { startScreen(); }
      if (state == 2) { gameScreen(); }
      if (state == 3) { Settings(); }
      if (state == 4) { Songs(); }
      //else settingScreen();
    }

    void Settings() {
      background(0);
      fill(#1BCE3A);
      textSize(90);
      text("Sound Effects", width/2, height/4);
     fill(#1BCE3A);
     ellipse(width/2.85,height/2,width/4,height/3);//left side 
     ellipse(width/1.55, height/2, width/4, height/3);
     fill(0);
     text("On",width/2.85,height/1.9);
     text("Off", width/1.55, height/1.9);
      println(mouseX, mouseY);
    }

    void Songs() {
      background(0);
      fill(#1BCE3A);
      textSize(100);
      text("Song Selection", width/2, height/7);
    }

    void gameScreen() {
      background(0);
      fill(#1BCE3A);
      textSize(50);
      text(score, 100, 50);
      vy = vy + a;
      y = y + vy;
      x = x + vx;
      //bounce off bottom
      if (y > height - diameter/2) {
       fill(255,0,0);
        textSize(100);
        strokeWeight(100);
        text("Game Over!", width/2,height/2);
        //text("Your score:",width/2,height/1.75);

     }
      //bounce off ceiling
      if (y < diameter/2) {
        vy=vy*-1;
        y=diameter/2;
      }
      //bounce off right side
      if (x > width - diameter/2) {
        vx=vx*-1;
        x=width - diameter/2;
      }
      //bounce off left side
      if (x < diameter/2) {
        vx=vx*-1;
        x=diameter/2;
      }
      //ball bouncing
      if (x >= mouseX-50 && y<height && x <= mouseX+50 && (y+diameter/2) >= height-20) {
        vy=vy*-1;
        y=height-30;
        score = score+1;
        playSong = random(0, 2);
      }
      if (playSong == 0) {
        song.play();
      }
      //else if

      //  (playSong == 1) {
      //    song2.play();
      //  }
      //  else if 
      //  (playSong == 2) {
      //    song3.play();
      //  }
      //  // rectMode(CENTER);


        fill(#05FC30);
        stroke(#05FC30);
        ellipse(x, y, diameter, diameter);

        fill(#05FC30);
        stroke(#05FC30);
        rect(mouseX, height-10, 100, 10);
      }

    void startScreen() {
    song.play();
      background(255);
      checkButton123();
      drawText();
      drawElements();
      isOver();
      rectMode(CENTER);
      float w = width;
      float h = height;
      color c = 255;

      fill(#1BCE3A);
      textFont(font, width/15);
      textSize(50);
      text("The Pong Game", width/2, 1.5*height/9);
      fill(0);

      fill(c);
      strokeWeight(5);//thickness of rectangle
      rectMode(CENTER);
    }

    void checkButton123() {
      background(0);
      //checkButton();
      if (b1 == true)
      {
        fill(255);
      } else
      {
        fill(c1);
      }  

      rect(width/2, 1.5*height/5, width/2, height/6, 30);//draws Sound button 1

      if (b2 == true)
      {
        fill(255);
      } else
      {
        fill(c2);
      }    
      rect(width/2, 2*height/3.5, width/2, height/6, 30);//draws Sound button 2

      if (b3 == true)
      {
        fill(255);
      } else
      {
        fill(c3);
      }    
      rect(width/2, 2*height/2.35, width/2, height/6, 30);//draws Sound button 3
    }
    void drawText() {
      fill(0);
      // textFont(font2, width/17);
      text("Play Game", width/2, height/3);//text of first button
      text("Settings", width/2, 2*height/3.35);//draws text of second button
      text("Song Selection", width/2, 1.5*height/1.70);//draws text of third button
      isOver();
    }

    void drawElements() {

      mouseClicked();
    }

    void isOver()
    {
      int mx, my;
      mx = mouseX;
      my = mouseY;

      // test if mouse is over button Uno
      if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 1.5*height/5 - height/12)&&(my < 1.5*height/5 + height/12))
      { 
        b1 = true;
        b2 = false;
        b3 = false;
      }
      // test if mouse is over button Dos  
      else if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 2*height/3.5 - height/12)&&(my < 2*height/3.5 + height/12))
      { 
        b1 = false;
        b2 = true;
        b3 = false;
      }    
      // test if mouse is over button Tres  
      else if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 2*height/2.35 - height/12)&&(my < 2*height/2.35 + height/12))
      { 
        b1 = false;
        b2 = false;
        b3 = true;
      }
      // mouse not over any buttons
      else
      { 
        b1 = false;
        b2 = false;
        b3 = false;
      }
    }
    void mouseClicked()
    {
      int mx, my;
      mx = mouseX;
      my = mouseY;

      // test if mouse is clicked button Uno
      if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 1.5*height/5 - height/12)&&(my < 1.5*height/5 + height/12)&&(mousePressed))
      { 


        state=2;
      }
      // test if mouse is clicked on button Dos  
      else if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 2*height/3.5 - height/12)&&(my < 2*height/3.5 + height/12)&&(mousePressed))
      { 


        state=3;
      }    
      // test if mouse is clicked on button Tres  
      else if ((mx > width/2 - width/4)&&(mx < width/2 + width/4)&&(my > 2*height/2.35 - height/12)&&(my < 2*height/2.35 + height/12)&&(mousePressed))
      { 
     state=4;
      }
    }
Tagged:

Answers

Sign In or Register to comment.