Best way to store data for your game? (Images, maps, and such)

前端 未结 3 1601
太阳男子
太阳男子 2020-12-18 06:58

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 fi

3条回答
  •  情歌与酒
    2020-12-18 07:32

    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(); 
            } 
        } 
    }
    

提交回复
热议问题