简体   繁体   English

带有 switch 语句的主菜单

[英]main menu with switch statement

So for a school project, I have to create a game with a program called 'Processing'.因此,对于一个学校项目,我必须使用一个名为“处理”的程序创建一个游戏。 I am creating a main menu with the switch statement.我正在使用 switch 语句创建一个主菜单。 For that I want to use the buttons 'Start', 'Help', and 'exit'.i would like to use those buttons to change the variables of the switch statement.为此,我想使用“开始”、“帮助”和“退出”按钮。我想使用这些按钮来更改 switch 语句的变量。 Therefore I'm using "mousePressed".因此我使用“mousePressed”。 The problem is that which button I'm pressing, is giving me the same result as the 'exit' button.问题是我按下的哪个按钮给了我与“退出”按钮相同的结果。 Could somebody give me tips on how I can structure my menu better or even make my button work?有人可以给我一些提示,告诉我如何更好地构建我的菜单,甚至让我的按钮工作吗? I am using a library on Processing called 'ControlP5' to make my buttons.我正在使用一个名为“ControlP5”的处理库来制作我的按钮。

here is my code so far:到目前为止,这是我的代码:

int mode; // 1: intro screen, 2: game  , 3: game over 

final int INTRO    = 1;
final int PLAY     = 2;
final int GAMEOVER = 3;

//==============================================================
void setup(){
size(1920,1080);
mode = 1;
}
//========================================================

void draw(){

  if(mode == 1){
    introScreen();   
  }
  else if (mode == 2){
    gameItself();   
  } 
   else if (mode == 3){
     gameOver();   
   }
   else println("mode error");{
   } 
}

void introScreen(){
mode = 1;
static int Page = 0;
import controlP5.*;


ControlP5 cp5;


 cp5= new ControlP5(this);
 switch(Page){

     case 0: // main menu
     cp5.addButton("Start").setValue(0).setPosition(1420,250).setSize(400,100);

     cp5.addButton("Exit").setValue(0).setPosition(1420,650).setSize(400,100);

     cp5.addButton("Help").setValue(0).setPosition(1420,450).setSize(400,100);
       break;

     case 1: //help menu

      cp5.addButton("Back").setValue(0).setPosition(1420,450).setSize(400,100);
        break;
   }



public void Start(){
 if(mousePressed){
   mode = 2; // switching to the game itself 
 }
  println("Start");
  }

  public void Exit(){
   if(mousePressed){
  exit(); }
  println("Exit");
  }

 public void Help(){


     Page = 1;

  println("Help");
  }

  public void Back(){
  if(mousePressed){
    Page  = 0;
  }
  println("Back");
  }




  void gameItself(){
// game and stuff
}

void gameOver(){
//gameover 
}

Take a look at how the mousePressed event works.看看mousePressed事件是如何工作的。 You may use this information useful.您可能会使用这些有用的信息。

To achieve your goal as to change the Page variable by clicking buttons, there are multiple options.要实现通过单击按钮更改Page变量的目标,有多个选项。 First, I'll go with the easier one, the one which doesn't need an import but just check for coordinates.首先,我将 go 使用更简单的,不需要导入但只需检查坐标的那个。 Then I'll do the same thing, but with controlP5.然后我会做同样的事情,但使用 controlP5。

1. Just checking where the clicks lands 1. 只检查点击的位置

I'll go with the most basic one: detecting the "click" and checking if it's coordinates are inside a button.我将 go 与最基本的一个:检测“点击”并检查它的坐标是否在按钮内。

First, we'll add the mouseClicked() method.首先,我们将添加mouseClicked()方法。 This method is called every time a mouse button is pressed.每次按下鼠标按钮时都会调用此方法。

// I'm typing this out of IDE si there may be some quirks to fix in the code
void mouseClicked() {
  switch(Page) {
    case 0:
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 250 && mouseY < 250+100) {
        // You're in the main menu and Start was clicked
      }
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 650 && mouseY < 650+100) {
        // You're in the main menu and Exit was clicked
      }
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
        // You're in the main menu and Help was clicked
      }
      // You should use 'else if' instead of 3 different if, but I coded it like that so it would be easier to see the small differences between the coordinates
    case 1:
      if (mouseX > 1420 && mouseX < 1420+400 && mouseY > 450 && mouseY < 450+100) {
        // You're un the help menu and Back was clicked
      }
  }
}

As you can see, I just used the coordinates and size of your buttons to check if the click was located inside one.如您所见,我只是使用按钮的坐标和大小来检查点击是否位于其中。 That's kind of ninja-ing my way out of this issue.这是我摆脱这个问题的一种忍者。 I don't know how far into programming you are, or else I would recommand to build a class to handle user inputs, but this way is easy to manage for small exercises like homework.我不知道你对编程有多远,否则我会建议构建一个 class 来处理用户输入,但这种方式对于家庭作业等小型练习很容易管理。

2. Designing controlP5 buttons 2. controlP5按钮设计

I'm not a ControlP5 expert, so we'll keep close to the basics.我不是 ControlP5 专家,所以我们将保持接近基础。

I'll be blunt, the code you provided is ripe with problems, and it's not so many lines, so instead of pointing where it goes wrong I'll give you some skeleton code which will work and on which you can build some understanding.我会直言不讳,您提供的代码已经存在问题,而且行数不多,所以我不会指出哪里出错了,我会给您一些可以工作的骨架代码,您可以在上面建立一些理解。 I'll give you the tools and then you can make your project work.我会给你工具,然后你可以让你的项目工作。

When you design your buttons, if you design them all in the same object, they'll share some properties.当您设计按钮时,如果您将它们全部设计在同一个 object 中,它们将共享一些属性。 For an example, all your buttons will be visible or invisible at the same time.例如,您的所有按钮将同时可见或不可见。 You don't need to redraw them all the time, because they already handle this, so you need to manage them with another method.你不需要一直重绘它们,因为它们已经处理好了,所以你需要用另一种方法来管理它们。

You should design your buttons as global objects (as you did), and add them to the ControlP5 object which makes the most sense.您应该将按钮设计为全局对象(就像您所做的那样),并将它们添加到最有意义的 ControlP5 object 中。 You can have one button per object if you want, or many if they are linked together, for an example all the "menu" buttons which appears at the same time could be owned by the same object.如果需要,每个 object 可以有一个按钮,或者如果它们链接在一起,则可以有多个按钮,例如,同时出现的所有“菜单”按钮都可以归同一个 object 所有。 Design your buttons in the setup() method if you design them only one time for the whole program.如果您只为整个程序设计一次按钮,请在setup()方法中设计按钮。 Of course, if this was more than an homework, you may want to avoid the buttons being globals, but it'll be much easier to keep them in memory for a short project.当然,如果这不仅仅是一项家庭作业,您可能希望避免这些按钮是全局的,但将它们保留在 memory 中用于一个简短的项目会容易得多。

The "name" of the button is also the name of the method that it'll try to call if you click on it.按钮的“名称”也是单击它时尝试调用的方法的名称。 Two buttons cannot share the same "name".两个按钮不能共享相同的“名称”。 Buttons can have a set value which will be sent to the method that they call.按钮可以有一个设置值,该值将发送到它们调用的方法。

You don't need to use Processing's mouse events for the buttons to work.您不需要使用 Processing 的鼠标事件来使按钮工作。 They are self-contained: they have their own events, like being clicked on or detecting when the mouse is over them.它们是独立的:它们有自己的事件,例如被点击或检测鼠标何时悬停在它们上方。 Here's the documentation for the full list of the methods included in the ControlP5 buttons . 这是 ControlP5 按钮中包含的方法的完整列表的文档

You don't need to manage the buttons in the draw() loop.您不需要管理draw()循环中的按钮。 They manage themselves.他们管理自己。

Here's some skeleton code to demonstrate what I just said.这里有一些骨架代码来演示我刚才所说的。 You can copy and paste it in a new Processing project and run it to see what's going on.您可以将其复制并粘贴到新的处理项目中并运行它以查看发生了什么。

ControlP5 cp5;
ControlP5 flipVisibilityButton;
int Page = 0;

void setup() {
  size(1920, 800);
  textAlign(CENTER, CENTER);
  textSize(60);
  fill(255);

  cp5 = new ControlP5(this);    // this is ONE object, which will own buttons.
  cp5.addButton("MenuButton0")  // this is the name of the button, but also the name of the method it will call
    .setValue(0)  // this value will be sent to the method it calls
    .setPosition(1420, 250)
    .setSize(400, 100);
  cp5.addButton("MenuButton1")
    .setValue(1)
    .setPosition(1420, 450)
    .setSize(400, 100);
  cp5.addButton("MenuButton2")
    .setValue(2)
    .setPosition(1420, 650)
    .setSize(400, 100);

  flipVisibilityButton = new ControlP5(this);  // this is a different object which own it's own controls (a button in this case)
  flipVisibilityButton.addButton("flipVisibility")
    .setValue(2)
    .setPosition(200, height/2)
    .setSize(200, 100);
}

void draw() {
  // No button management to see here
  background(0);
  // showing which button has been pressed while also keeping watch to see if the mouse is over one of the cp5 buttons
  text(Page + "\n" + cp5.isMouseOver(), width/2, height/2);
}

void MenuButton0(int value) {
  ChangePage(value);
}

void MenuButton1(int value) {
  ChangePage(value);
}

void MenuButton2(int value) {
  ChangePage(value);
}

void ChangePage(int value) {
  Page = value;
}

void flipVisibility(int value) {
  // When the buttons are invisible, they are also unclickable
  cp5.setVisible(!cp5.isVisible());
}

You should be able to expand on this example to do your project, but if you have difficulties don't hesitate to comment here and ask further questions.您应该能够扩展此示例来完成您的项目,但如果您有困难,请不要犹豫,在这里发表评论并提出更多问题。 Have fun!玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM