Hey all. I'm building a game with Slick2d, and I wanted to make a platformer. To hold my levels right now, I make a class each level and type in what assets I need to initialize, then I make a new instance of that class. This doesn't feel very right, so I'd like to put the level data into .txt files that only hold certain things like the Map, (which is a different .class) and original player coordinates. I was wondering how I could parse these text files. Also, if this is bad programming, please tell me.
"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
Alix Ollivier wrote:Hey all. I'm building a game with Slick2d, and I wanted to make a platformer. To hold my levels right now, I make a class each level and type in what assets I need to initialize, then I make a new instance of that class. This doesn't feel very right, so I'd like to put the level data into .txt files that only hold certain things like the Map, (which is a different .class) and original player coordinates. I was wondering how I could parse these text files. Also, if this is bad programming, please tell me.
Well, text files are easy to update (which can also be dangerous), but an alternative might be to make it an enum, which keeps it wholly contained in Java. I presume these "levels" are something like a skill level, so you'll have various abilities or resources that you can use; so just make them attributes of your enum.
Isn't it funny how there's always time and money enough to do it WRONG?
Alix Ollivier wrote:Is enum like class or interface? I've never heard of that.
An enum is a class specifically designed for defining short lists of values that you access by name. Each value is unique in a JVM; but apart from that they are pretty much like any other class. They can have attributes (although these are usually final, and set at construction time) and methods, and they can implement interfaces.
Have you looked into serializing the data for the levels? I used this method to store different puzzles to be loaded into a puzzle board. It seems to me it could work as a basis for loading level data, as well.
The tutorial gives the needed information for how to parse. If you go up a section or two in the tutorial, you will also find info on how to parse Text files if you prefer to stick with them. One drawback cited for text files (for games) is that they can be inspected and altered, easily, by external means. I'm not sure how much more difficult it is to open serialized files, as I think they are just zips of Objects. (Not sure about this.)
I believe there are many implementations where XML or JSON data files are used for game data.
I prefer using text. I had a go at creating a platform game language so you could define each level with a bunch of text. Altering a level or defining a new level was then quite easy. My aim was to produce an "engine" that could run the platform language and then let others (hopefully) write their levels. All the levels could then be tied together into one big game. I had a grand idea about using feedback to score the screens so better screens would appear more often and obviously harder screens would appear later in the game. Here's an example of what I was using to define a screen:
It might give you some ideas on how to start defining a screen using text. The problem with using classes or enums to define a level is that you need to recode the program to add each new level. Also you'd be the only person who'd be able to do this. The language and screen generation all worked fine, everything moved as it should and the physics looked believable but I just couldn't get a realistic looking running and jumping man on screen so I stopped at that point
The above code produces this screen where the middle platforms move up and down. The idea is to get from one side to the other. On landing on the little blue triangle it would take you to the next screen.