开发者

how to display the highest score in a game

I am creating a game called Jumbled Words in which the user is given a word in a scrambled format for which the user has to unscramble it. For each correct answer the score gets incremented by 10 and the user is shown the current score. After the user exits the application the current score should get stored somewhere, say a file or a database (assuming the game is played for the first time) and display it as the highest score. Suppose for the first time the game is played and the score is 100 and the value is stored in the file/DB, for the second time when the user plays the game and the score is 200. By assuming 200 is the highest score compared to the highest score in the file or DB i.e. 100, the value 100 must be replaced by 200 and display 200 as highest score. Whenever the user wants to see the highest score, it should retrieve the highest score and display it in a view.

I want the following suggestions from my folks:

开发者_如何学C
  1. What type of data storage system must I use? A file, database or a properties file. Which would be better.

  2. What logic must I apply to display the highest score?

  3. What APIs of Android must I use for the File, DB or Properties?


I would recommed the SharedPreferences class & api. It's simple and requires minimal code, making it great for storing small amounts of data in key-value pairs.

It's described on this page (along with other data storage methods): http://developer.android.com/guide/topics/data/data-storage.html#pref

Here's some of the example code, modified to look something like how you could use it:

//Some strings to address your preferences
public static final String PREFS_NAME = "JumbleWordsPreferences";
public static final String HIGH_SCORE = "HighScore";

//Retrieving high score
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int highscore = settings.getInteger(HIGH_SCORE, 0);

//Saving current score as high score
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInteger(HIGH_SCORE, currentScore);
// Commit the edits!
editor.commit();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜