简体   繁体   English

通过Java游戏时返回主菜单

[英]Return to main menu when game over java

I'm on the finnishing touches my first game and are having issues returning to the main menu when the player lose the game. 我正在完成我的第一个游戏的修饰,当玩家输掉游戏时,返回主菜单时遇到问题。

Main(Game) class: 主类(游戏类):

public static void main(String[] args) throws InterruptedException {
    Main main = new Main();

This is where the menu is added to my frame. 这是菜单添加到我的框架的地方。 The menu itself is a JComponent with graphics and mouselisteners: 菜单本身是带有图形和鼠标监听器的JComponent:

main.setMenu();
    while (true) {
        menuChoice = meny.getDecision();
        if (i == 0 && menuChoice == 2) {
            main.run(cp);
        } else if (i > 0) {
            main.run(cp1);
        } else if (menuChoice == 0) {
            System.exit(0);
        }

public void run(Computer cp) throws InterruptedException {
    if (time == 0) {

This is where my frame gets the game's background and so on: 这是我的框架获取游戏背景的地方,依此类推:

        setWindow(data, player, cp);
        time++;
    }

    //Here's a bunch of irrelevant ifs

    } if (cpScore.returnScore() == 10 && time > 0) {

Here's where you should return to main menu. 在这里您应该返回主菜单。 What needs to be done here is basically what the mainmethod does at the absolute beginning (add a Mainmenu-frame that takes input for further instructions). 基本上,这里需要做的是main方法在绝对开始时所做的事情(添加一个Mainmenu框架,该框架接受输入以供进一步说明)。

}
}

Any ideas? 有任何想法吗?

可能最简单的方法是使用初始化为true的布尔值将主菜单设置为其自己的方法,然后只要该值为true,菜单就会继续进行,当您想返回主菜单时,只需更改此设置即可值返回到false,结束循环并允许返回主菜单方法。

Create some functions inside of your main class that you can call to do what you are requesting as needed. 在您的主类中创建一些函数,您可以调用这些函数来根据需要执行您要的操作。 No one is going to write the code for you, but here are some ideas 没有人会为您编写代码,但是这里有一些想法

if (main.isGameOver() || main.isMainMenuRequested()) { 
   main.stopGameThread();
   main.purgeOldGraphicsEtc();
   main.executeMenuThread();
}

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

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