到目前为止,我正在制作一个你玩的游戏,然后每五秒就会保存一次游戏。到目前为止我有一个好的保存系统。假设你有 100 个木头、30 个面包和 0 个泥土,那么它会保存到文件“100 30 0”。每次启动应用程序时,它都会通过以下方式加载保存:
public static void loadSave(File loadPath){
try{
Scanner loadScanner = new Scanner(loadPath);
while(loadScanner.hasNext()){
for (int i = 0; i < ldStr.length; i++) {
ldStr[i] = loadScanner.nextInt();
if(i == 7){
Print();
}
}
}
loadScanner.close();
}catch(Exception e){}
}
之后,它将它们放入我的主类中的变量中,如下所示:
public static void Print(){
System.out.println("Loaded File. These are the values: " + ldStr[0] + " " + ldStr[1] + " " + ldStr[2] + " " + ldStr[3] + " " + ldStr[4] + " " + ldStr[5] + " " + ldStr[6]);
Pane.numberOfChopers = ldStr[0];
if(ldStr[0] > 0){
Pane.boughtChoper = true;
Pane.isChoping = true;
}
Pane.numberOfHuts = ldStr[1];
if(ldStr[1] > 0){
Pane.boughtHut = true;
Pane.isRenting = true;
}
Pane.resMoney = ldStr[2];
Pane.resWood = ldStr[3];
Pane.hutCostMoney = ldStr[4];
Pane.hutCostWood = ldStr[5];
Pane.chopperPrice = ldStr[6];
Pane.numberOfPeople = ldStr[7];
}
无论如何,我只是想知道这是否有效。另外,如果我要创建一个登录系统,我该如何将这些值保存到他们的名称中(为每个玩家创建一个单独的文件就可以了)。
ps。我不打算使用 SQL
最佳答案
要为每个玩家创建一个单独的文件,我将使用以下命令:
import java.io.File;
import java.io.IOException;
public class Login {
static String playerName;
public static void main(String[] args) {
playerName = "John"; //set playerName to the name of the player
createFile();
}
public static void createFile() {
File f = new File("res/" + playerName + ".txt");
if (!f.exists())
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getSavePath(){
return "res/" + playerName + ".txt";
}
}
将“res/”
+playerName +“.txt”
替换为保存的文件夹(或路径)的名称
应创建文件。
关于java - 我需要有关创建保存系统的建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23976684/