Using Maps/Dictionaries to Score Game Level Scores
One of the features that I need to include in my game is the ability to award the player stars for each level, depending on how many bounces it took to hit all the goals. Each level has a target number of bounces, and players get three stars if they beat the level within that range, two stars if they slightly exceed the range (based on some simple math), and one star otherwise. Uncompleted levels have zero stars.
This information should be stored somewhere, so that when players return to the level select screen, either from a level or after reloading the game, the stars are shown for each level.
One way to accomplish this task might be to a simple text format, or maybe something like XML or JSON. There's also another way. Remember in my last devlog (https://cloudyheavengames.itch.io/brain-bouncer/devlog/187497/converting-and-loading-levels-from-gamemaker-studio-with-json) where we discussed key-value pairs and maps in GameMaker Studio? C# has a key-value pair storage structure called a dictionary, which can be used to store the data.
For this task, I set the keys in the dictionary to be the level number, and the value is the number of earned stars. It's easy and quick to look up the key-value pairs by the level number, and to make updates to the number of stars. For example, suppose that I completed a level and earned only one star. Then later, I go back and find a better solution, and earn three stars. By quickly retrieving and checking the number of stars currently stored in the dictionary, I can update the stored value if it's better than my previous star count.
High Score Dictionary Example
Take a look at the code sample I posted at https://codepad.co/snippet/high-score-with-dictionary-example. I demonstrate how you can use a dictionary to save high scores for a level. The comments in the code explain how it works.
There are a few methods and features that we use in the example to deal with our map. You'll notice that at the very top of the ScoreData class, we create a new Dictionary with this line of code:
public Dictionary<int, int> highScores = new Dictionary<int, int>();
In the angular brackets, we see <int, int>. We are specifying that for this dictionary object, the key should be of type int, and the value should be of type int as well. You can use whichever data types you need for your project.
To add a new key-value pair to the dictionary, I used the Add() method, like this:
highScores.Add(level, score);
If there is already a score for the level stored, than we don't want to use Add() because that will give us an error. Instead, we can use bracket notation to access and change the value for that level (similar to accessing elements in an array):
highScores[level] = score;
We can use the ContainsKey() method to check if a level already has an entry in the dictionary:
if(highScores.ContainsKey(level))
Then, if we want to retrieve the score for a level, we can again check if the level has a score stored, using ContainsKey(). If there is a stored score, use the bracket notation again to get the score:
if(highScores.ContainsKey(level)) {
return highScores[level];
}
This was the basic idea of how I used a dictionary structure to store level score data. In the next dev-log entry, I will discuss how I saved this to an external file so that the data can be stored even after the user quits the game, and reloaded once the player comes back to the game.
Get Brain Bouncer
Brain Bouncer
A bouncy puzzle game that will have you considering all angles.
Status | In development |
Author | cloudyheavengames |
Genre | Puzzle |
Tags | 2D, Physics |
Languages | English |
More posts
- Saving the Level Data and Reloading from a FileNov 16, 2020
- Converting and Loading Levels from GameMaker Studio with JSONOct 23, 2020
- Adding and Organizing Content in MonoGameApr 11, 2020
- Adding Source Control in Visual StudioApr 03, 2020
- Getting Started with the MonoGame Project: Setting Up a Shared ProjectMar 29, 2020
- Migrating from GameMaker Studio to MonoGameSep 17, 2019
- New features, and available on Google Play!Nov 23, 2017
Leave a comment
Log in with itch.io to leave a comment.