libGDX save and load game data

So you want to load and save some data in your game, a good way is via a JSON file.

In your main class:

	public void create () {
		batch = new SpriteBatch();
		setScreen(new IntroScene(this));

Set up a GameData class:

public class GameData {

  private int highscore;
    private boolean musicOn;

  public int getHighscore() {
    return highscore;

  public void setHighscore(int highscore) {
    this.highscore = highscore;

  public boolean isMusicOn() {
    return musicOn;

  public void setMusicOn(boolean musicOn) {
    this.musicOn = musicOn;

And set up a GameManager class:

  private static GameManager ourInstance = new GameManager();

  public GameData gameData;
  private Json json = new Json();
  private FileHandle fileHandle = Gdx.files.local("bin/GameData.json");
  private GameManager() {}

  public void initializeGameData() {
    if (!fileHandle.exists()) {
      gameData = new GameData();


    } else {

  public void saveData() {
    if (gameData != null) {

  public void loadData() {
    gameData = json.fromJson(GameData.class,
    public static GameManager getInstance() {
    return ourInstance;

Game data will be loaded if the is JSON file in “bin/” else a file will be created. The file will be encoded so it will be hard to manipulate by a user.

Building a game in Java

Just started working a game in Java and libGDX for a course @ LiU.

Game Idea

”My Nightmare” is a side-scrolling game where the object is to get to the end of the level, where the user will be introduced to a new level. You can move right/left and also jump/duck. Each level has a number of monsters, the monsters; stay still when you’re facing them but move when you look in another direction. You can not kill the monsters, so the goal of the game is to reach the end of each level without getting caught by monsters or trapped on a level.

From left the screen will fall into darkness. This makes it impossible to go back in the game, and you must also progress to the right before the darkness catches up with the player. When the darkness hits you its game over. 
The USP is the game graphics which are heavily inspired by South Park and animated like in that show. Colors used in the game are black, white, gray, orange and red. Initially I aim for a playable demo with illustrational art and graphics, but my aim is to present a game with the original artwork as its part of the gameplay is essential. 

Inspirational games:
Super Mario Bros

Castlevania Mega Man Little Nightmares 
 Game Tech The game will be developed in Java and libGDX which will be a good fit for the 2D platform game that i aim for, together with the IDE IntelliJ and some graphics editing in Gimp I hope that I have all the tools I need for the project. Graphics for the game will be done via traditional analog animation technics.

Progression in the Game

With the complexity with the monsters not moving when you look at them, I see a lot of puzzle type of levels. To help the player an extra feature in the game will be available, a flashlight with bad batteries which can not be used unlimited times. When monsters are hit by the light they will be destroyed. Time will ”charge” the batteries.

In-game tutoring approach

The first levels will introduce the player to the different aspects of the game. On the first level the user will see that the monsters don’t move when you are looking at them, but start moving when you move. The flashlight will be introduced after the first levels when the user has learned how to jump and duck and knows how the monsters move.

Source code

Game full source are avelibal at github.

Working with options in c++

Im currently working with a small application and needed to handle commandline options, and was looking for a pritty quick and dirty way to handle this.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Modcom {
  Modcom(int &argc, char **argv);
  const string& get_command(const string &option);
  bool cmd_options_exists(const string &option);
  vector<string> options;

Modcom::Modcom(int &argc, char **argv){
  for (int i=1; i < argc; ++i)

const string& Modcom::get_command(const string &option) {
  vector<string>::const_iterator it;
  it = find(options.begin(), options.end(), option);
  if (it != options.end() && ++it != options.end()) {
    return *it;
  return "";

bool Modcom::cmd_options_exists(const string &option) {
  return find(options.begin(), options.end(), option)
                     != options.end();

int main(int argc, char **argv){
  Modcom modcom(argc, argv);
  if (modcom.cmd_options_exists("--help")) {
    cout << "do something cool with this function" << endl;

a simple calculator in c++

A simple calculator in c++. The result must be initialized with 0 to silent the -Werror switch in the compiler. When we go into the switch state this will be uninititalized when we hit ‘default’.

  #include <iostream>
  using namespace std;

  class Solution{
    void a_simple_calculator() {
      float a, b, result{0};
      char op;
      cout << "help: 10+10 or 20-10 or 5*5" << endl;
      cin >> a >> op >> b;
      switch (op)
        case '+':
                result = a + b;
        case '-':
                result = a - b;
        case '*':
                result = a * b;
        case '/':
                result = a / b;
                cout << "Not possible to compute!" << endl;
      cout << result << endl;

  int main() {
    Solution sol;

Older Posts

Solving the fizzbuzz problem in c++ 12 Aug 2016