繁体   English   中英

如何在不退出 Java 代码的情况下退出 2D 游戏模拟(仅)?

[英]How to exit 2D game simulation (only) without exiting the Java code?

我(Java 新手)正在使用 Golden T Studios Game dev jdk 构建一个已有数十年历史的 Java 项目。 我在项目中有一个运行 2D 模拟的游戏。 简而言之,代码结构如下:

package game;
import java.io.*;
import java.lang.reflect.Field;
import javax.swing.JFrame;

import com.golden.gamedev.GameLoader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import sometcpimports.*;

public class MainGAME extends JFrame implements Runnable {

    public static void main(String[] args) { //call new MainGAME;
    }

    public MainGAME() {//initiate game parameters; 
    //start new THREAD; 
    }

   @Override
   public void run() { //initiate new game and environment;
    game = new GameLoader();
    gameenv = new GameEnvironment(params); //This class is in another file "public class GameEnvironment extends Game {}" 
    //I don't clearly undertsand what the following lines do, so I'm mentioning them as is;

    game.setup(gameenv, dimensions);
    this.setVisible(false);
    gameenv.setVisible(false);
    game.start();
    game.setVisible(true);

    //tbd (code reaches this step)

    }
}

我的目标是多次运行上述模拟(每次使用不同的输入)并在每次运行后在新的主 class 中提取信息,如下所示。

public class gamedriver {
    public static void main(String[] args) {
        MainGAME.params = some params;
        MainGAME.main(); // runs the simulation;

        //tbd1 (code doesn't reach this step)
    }
}

问题是,在从不同的文件(新的主类)运行模拟时,我无法在一次运行后退出 2D 模拟器。 代码在新的主 class 中没有到达//tbd1 ,其中有一些 output 打印语句。 本质上我想简单地退出模拟器而不是整个 JVM。 到目前为止,我已经尝试过:

  1. //tbdgame.stop()gameenv.finish()什么都不做。
  2. System.exit(0) at //tbd在模拟后退出游戏但也退出 jvm 并且没有到达其他主要 class。
  3. //tbd和 GameEnvironment class 处的finish() ,其行为与第 2 点完全相同。

此外,我无法在 for 循环中运行上述(游戏驱动程序类中的 MainGAME)。 这会引发Game.Exception error 我知道它与线程有关,但我不确定如何运行它们。

感谢您的时间。 感谢你的帮助!

我找到了解决上述问题的方法。 我将我的解决方法发布为遇到类似问题的人的参考。 请注意,可能还有其他(更好的)解决方案。

  1. 我禁用了 MainGAME 中的所有(任何)线程实现(只需将其注释掉)。
  2. 我在 MainGAME 中添加了System.out.println()以在模拟结束时打印我的所有输出。
  3. 我的第二个脚本使用运行时进程来执行 MainGAME:
public class gamedriver {
    public static void main(String[] args) {
        try {
            String separator = System.getProperty("file.separator");
            String classpath = System.getProperty("java.class.path");
            String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
            String[] command = new String[]{path, "-cp", classpath, gamedriver.GAME.class.getName(), "output.csv"};
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class GAME {

        public static void main(String args[]) {
            PrintStream out = null;
            try {
                out = new PrintStream(args[1]); 
            } catch (FileNotFoundException p) {
                p.printStackTrace();
            }

            System.setOut(out);// this catches all output from the game to a csv file (output.csv)
            MainGAME temp = new MainGame(some params); // initiate the simulation
            temp.run()
            out.close();
            System.exit(0); // force close the simulator
        }
    }
}

  1. 我解析 output.csv 得到解决方案。

这可能是解决问题的一种草率方法,但它对我有用,特别是因为 GTGE 提供的其他方法都不起作用。

暂无
暂无

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

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