Menu for a game

Hello,
after multiple help and time I have this code:

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;

void setup() {
   fullScreen();
}

void draw() {
  background(0);
  fill(255);
  if (lastTime + wait < millis()) {
    randomize_square_position();
  }
  rect(square_x, square_y, square_size, square_size);
  text("Score = ", 20, 1050);
  textSize(32);
  text(score, 150, 1050);
}

void mousePressed() {
  if ( over_square() ) {
    score += bonus;
    bonus++;
  } else {
    score = 0;
    bonus = 1;
  }
  randomize_square_position();
}

void randomize_square_position() {
  square_x = random(width - square_size);
  square_y = random(height - square_size);
  lastTime = millis();
}

boolean over_square() {
  return(
    mouseX > square_x &&
    mouseX < square_x + square_size && 
    mouseY > square_y && 
    mouseY < square_y + square_size
    );
}

and I would like to add before starting the game directly a menu with the Start button which, when I press it displays the game for the moment I have this (I think I have mixed everything) :

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;
final int menu      = 0;
final int jeux      = 1;

int state = menu;
PFont font;

void setup() {
   fullScreen();       //fullscreen
   smooth();
  font = createFont("ARCARTER-78.vlw", 14);
  textFont(font);
}

void draw()
{ 
switch (state) {
  case menu:
    showMenu();
    break;
  case jeux:
    handlejeux();
    break;
  default:
    println ("Unknown state (in draw) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  } // switch
}
{
  background(0);          //couleur du fond
  fill(255);           
  if (lastTime + wait < millis()) {             //si j'ai pas click au bout de X temps
    randomize_square_position();                //alors nouveau carré
  }
  rect(square_x, square_y, square_size, square_size);    //dessin du carré
  text("Score = ", 20, 1050);       //Afficher "Score =" a certaine coo
  textSize(32);           //taille du texte
  text(score, 150, 1050);     //coordonnée du score
}

void mousePressed() {
  if ( over_square() ) {     
    score += bonus;         //plus j'appuye plus je gagne du score
    bonus++;
  } else {
    score = 0;              //score de base
    bonus = 1;              //score +1 par combo
  }
  randomize_square_position();
}

void randomize_square_position() {
  square_x = random(width - square_size);          //Spawn random de carré sur les coo X
  square_y = random(height - square_size);         //Spawn random de carré sur les coo Y
  lastTime = millis();                             //depop si trop long
}

boolean over_square() {
  return(
    mouseX > square_x &&                    //si je click sur le carré, alors disparition  
    mouseX < square_x + square_size &&      
    mouseY > square_y && 
    mouseY < square_y + square_size
    );
}
void keyPressed() {
 
  switch (state) {
  case menu:
    keyPressedForMenu();
    break;
  case jeux:
    keyPressedForjeux();
    break;
  default:
    println ("Unknown state (in keypressed) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  } 
 
} 
void keyPressedForMenu() {
 
  switch(key) {
  case '1':
    state = jeux;
    break;
  case '2':
  case 'x':
  case 'X':
   
    exit();
    break;
  default:
   
    break;
  }
  
} 
void keyPressedForjeux() {

  switch(key) {
  default:
    state = menu;
    break;
  } // switch
  //
} // func

void showMenu() {
  background(255, 204, 0);
  fill(0);
  textSize(32);
  text(" OSU ", 150, 100, 3);
  textSize(14);
  text("Press 1 Jouer ", 100, 200);
  text("Press 2 Stats Parties ", 100, 220);
 
  text("Press x Pour Quitter ", 100, 260);
  
} 

void handlejeux() {
  background(255, 204, 0);
  fill(0);
  textSize(32);
  text(" Jouer ", 150, 100, 3);
  textSize(14);
  text("..... some text ", 100, 200);
  
} 

instead of drawing rectangles, maybe you can use the JButton class? You can look at the javadoc and import the javax.swing library. Use a boolean to control whether the game starts, and if that boolean is false, then display the menu. Once the play button is hit, the boolean becomes true.

This menu works

  • I also made sections in your code, separated by "-----"

  • You can use Esc now to leave the game and return to the menu. Otherwise Esc is disabled.

Warm regards,

Chrisir

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int bonus = 1;

int square_size = 50;
float square_x;
float square_y;

final int menu      = 0;
final int jeux      = 1;
int state = menu;

PFont font;

// -----------------------------------------------------------------------------------------

void setup() {
  fullScreen();       //fullscreen
  //size(800, 800);
  smooth();
  font = createFont("ARCARTER-78.vlw", 14);
  textFont(font);
}

void draw() {
  // We use a switch() to eval state - nothing in draw() is allowed outside this switch 
  switch (state) {
  case menu:
    showMenu();
    break;
  case jeux:
    handlejeux();
    break;
  default:
    println ("Unknown state (in draw) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  } // switch
}//func

// -----------------------------------------------------------------------------------------
// Inputs mouse 

void mousePressed() {
  if (state!=jeux)
    return;//leave !!!  

  if ( over_square() ) {     
    score += bonus;         //plus j'appuye plus je gagne du score
    bonus++;
  } else {
    score = 0;              //score de base
    bonus = 1;              //score +1 par combo
  }
  randomize_square_position();
}

// -----------------------------------------------------------------------------------------
// Inputs keyboard 

void keyPressed() {
  // We use a switch() to eval state
  switch (state) {
  case menu:
    keyPressedForMenu();
    break;
  case jeux:
    keyPressedForjeux();
    break;
  default:
    println ("Unknown state (in keypressed) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  }
} 

void keyPressedForMenu() {

  switch(key) {
  case '1':
    state = jeux;
    break;

  case '2':
    // ????
    break; 

  case 'x':
  case 'X':
    exit();
    break;

  default:
    // 
    key=0; // kill Esc
    break;
  }
} 

void keyPressedForjeux() {
  switch(key) {
  default:
    key=0; // kill Esc
    state = menu;
    break;
  } // switch
  //
} // func

// -----------------------------------------------------------------------------------------

void showMenu() {
  background(255, 204, 0);
  fill(0);
  textSize(32);
  text(" OSU ", 150, 100, 3);
  textSize(14);
  text("Press 1 Jouer ", 100, 200);
  text("Press 2 Stats Parties ", 100, 220);

  text("Press x Pour Quitter ", 100, 260);
} 

void handlejeux() {
  //background(255, 204, 0);
  //fill(0);
  //textSize(32);
  //text(" Jouer ", 150, 100, 3);
  //textSize(14);
  //text("..... some text ", 100, 200);


  background(0);          //couleur 
  fill(255);           
  if (lastTime + wait < millis()) {             //si j'ai pas click au bout de X temps
    randomize_square_position();                //alors nouveau carré
  }
  rect(square_x, square_y, square_size, square_size);    //dessin du carré
  text("Score = ", 20, height-25);       //Afficher "Score =" a certaine coo
  textSize(32);           //taille du texte
  text(score, 150, height-25);     //coordonnée du score
}

//--------------------------------------------------------------
// Tools

void randomize_square_position() {
  square_x = random(width - square_size);          //Spawn random de carré sur les coo X
  square_y = random(height - square_size);         //Spawn random de carré sur les coo Y
  lastTime = millis();                             //depop si trop long
}

boolean over_square() {
  return(
    mouseX > square_x &&                    //si je click sur le carré, alors disparition  
    mouseX < square_x + square_size &&      
    mouseY > square_y && 
    mouseY < square_y + square_size
    );
}
//