I need some help with a project

This post was flagged by the community and is temporarily hidden.

Hello and welcome to the forum!

Great to have you here.

the idea is that you have a variable int state that dictates the screen you are on:

  • When state is 0 you show the start screen.

  • If state is 1 you show the game (what you have in draw() now)

  • when state is 2 you show the game over screen.

In draw() you check what value state has and act accordingly. I use switch() to check this but if… else if… would be the same.

(you can even name the numbers 0,1,2 as constants)
(since a mousePressed means something different in the states (to which state you want to go next that is), you can use switch(state) in mousePressed too)

Chrisir

Here is an example

//
// states
final int stateGame  = 0;  // constants
final int statePause = 1;
int state = stateGame;    // current state

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

void setup() {
  // init (runs only once)
  size(800, 600);
} // func 

void draw() {

  // draw() runs on and on 

  switch (state) {

  case stateGame: 
    // Game
    handleStateGame();
    break; 

  case statePause:
    // Pause
    handleStatePause(); 
    break;

  default:
    // error
    println("Error number 939; unknown state : " 
      + state 
      + ".");
    exit();
    break;
  } // switch
  //
} // func 

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

// functions for states - called from draw() 

void handleStateGame() {
  // Game
  background(11);
  fill(244, 3, 3); // red 
  text ("Game....click p to pause", 210, 313);
  signHome(10, 100);
} // func 

void handleStatePause() {
  // Pause
  background(255);
  fill(244, 3, 3); // red 
  text ("Pause.... ", 210, 313);
  text ("Hit p to start ", 210, 385);
  signHome(width-70, 100);
} // func 

// ----------------------------------------
// keyboard input 

void keyPressed() {

  switch (state) {

  case stateGame: 
    // Game
    keyPressedForStateGame();
    break; 

  case statePause:
    // Pause
    keyPressedForStatePause(); 
    break;

  default:
    // error
    println("Error number 1039; unknown state : "
      + state 
      + ".");
    exit();
    break;
  } // switch
} // func 

// ----

void keyPressedForStateGame() { 
  if (key == CODED) 
  {
    if (keyCode == UP) {
      //
    } else if (keyCode == DOWN) {
      //
    }

    if (keyCode == LEFT) {
      //
    } else if (keyCode == RIGHT) {
      //
    } else {
      // do nothing
    } // else
  } //  if (key == CODED) {
  else 
  {
    // not CODED ---------------------- 
    if (key == 'p') {
      // Pause 
      state = statePause;
    } else {
      // do nothing
    } // else
  } // else not CODED
} // func

void keyPressedForStatePause() { 
  if (key == CODED) 
  {
    if (keyCode == UP) {
      //
    } else if (keyCode == DOWN) {
      // none
    }

    if (keyCode == LEFT) {
      //
    } else if (keyCode == RIGHT) {
      //
    } else {
      // do nothing
    } // else
  } //  if (key == CODED) {
  else 
  {
    // not CODED ---------------------- 
    if (key == 'p') {
      //
      state = stateGame;
    } else {
      // do nothing
    } // else
  } // else not CODED
} // func

// -------------------------------------------------------
// Mouse input

void mousePressed() {
  //
  switch (state) {

  case stateGame: 
    // Game
    mousePressedForStateGame();
    break; 

  case statePause:
    // Pause
    mousePressedForStatePause(); 
    break;

  default:
    // error
    println("Error number 1139; unknown state : " + state+".");
    exit();
    break;
  } // switch
} // func 

void mousePressedForStateGame() {
  if (dist(mouseX, mouseY, 10, 100) < 30 ) { 
    println ("Hit Game.");
  }
} // func 

void mousePressedForStatePause() {
  if (dist(mouseX, mouseY, width-70, 100) < 30 ) { 
    println ("Hit Pause.");
  }
} // func 

// -------------------------------------------------------
// Misc

void signHome(int x, int y) {
  // gives a sign for a house / home sign

  final int width1=6;
  final int height1=8;

  fill( 2, 255, 0 );
  strokeWeight(1);
  stroke ( 2, 255, 0 );
  noFill();
  x+=21;
  y+=14;
  triangle ( x-width1, y, 
    x, y-height1, 
    x+width1, y );
  rect(x-width1/2, y, width1, width1);
  rect(x-width1/4+1, y+height1/4+1, width1/4, width1/3);
  strokeWeight(1);
}

// =====================================================================