Start, Exit and other buttons: main menu -> game

Alright - i need to make a main menu and want to have a start button, exit button and a highscore button (which pops up another menu). The thing is, i know how to make a button. But i can’t figure out how to make this button function with the switch statement.

My problems and questions right now:

  • I’m using methods to draw the start, exit and highscore button. But what is the best practice for it? Cause it’s confusing me a lot right now.
    What i want to know is: how can i get the XY, width and height values from startButton() to mousePressed() to use the if statement to make a button. Am i overthinking this? I might have a solution to just copy paste the X,Y variables in the mousePressed area and use that to make invisible buttons and just draw the rectangles via a method. Is this how it’s supposed to work? Wish there was a best practice book somewhere :slight_smile: (or is there)?

  • We’re not allowed to use Classes.

  • is the void runGame(); and runMenu(); a good practice to use for the switch statement? Or is this utterly bad?

int state = 0;
final int mainMenu = 1;
final int game = 0;

void setup() {
  size(1024, 768);
}

void draw() {
  background(51);
  switch(state) {
  case mainMenu:
    runMenu();
    break;
  case game:
    runGame();
    break;
  }
}

void runMenu() {
  menuBackground();
}

void menuBackground() {
  PFont mono;
  mono = createFont("calibri", 32);
  textFont(mono);
  int x1, y1, x2, y2;
  int b = 50;
  int bR = 5;
  x1 = width/2;
  y1 = height/2;
  x2 = width;
  y2 = height;
  rectMode(CENTER);
  textSize(32);
  fill(56, 41, 51);
  rect(x1, y1, x2-b, y2-b, bR);
  fill(255, 255, 0);
  textSize(96);
  text("Memory Game", x1/3, y1/2);
  textSize(16);
  text("By Sunny Jansen", x1/4, y1*2-bR);
}

void startButton(int x, int y, int w, int h) {
  int bR = 5;
  String s = "Start";
  float sW = textWidth(s);
  rectMode(CENTER); 
  textSize(32);
  fill(200);
  rect(x, y, w, h, bR);
  fill(10);
  text(s, x-sW, y);
}

void exitButton(int x, int y, int w, int h) {
  int bR = 5;
  String s = "Exit";
  float sW = textWidth(s);
  rectMode(CENTER);
  textSize(32);
  fill(200);
  rect(x, y, w, h, bR);
  fill(10);
  text(s, x-sW/2, y);
}

void highscoreButton(int x, int y, int w, int h) {  
  int bR = 5;
  String s = "Highscore";
  float sW = textWidth(s);
  rectMode(CENTER);
  textSize(32);
  fill(200);
  rect(x, y, w, h, bR);
  fill(10);

  text(s, x-sW/4, y);
  println(x, y);
}

void runGame() {
  showField();
  drawCards();
}

void drawCards() {
  int x, y, w, h, r;
  int spc;
  x = width/10;
  spc = 10;
  y = x + spc;
  w = 100;
  h = 150;
  r = 5;
  fill(200);
  for ( int i = y+spc; i < height-height/10; i = i + y+h/4+spc) {
    for ( int j = x+spc; j < width-width/10; j = j + x+spc) {
      rect(j, i, w, h, r);
      rect(j, i, w-10, h-10, r);
    }
  }
}

void showField() {
  rectMode(CENTER);
  int DARKGREEN = #03551F;
  int x1, y1, x2, y2;
  x1 = width/2;
  y1 = height/2;
  x2 = width-width/10;
  y2 = height-height/10;
  fill(DARKGREEN);
  rect(x1, y1, x2, y2);
}

void mouseReleased() {
  //int sX, sY, sW, sH;
  //sX = width/2-250;
  //sY = height/2;
  //sW = 100;
  //sH = 150;

  //if (mouseX > sX && mouseX < sX + sW && mouseY > sY && mouseY < sY+sH) {
  //}
  state = state==mainMenu?game:mainMenu;
}

I can‘t really help you with what is considered good practice for your Professor, But i know that it is best to make code as Reusable as possible. I Would suggest you to, since you can‘t make a class, make a Button function. Pretty much what you have right now with Start and ExitButton() But more like

void drawButton(String startOrExit, the other variables) {

}

For the switch Statement… as far as i know, a Switch Statement is Mainly used in case of many different States. For 2 cases it‘s best to just Stick with ifElse statements.

And for how to get if the Button is pressed, just add into your Button method something that gets if the mouse is pressed and is within the bounds of the rect of your Button. BTW, you can use mousePressed as a Variable to get if the mouse is pressed.

1 Like