aspose file tools*
The moose likes Game Development and the fly likes How to make levels in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "How to make levels in Java" Watch "How to make levels in Java" New topic
Author

How to make levels in Java

Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

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.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

Is enum like class or interface? I've never heard of that.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

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.

I suggest you read up on them here.

BTW, there's no guarantee that an enum is what you want; but if this data is relatively static, it's probably the way I'd go first.

Winston
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

Oh. That may be useful. Actually, in response to your above post, I meant levels as in levels in mario, with level 1-1, 1-2, etc. etc. Thanks anyways.
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
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.

http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html

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.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
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

Mike



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.


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make levels in Java
 
Similar Threads
java.util.logging
Best way to store levels
java.util.logging
public, protected, package
recursion and retaining variable values