View Single Post
Old Feb 19th, 2005, 11:10 AM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Heh. Funny you should ask. I'm making a Mario game right now, where everything (items, objects, backgrounds, enemies) except Mario himself is being read from a text file. I honestly believe it works pretty damned well, and it's probably far more organized, and changable than putting everything in your code.

I'll give you a little description of the way I have it set up, in case it helps you out any. First of all, I have a canvas class, where I'm drawing everything. Then I have seperate classes for everything, such as the ground, enemies, bricks, background objects, Mario and so on, you can create them as the need arises. I also created an interface with all the methods I possibly thought I might need, and had all of my classes aside from Mario implement it. I used an ArrayList in my canvas class to hold any objects I might need to (such as the ground, enemies, background object, and so on). Now, as Mario moved along to the right, I used an instance of Scanner (new to 1.5) to read my text file. As it read, it added it to the ArrayList. Here's what my text file might look like:

  (this is level1.lvl)
  
  
  
  Ground 100 520 40 40 HARD NOT_ACTING ground.gif

And then, from my code, I basically read it like this:

  // this is way above in my code
  ArrayList <Elements> elements = new ArrayList();
  Scanner levelReader = new Scanner(new File("level1.lvl"));
  // now we skip a lot of lines
  .
  .
  .
  .
  .
  .
  .
  //this is a little segment of how I might read from the file and then initialize it
  
  String [] tempData = levelReader.nextLine().split(" ");
  if(tempData[0].equals("Ground")
  {
  	 int xPos = Integer.parseInt(tempData[1]);
  	 int yPos = Integer.parseInt(tempData[2]);
  	 int width = Integer.parseInt(tempData[3]);
  	 int height = Integer.parseInt(tempData[4]);
  	 boolean hard = NOT_HARD; // HARD and NOT_HARD are predefined constants;
  	 if(tempData[5].equals("HARD"))
  	 {
  		  hard = HARD;
  	 }
  	 boolean acting = NOT_ACTING; // again, predefined constants;
  	 if(tempData[6].equals("ACTING"))
  	 {
  		  acting=ACTING;
  	 }
  	 String imgName = tempData[7];
  	 elements.add(new Ground(xPos, yPos, width, height, hard, acting, imgName));
  }
  
  // Check for other objects, such as enemies, blocks, stuff like that
  .
  .
  .
  .
  .
  .
  .
  .
  for(int i = 0; i<elements.size(); i++)
  {
  	 // draw them onto the canvas
  }

That's basically how I read from the text file. Of course, I don't parse everything into its own variable usually, I just do it in the constructor call itself, but I was hoping that the variable names would show you what each of those numbers meant. Hard just means if Mario intersects it, whether it should stop, or it should just keep going through it. Acting is for enemies, or blocks that break. They'll have an act method. Ground of course, doesn't act, therefore it is NOT_ACTING. Yea, but it's pretty easy to read from the text file, and pretty effective, in my opinion.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower

Last edited by Mjordan2nd; Feb 19th, 2005 at 11:21 AM.
Mjordan2nd is offline   Reply With Quote