• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make levels in Java

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is enum like class or interface? I've never heard of that.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 287
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic