如何使用Processing保存游戏进度

时间:2014-10-15 21:32:52

标签: processing

我正在通过Processing制作一个小游戏,我想知道如何让游戏能够保存游戏进度。我搜索了主题并发现了诸如saveString()和序列化之类的内容,但我并没有真正理解它,也不了解如何使用它。 基本上我需要一个功能,当玩家点击保存时,将保存当前状态的所有当前变量以及整个程序。他们以后可以重新打开数据并从他们中断的地方继续游戏。 想知道你们是否知道任何教程,或者可能会给我一些建议。

2 个答案:

答案 0 :(得分:1)

除了loadStrings(),saveStrings()之外,您还可以考虑使用JSON object和类似的功能,例如saveJSONObject()loadJSONObject()。使用字符串的优点是它可以简化序列化基本类型和嵌套结构。

这是一个非常基本的虚构例子:

{
    "level":1,
    "difficulty":0,
    "checkpoint":3,
    "paused":true,
    "items":[0,1,2]
}

这是你如何加载/解析它,然后在Processing中修改并保存它(如果上面保存为state.json):

JSONObject state;

void setup(){
  size(200,200);
  //load JSON
  state = loadJSONObject("state.json");
}
void draw(){
  background(0);
  text(state.toString(),10,15);
}
void keyPressed(){
  if(key == 'd'){
    //access JSON (using get methods) and modify (using set methods)
    state.setInt("difficulty",state.getInt("difficulty") + 1);
  }
  //save JSON
  saveJSONObject(state,"state.json");
}

请注意,在示例中,使用getInt()setInt()获取和设置整数。请务必查看参考和示例。布尔值/字符串/浮点数以及嵌套的JSON对象和数组都有类似的方法。

答案 1 :(得分:0)

您需要使用saveStrings()loadStrings()功能。首先,您需要在数据文件夹中创建一个文本文件(.txt)。比,你可以使用这样的东西,

//Saving

String words = "apple bear cat dog";
String[] list = split(words, ' ');//This creates a list with apple, bear, cat, and dog

// Writes the strings to a file, each on a separate line

saveStrings("nouns.txt", list);//This copies everything from the list named 'list' and pastes it onto the file 'nouns.txt'
//Calling upon the save
String lines[] = loadStrings("nouns.txt");]//This creates a list that has everything on the file 'nouns.txt'

//doing stuff with the save

println("there are " + lines.length + " lines");
for (int i = 0 ; i < lines.length; i++) {
   println(lines[i]);//This is self explanatory very simple stuff
}
相关问题