How to create a start menu that has three buttons: Easy, Medium and Hard?

How to create a start menu that has three buttons: Easy, Medium and Hard. When a button is pressed, the initial speed of the ball is set based on the difficulty and the game begins.

float x = 700;
float y = 350;
float spd = 0.5;
float paddle1Y;
float paddle2Y;
int p1Score = 0;
int p2Score = 0;
int p1Life = 10;
int p2Life = 10;
int dirX = -1;
int dirY = -1;
int state = 0;

void setup() {
  size(800, 600);
  noStroke();
  smooth();
}


void draw() {
  background(0, 0, 0);  //Erase The Screen

  //*-------------------Menu---------------------------------
  if (state == 0) {
    fill(255);
    textSize(45);
    text("Pong Assignment by Nathaniel K.", 36, 200);
    p1Life = 10;
    p2Life = 10;
    if (mousePressed) {
      state = 1;
    }
  }
  //*----------------------Reset------------------------------
  else if (state == 1) {
    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(85, paddle1Y, 30, 30);
    rect(40, paddle1Y - 50, 20, 100); 
    rect(740, paddle2Y - 50, 20, 100);

    if (mousePressed) {

      if (keyPressed && key == 'w') 

        paddle1Y = paddle1Y - 8;

      else if (keyPressed && key == 's') 

        paddle1Y = paddle1Y + 8;

      if (keyPressed && key == 'i') 

        paddle2Y = paddle2Y - 8;

      else if (keyPressed && key == 'k') 

        paddle2Y = paddle2Y + 8;

      x = 75;
      y = paddle1Y;
      dirX = 1;  // add or subtract the speed based on the direction to move the ball
      dirY = 1;
      state = 2;
    }
  }

  //*-------------------Game Play-----------------------------
  else if (state == 2) {
    if (keyPressed && key == 'w') 

      paddle1Y = paddle1Y - 8;

    else if (keyPressed && key == 's') 

      paddle1Y = paddle1Y + 8;

    if (keyPressed && key == 'i') 

      paddle2Y = paddle2Y - 8;

    else if (keyPressed && key == 'k') 

      paddle2Y = paddle2Y + 8;

    fill(255);
    //  Check The Walls
    if (x > 800 || x < 0) {  // if the ball hits either wall
      dirX *= -1;  //flip the sign (change direction)
    }

    if (y > 600 || y < 0) {  // if the ball hits either wall
      dirY *= -1;  //flip the sign (change direction)
    }

    //  Check The P1 Paddle
    if (x <= 70 && y >= paddle1Y - 50 &&  y <= paddle1Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += 1;
      p1Score++; //Adds the score by 1
    }

    //  Check The P2 Paddle
    if (x > width - 75 && y >= paddle2Y - 50 &&  y <= paddle2Y + 50 && x >= 5) {
      dirX *= -1;  //flip the sign (change direction)
      dirX += -1;
      p2Score++; //Adds the score by 1
    }

    if (p1Score == 5) {
      ellipse(x, y, 30, 30);
    }

    if (p2Score == 5) {
      ellipse(x, y, 30, 30);
    }

    x += 2.5*dirX;  // add or subtract the speed based on the direction to move the ball
    y += 2.5*dirY;

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    ellipse(x, y, 30, 30);  // draw the ball
    rect(40, paddle1Y - 50, 20, 100);  // draw the paddle

    stroke(255);
    strokeWeight(5);
    fill(255, 0, 0);
    rect(740, paddle2Y - 50, 20, 100);  // draw the paddle

    if (x > 740) {
      p2Life--;
      state = 1;
    }
    if (x < 40) {
      p1Life--;
      state = 1;
    }

    if (p1Life == 0) {
      state = 3;
    }
    if (p2Life == 0) {
      state = 3;
    }


    fill(255);
    //displays score and lives remaining.
    textSize(30);
    text("P1 Score:" + p1Score + " ", 50, 50);
    text("P1 Lives:" + p1Life + " ", 50, 85);
    text("P2 Score:" + p2Score + " ", 500, 50);
    text("P2 Lives:" + p2Life + " ", 500, 85);
  }

  //*-----------------------Game Over-----------------------------
  else if (state == 3) {
    background(255, 0, 0);
    fill(255);
    textSize(80);
    text("GAME OVER", 158, 200);
    fill(0);
    textSize(40);
    text("CLICK TO RESTART", 200, 400);
    if (mousePressed) { 
      state = 0;
    }
  }
}
Tagged:

Answers

  • You can create your own buttons or use an available library that will do that for you. Two common ones are G4P and controlP5. You cna install them using the library manager and then go directly to the examples and try them on until you find the one that is suitable for you. For more info, check their web pages:

    http://www.lagers.org.uk/g4p/
    http://www.sojamo.de/libraries/controlP5/

    Kf

  • Answer ✓

    It might be easier for a beginner to use your own buttons and not a library

    Use textAlign(CENTER);

    Write easy medium and hard with text in state 0

    Make a function mousePressed see reference

    and use if(dist(textposx,textposy,mouseX,mouseY)<40)){

    speed=1;

    state =1;

    }

    else if(dist.....

Sign In or Register to comment.