Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make levels in Java

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is enum like class or interface? I've never heard of that.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic