Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store levels in an Android game?

Tags:

android

This isn't so much a technical question as it is a style question. I'm making a generalization of peg solitaire for Android, and there's going to be different levels you can play. Each of them will have different starting configurations for the board, different sets of moves you can do, etc. Right now I have it programmed with a single basic level, and all those variables are initialized that way originally.

Do I add a text file that has all the different kinds of levels, and program it to read it from the text file? Do I add a Level class with all of this information from each level, and make several Level objects when it initializes? Do I make a Level class, but initialize it by reading the levels from a text file?

I'm sure all the methods would work, but is there a standard way to do it? I've never programmed anything like that before.

like image 881
DanielLC Avatar asked Aug 30 '12 23:08

DanielLC


2 Answers

There really isn't a standard way to do what you want, it really depends on the complexity of the data you need to store to do each level initialization. Is this data going to change or is it static? Does it make sense to have it properly organized and separated from code logic? These are questions you need to answer yourself cause it's your game and we don't know all the details.

Given that, you decide if you should store that data in an XML file, TXT file or something else. There really isn't a correct answer. But you should (IMO) always have some sort of Level class, that's the right way to go in a OOP context. The constructor could have the necessary parameters to build the level from the XML/TXT/whatever data. It could also have all the level parameters so you could initialize everything by code, if you need to for some reason. And, of course, don't forget the getter/setter methods for all those level attributes.

One thing I think you shouldn't do is to "make several Level objects when it initializes". You should separate your game logic from the data, you shouldn't mix both that way.

Probably not the answer you were looking for, but it's mostly up to you.

like image 54
rfgamaral Avatar answered Nov 15 '22 00:11

rfgamaral


The way my friends and I are doing it is storing the level attributes in a database then depending on which level the user selects, we read the database and load that into our Level class which creates the level for the user the play!

Edit

We're beginners as well, but it's been very easy to make new levels after the first one has been made. =] Especially since all we have to do is add a row to our database and fill in the values.

like image 40
corgichu Avatar answered Nov 14 '22 23:11

corgichu