带有 switch 语句的主菜单
main menu with switch statement
因此,对于一个学校项目,我必须使用名为 'Processing' 的程序创建一个游戏。
我正在使用 switch 语句创建一个主菜单。为此,我想使用按钮 'Start'、'Help' 和 'exit'。我想使用这些按钮来更改 switch 语句的变量。因此我使用 "mousePressed"。问题是我按下哪个按钮,给我的结果与 'exit' 按钮相同。有人可以给我一些提示,告诉我如何更好地构建我的菜单,甚至让我的按钮起作用吗?我正在使用一个名为 'ControlP5' 的 Processing 库来制作我的按钮。
到目前为止,这是我的代码:
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
}
看看 mousePressed 活动是如何运作的。您可能会使用这些有用的信息。
要实现通过单击按钮更改 Page
变量的目标,有多种选择。首先,我将使用更简单的方法,即不需要导入但只需检查坐标的方法。然后我会做同样的事情,但使用 controlP5.
1。只需检查点击的位置
我将使用最基本的方法:检测 "click" 并检查它的坐标是否在按钮内。
首先,我们将添加 mouseClicked()
方法。每次按下鼠标按钮时都会调用此方法。
// 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
}
}
}
如您所见,我只是使用了您按钮的坐标和大小来检查点击是否位于其中。这是我解决这个问题的一种忍者方式。我不知道你在编程方面有多深入,否则我会建议构建一个 class 来处理用户输入,但这种方式对于像家庭作业这样的小练习来说很容易管理。
2。设计 controlP5 按钮
我不是 ControlP5 专家,所以我们将紧跟基础知识。
我会直言不讳,你提供的代码已经有问题了,而且它没有那么多行,所以我不会指出哪里出错了,我会给你一些框架代码,你可以在其中运行可以建立一些理解。我会给你工具,然后你就可以开始你的项目了。
当您设计按钮时,如果您将它们全部设计在同一个对象中,它们将共享一些属性。例如,您的所有按钮将同时可见或不可见。你不需要一直重绘它们,因为它们已经处理过了,所以你需要用另一种方法来管理它们。
您应该将按钮设计为全局对象(就像您所做的那样),并将它们添加到最有意义的 ControlP5 对象中。如果需要,您可以为每个对象设置一个按钮,或者如果它们链接在一起,则可以设置多个按钮,例如,同时出现的所有 "menu" 按钮都可以归同一个对象所有。如果整个程序只设计一次按钮,请使用 setup()
方法设计按钮。当然,如果这不仅仅是一项家庭作业,您可能希望避免按钮是全局的,但对于一个简短的项目来说,将它们保存在内存中会容易得多。
按钮的 "name" 也是您单击它时将尝试调用的方法的名称。两个按钮不能共享相同的 "name"。按钮可以有一个设置值,该值将被发送到它们调用的方法。
您不需要使用 Processing 的鼠标事件来使按钮工作。它们是独立的:它们有自己的事件,比如被点击或检测鼠标何时悬停在它们上面。 Here's the documentation for the full list of the methods included in the ControlP5 buttons.
您不需要管理 draw()
循环中的按钮。他们自己管理。
这里有一些骨架代码来演示我刚才所说的内容。您可以将其复制并粘贴到新的 Processing 项目中,然后 运行 查看发生了什么。
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());
}
您应该可以扩展此示例来完成您的项目,但如果您遇到困难,请不要犹豫,在这里发表评论并提出更多问题。玩得开心!
因此,对于一个学校项目,我必须使用名为 'Processing' 的程序创建一个游戏。 我正在使用 switch 语句创建一个主菜单。为此,我想使用按钮 'Start'、'Help' 和 'exit'。我想使用这些按钮来更改 switch 语句的变量。因此我使用 "mousePressed"。问题是我按下哪个按钮,给我的结果与 'exit' 按钮相同。有人可以给我一些提示,告诉我如何更好地构建我的菜单,甚至让我的按钮起作用吗?我正在使用一个名为 'ControlP5' 的 Processing 库来制作我的按钮。
到目前为止,这是我的代码:
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
}
看看 mousePressed 活动是如何运作的。您可能会使用这些有用的信息。
要实现通过单击按钮更改 Page
变量的目标,有多种选择。首先,我将使用更简单的方法,即不需要导入但只需检查坐标的方法。然后我会做同样的事情,但使用 controlP5.
1。只需检查点击的位置
我将使用最基本的方法:检测 "click" 并检查它的坐标是否在按钮内。
首先,我们将添加 mouseClicked()
方法。每次按下鼠标按钮时都会调用此方法。
// 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
}
}
}
如您所见,我只是使用了您按钮的坐标和大小来检查点击是否位于其中。这是我解决这个问题的一种忍者方式。我不知道你在编程方面有多深入,否则我会建议构建一个 class 来处理用户输入,但这种方式对于像家庭作业这样的小练习来说很容易管理。
2。设计 controlP5 按钮
我不是 ControlP5 专家,所以我们将紧跟基础知识。
我会直言不讳,你提供的代码已经有问题了,而且它没有那么多行,所以我不会指出哪里出错了,我会给你一些框架代码,你可以在其中运行可以建立一些理解。我会给你工具,然后你就可以开始你的项目了。
当您设计按钮时,如果您将它们全部设计在同一个对象中,它们将共享一些属性。例如,您的所有按钮将同时可见或不可见。你不需要一直重绘它们,因为它们已经处理过了,所以你需要用另一种方法来管理它们。
您应该将按钮设计为全局对象(就像您所做的那样),并将它们添加到最有意义的 ControlP5 对象中。如果需要,您可以为每个对象设置一个按钮,或者如果它们链接在一起,则可以设置多个按钮,例如,同时出现的所有 "menu" 按钮都可以归同一个对象所有。如果整个程序只设计一次按钮,请使用 setup()
方法设计按钮。当然,如果这不仅仅是一项家庭作业,您可能希望避免按钮是全局的,但对于一个简短的项目来说,将它们保存在内存中会容易得多。
按钮的 "name" 也是您单击它时将尝试调用的方法的名称。两个按钮不能共享相同的 "name"。按钮可以有一个设置值,该值将被发送到它们调用的方法。
您不需要使用 Processing 的鼠标事件来使按钮工作。它们是独立的:它们有自己的事件,比如被点击或检测鼠标何时悬停在它们上面。 Here's the documentation for the full list of the methods included in the ControlP5 buttons.
您不需要管理 draw()
循环中的按钮。他们自己管理。
这里有一些骨架代码来演示我刚才所说的内容。您可以将其复制并粘贴到新的 Processing 项目中,然后 运行 查看发生了什么。
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());
}
您应该可以扩展此示例来完成您的项目,但如果您遇到困难,请不要犹豫,在这里发表评论并提出更多问题。玩得开心!