How to imput a main menu screen?

edited April 2017 in Questions about Code

For my computing class, I have to make a game that starts with a main menu screen, and when a key is pressed, moves into the game. While searching for how to do this, I found information about states, but I am not quite sure how to make this work for my code. This is what I have so far.

int u=325; //y-value for soccer ball
int v=250; //x-value for soccer ball
int w=0;//x-value for goal
int x=150; //y-value for easy
int y=90; //y-value for medium
int z=35; //y-value for difficult
int savedTime; //save the time
int totalTime=35000;
PImage goal; //goal image
PImage background; //background image
PImage ball; //soccer ball image
int score = 0;
int state=0;
final int mainMenu=0;
final int game=1;


void settings(){
    size(500, 375);
}

void setup() {
  //Makes images appear
  goal=loadImage("Soccer Goal.png");
  background=loadImage("Field picture.jpg");
  ball=loadImage("Soccer ball.png");
  savedTime=millis();
}

void draw() {
  switch(state) {
  case mainMenu:
  background(255);
  if(key=='z'){
    break;
  }
  case game:
    break;
  }

  //background must be in start of draw, otherwise unwanted trail occurs behind pictures
  background(background);
  textSize(30);
  text("Score: " + score, 30, 30);

  image(ball, v, u, 40, 40);

  int passedTime=millis()-savedTime;

  if (passedTime>totalTime) {
    fill(255);
    stroke(255, 10, 10);
    rect(-20, 80, 520, 295); //background box
    fill(255, 10, 10); //text color
    text("GAME OVER", 165, 200);
    text("PRESS X TO PLAY AGAIN",75,235);
    if(key=='x'){
      setup();
      draw();
      score=0;
    }
  }

  if (score <= 8) {
    game1();
  }
  if (score > 8 && score <= 16) {
    game2();
  }
  if (score > 16) {
    game3();
  }
  if (passedTime>totalTime) {
    fill(255);
    stroke(255, 10, 10);
    rect(0, 80, 500, 295); //background box
    fill(255, 10, 10); //text color
    text("GAME OVER", 165, 200);
    text("PRESS X TO PLAY AGAIN",75,235);
    if(key=='x'){
      setup();
      draw();
      score=0;
    }
  }
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      v=v+3;
    }
    if (keyCode == LEFT) {
      v=v-3;
    }

    if (keyCode == UP) {
      u=height-285; //difficult goal ball shoot
      if (score <= 8) {
      if(v > w && v < w +250) {
        score++;
      }
    }
    if (score > 8 && score <= 16) {
      if(v > w && v < w +200) {
        score ++;
      }
    }
    if (score > 16) {
      if(v > w && v < w +150) {
        score ++;
      }
    }

    }
    if (keyCode == DOWN) {
      u=325;
    }
  }
}
Tagged:

Answers

  • Why do you have key==z in line 34?

    You are in the right track. You should do something like this:

    void draw(){
    switch(state) {
      case mainMenu:
      runMenu();
        break;
      }
      case game:
        runGame();
        break;
      }
    }
    
    void runMenu(){
      //SHOW your menu here
    }
    void runGame(){
       //RUN your game here
    }
    
    //Next assumes only two state, either 0 or 1
    void mouseReleased(){
       state= state==mainMenu?game:mainMenu;  //Ternary operator
       //state= (state+1)%2;   //Does the same   
    }
    

    And you switch between your menu and your game by changing the value of state. For example, in the above code and change the state by a mouse event. You should adapt your code to your needs.

    Kf

  • I have an opening screen now, and it moves into the game when the key is pressed. When I go to shoot the ball, though, the game pulls back up the start screen.

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim m;
    AudioPlayer s1;
    int u=325; //y-value for soccer ball
    int v=250; //x-value for soccer ball
    int w=0;//x-value for goal
    int x=150; //y-value for easy
    int y=90; //y-value for medium
    int z=35; //y-value for difficult
    int savedTime; //save the time
    int totalTime=35000;
    PImage goal; //goal image
    PImage background; //background image
    PImage ball; //soccer ball image
    int score = 0;
    int state=0;
    final int mainMenu=0;
    final int game=1;
    
    void settings(){
        size(500, 375);
    }
    
    void setup() {
      //Makes images appear
      m= new Minim(this);
      s1=m.loadFile("music.mp3",1024);
      s1.play();
      s1.loop();
      goal=loadImage("Soccer Goal.png");
      background=loadImage("Field picture.jpg");
      ball=loadImage("Soccer ball.png");
      savedTime=millis();
    }
    
    void draw() {
      switch(state) {
      case mainMenu:
        showMenu();
        break;
      }
      switch(state){
      case game:
        runGame();
        break;
      }
    }
    
    void showMenu(){
      background(255);
      fill(50);
      text("Using the arrow keys, move the soccer ball left and right",100,100);
      text("Use the up button to shoot the ball towards the moving goal",90,125);
      text("Press z to begin the game",150,150);
      if(key=='z'){
        runGame();
      }
    }
    
    void runGame(){
      //background must be in start of draw, otherwise unwanted trail occurs behind pictures
      background(background);
      textSize(30);
      text("Score: " + score, 30, 30);
    
      image(ball, v, u, 40, 40);
    
      int passedTime=millis()-savedTime;
    
      if (passedTime>totalTime) {
        fill(255);
        stroke(255, 10, 10);
        rect(-20, 80, 520, 295); //background box
        fill(255, 10, 10); //text color
        text("GAME OVER", 165, 200);
        text("PRESS X TO PLAY AGAIN",75,235);
        if(key=='x'){
          setup();
          draw();
          score=0;
        }
      }
    
      if (score <= 8) {
        game1();
      }
      if (score > 8 && score <= 16) {
        game2();
      }
      if (score > 16) {
        game3();
      }
      if (passedTime>totalTime) {
        fill(255);
        stroke(255, 10, 10);
        rect(0, 80, 500, 295); //background box
        fill(255, 10, 10); //text color
        text("GAME OVER", 165, 200);
        text("PRESS X TO PLAY AGAIN",75,235);
        if(key=='x'){
          setup();
          draw();
          score=0;
        }
      }
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == RIGHT) {
          v=v+3;
        }
        if (keyCode == LEFT) {
          v=v-3;
        }
    
        if (keyCode == UP) {
          u=height-285; //difficult goal ball shoot
          if (score <= 8) {
          if(v > w && v < w +250) {
            score++;
          }
        }
        if (score > 8 && score <= 16) {
          if(v > w && v < w +200) {
            score ++;
          }
        }
        if (score > 16) {
          if(v > w && v < w +150) {
            score ++;
          }
        }
    
        }
        if (keyCode == DOWN) {
          u=325;
        }
      }
    }
    
  • Yes... ehhh, don't call draw and setup inside runGame(). The purpose of your program is to call your menu OR your game inside draw. The function draw() runs 60 times per second! Please check the reference:

    https://processing.org/reference/draw_.html

    Kf

  • I tried moving that section out of runGame() and it made the timer for the game to end stop working as well as the game's ability to reset without doing anything about the main menu redisplaying. I also tried to make the section into it's own stage, but it had the same effect.

Sign In or Register to comment.