Download and save a tile based game in Java. XML or TXT?

For some time I have been doing a 2-dimensional game on a sim, and everything is going well! Thanks to this site and its kind people (you!), I just finished the search path, which is fantastic! THANKS! ... In any case, to the question.

Currently, the test level is hard-coded into the game. Clearly, I need to recycle this. My idea was to keep all the variables in the Game class stored in a text file in various ways. I could also write the details of each level into a file to be loaded for the level. My question is: should I use only a text file or use XML? I understand that XML is basically, but I really don’t know how I will use it in conjunction with JAVA, or why it is preferable or not from a plain text file. I had a little Google, and there are ALL books on XML and JAVA! I don’t see that I need to know EVERYTHING about how to use XML with JAVA to do this work, but I need to know more than me.

Thanks in advance for your help.

Edit: I will try a few ideas here until I find one that works best, and then this will be chosen as the answer.

Edit: Select xStream. Simply and easily!

+3
source share
12 answers

If you have a game state in an object (for example, a HashMap or perhaps an object of your own design), you can write it as an XML file trivially using XStream , and then read it back to the new object using XStream.

, , /. - - - . JAXP, SAX, DOM ..

+1

2D- XML, , . XML / , , 2D- , .

, (, ). , 2D- .

SokoBean (, 10 , , , ) , . , XML, .

+4

. : , , . , . , .

, - ( , -, xml - xml ).

+2

, , , .

, ints, . XML.

Edit:

:

, ! , ( ) . , . , - , , ..

+2

, , XML , XML .

  • / , , .
  • Java , XML-.
  • , .
  • , XML,
+2

, , XML . , , XML , , , , .

, java.io.ObjectOutputStream j ava.io.ObjectInputStream, ( ) java.util.Properties ( ).

, XML , " ", , Java, , , , .

, 2D- XML , .

.)

WWWWWWWWWW  
WSWWWWW EW  
W W K WW W  
W W   WW W  
W WW WWWDW  
W  X     W  
WWWWWWWWWW  

- , W - , S - , E - , K , D - , X - , , , , .

. , XML, - , XML " ", , , .

:

SpawnFrequency 0.1 20.5
HealthPoints 5
Stats 12 500 30 10 11  

, . , Java, BufferedReader.readLine() , String.split(), . switch /else , split, , . , XML-, . XML, , , :

XML:         

:

`<GuyStuff>`  
    `<SpawnFrequencyMinimum>0.1</SpawnFrequencyMinimum>`  
    `<SpawnFrequencyMaximum>20.5</SpawnFrequencyMaximum>`  
    `<Health>5</Health>`  
    `<Strength>12</Strength>`  
    `<Agility>500</Agility>`  
    `<Stamina>30</Stamina>`  
    `<TechnoWizardry>10</TechnoWizardry>`  
    `<JavaSkillz>11</JavaSkillz>`  
`</GuyStuff>`  

, java.io.Serializable, .

Serializable.

public class MySaveData implements java.io.Serializable //Serializable doesn't need any methods implemented, it simply says, "hey Java, my object can be written to a file."
{
    float data1;
    int data2;
    Object data3;
    //BufferedImage data4; //Note that in order for an object to serialize, it can't contain any data members that are not Serializable, so this would be illegal.
}

import java.io.*;
public class DataReader
{
    public void saveData(File loc, MySaveData obj1, MySaveData obj2, MySaveData obj3)
    {
        try
        {
            //You can use any type of OutputStream to create an OOS, so you can for
            //example use one when you're connected with Sockets or something similar.
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(loc));

            //Writing objects is tremendously easy, and so are primitives.
            //Note that the order you write them in is the same order you will read them.
            os.writeObject(obj1);
            os.writeObject(obj2);
            os.writeObject(obj3);
            os.writeDouble(someValueINeededToWrite);
            os.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void loadData(File loc, PlaceToPutData place)
    {
        try
        {
            //ObjectInputStream follows the same rules as OOS mentioned above.
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(loc));

            //Remember to read your data back the same order you wrote it.
            place.obj1 = is.readObject();
            place.obj2 = is.readObject();
            //If you don't want one of the objects for some reason and want
            //to get something after it, you can just read the next one but not use it.
            readObject();
            place.value = is.readDouble();
            is.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

: new java.io.File("/Documents/MyGame/MyDataFile.whateverIFeelLikeAsLongAsItsConsistent").

+2

, , .

XML java POJO , . , , , .

, SQL- .

0

, , , . , , , XML. XML , , ( ) .

0

XML, - XML-, : , , / .. , , , ,

- - , - : " , XML-?" : " ".

0

, JavaDB, : http://developers.sun.com/javadb/

2,5 , , , .

ORM, Hibernate, , , , . JDBC .

.

0

? , - . , .

, XML. -

<Map>
    <Tile x="1" y="1" value="2"></Tile>
    <Tile x="1" y="2" value="1"></Tile>
    <Tile x="1" y="3" value="7"></Tile>
    ...
</Map>

.

0

( java.io.Serializable), . ObjectOutputSteam . , ObjectInputStream.

Martine

0
source

Source: https://habr.com/ru/post/1711942/


All Articles