0
\$\begingroup\$

When I start my game, it displays a level menu, and When I select a level, it's gonna create a game corresponding to the level and I can play. Now I'd like to store the progress of each user, implementing something like this but I don't know where to start.

enter image description here

public class Game {

    private Level level;
    // Each level are stored in a txt file,
    // the class Level read and load the file.

    public Game(Level level) {
        this.level = level;
    }

// Other methods.. such as Undo, restart etc...

}

I have created a login system in a class called User where it stores the data in a map.

And I need a class that link them so I created a new class but I'm not sure what kind of method it should contain :

public class MoveTheBlock{

    private User currentUser;

    public MoveTheBlock() {

    }

}
\$\endgroup\$
  • 2
    \$\begingroup\$ You code examples don't tel us much about how you're trying to handle saving. How have you tried reading / writing your save file so far? \$\endgroup\$ – DMGregory Apr 5 at 10:28
2
\$\begingroup\$

It seems to me that your problem is in two "layers".

Layer one is just coding the solution: I'm not sure what "move the block" class is supposed to do, I just assume is the main class game? If that's the case, it should have methods for handling input. For example: MovePlayer, CollectPickup, MoveBlock. Also methods that relate to the general gameplay, like checking if the player has scored somehow, or did the game end due to time being finished?

If MoveTheBlock is supposed to be the save game file, it should have at least a method to save the game, and to load the game. Save would take all the save'able info from the game, like player score, position, maybe position of items if they are movable? And then, put it into a single object and store it into a file. If you want to make that easier, you might want to look up "serialization".

With that being said, the best way to handle it is to do the tasks one by one, and look for solutions to each problem.

The second layer of your issue is the whole project organization. That is something that comes with practice - try to think in terms of object oriented programming. People often compare code to cars, and it's a good idea. A car has many parts, and every single part has one responsibility. The engine, the tires and steering wheel - and they only "connect" where and when it's needed.

You should try to organize your program in the same way. You might have a player. A block. A timer. Ask yourself what are they supposed to do, and what other elements they have to communicate with.

Something that helps me with that, is thinking of the core gameplay loop. Always try to think of making a working game first, and then start worrying about other things, like saving the game.

I'm sorry for blabbering - I tried to keep my answer short. It would really help us if you provided more details about your game :)

\$\endgroup\$
0
\$\begingroup\$

The way I have handled this is to have two classes: one for saving and loading (call it Save); one for just holding the information (Object). Save needs two methods: one to load previously saved information by reading Object; one to save information by writing to Object. The load method is triggered when a user signs in. The information from Object is loaded and the level screen is displayed. The save method is called when a level is completed or when settings are changed.

Object is just a serializable two-dimensional array. It holds user names, levels they have completed, scores for those levels and preferences, such as sound enabled and music enabled.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.