存储游戏数据的最佳方式? (图像,地图等) [英] Best way to store data for your game? (Images, maps, and such)

查看:423
本文介绍了存储游戏数据的最佳方式? (图像,地图等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本的2D游戏(以及一个游戏引擎),我目前正在为我的数据开发文件格式。当然,对于这个游戏来说,我需要缓存。我发现将游戏的所有数据都保留在一个文件中是非常不专业的(不一定,只要文件是某种缓存格式)。

I'm creating a basic 2D game (well a game engine) and I've currently been developing the file formats for my data. Of course, for this game to run, I will need cache. I find it quite unprofessional to leave all the data for the game to not be in a single file (well not necessarily, as long as the files are in a cache format of some kind).

这就是我来这里问的原因。我在考虑做一个zip文件,但我觉得这不是最好的方法。我还在考虑做另一个二进制写入器,它将具有标题(文件类型,位置)和每个文件的封闭标记,因此很容易解释。但我觉得这样效率太低了。

So that's why I've came here to ask. I was thinking of doing a zip file but I feel like that isn't the best way at all. I was also thinking of doing another binary writer which will have headers (type of file, "location") and an enclosing tag for each of the files so it would be easy to interpret. But I felt like that was too inefficient.

那么请你有什么想法吗?

So please, do you have any ideas?

注意:这个游戏引擎真的只是为了学习目的。

Note: This game engine is really just for learning purposes.

tl; dr我需要一种有效的方法来保存我正在制作的游戏中的图像等数据。

tl;dr I need an efficient way to store data such as images for my game I'm making.

推荐答案

您可以将任何对象存储为 .dat文件

You can store any object as .dat file:

public class MyGame implements Serializable 
{ 
    private static void saveGame(ObjectType YourObject, String filePath) throws IOException 
    { 
        ObjectOutputStream outputStream = null; 
        try 
        { 
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath)); 
            outputStream.writeObject(YourObject); 
        } 
        catch(FileNotFoundException ex) 
        { 
            ex.printStackTrace(); 
        } 
        catch(IOException ex) 
        { 
            ex.printStackTrace(); 
        } 
        finally 
        { 
            try 
            { 
                if(outputStream != null) 
                { 
                    outputStream.flush(); 
                    outputStream.close(); 
                } 
            } 
            catch(IOException ex) 
            { 
                ex.printStackTrace(); 
            } 
        } 
    } 

    public static ObjectType loadGame(String filePath) throws IOException 
    { 
        try 
        { 
            FileInputStream fileIn = new FileInputStream(filePath); 
            ObjectInputStream in = new ObjectInputStream(fileIn); 
            return (ObjectType) in.readObject(); 
        } 
        catch(FileNotFoundException ex) 
        { 
            ex.printStackTrace(); 
        } 
        catch(IOException ex) 
        { 
            ex.printStackTrace(); 
        } 
    } 
}

这篇关于存储游戏数据的最佳方式? (图像,地图等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆