加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何最好地在java中存储游戏配置变量

发布时间:2020-05-25 11:38:16 所属栏目:Java 来源:互联网
导读:我正在编写一个小型java游戏,并将全局游戏设置存储在类结构中,如下所示:public class Globals { public static int tileSize = 16; public static String screenshotDir = ..somepath..; public static Stri

我正在编写一个小型java游戏,并将全局游戏设置存储在类结构中,如下所示:

public class Globals {
    public static int tileSize = 16;
    public static String screenshotDir = "..somepath..";
    public static String screenshotNameFormat = "gameNamexxx.png";
    public static int maxParticles = 300;
    public static float gravity = 980f;
    // etc
}

虽然这很方便,但我想知道这是否是可接受的模式. 最佳答案 将其存储在.properties文件中.

config.properties

tile.size=16
screenshot.dir=..somepath..

阅读它

// Make sure this happens only the first time you start your application
Properties properties = new Properties();
// You can use FileInputStream,ClassLoader.getResourceAsStream or a reader too
properties.load(...)

使用它

int tileSize = Integer.valueOf(properties.getProperty("tile.size"));
String screenshotDir = properties.getProperty("screenshot.dir");

为了简化操作并保持最小化,您还可以执行以下操作:

public class Globals {
    private static final Properties properties = new Properties();

    static {
        // do the loading here
    }

    public static final int TILE_SIZE = 
        Integer.valueOf(properties.getProperty("tile.size"));
    public static final String SCREENSHOT_DIR = 
        properties.getProperty("screenshot.dir");
    // etc
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读