Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 18th, 2005, 11:55 PM   #1
InflamedSpirit
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 InflamedSpirit is on a distinguished road
Question Sprite-Based Games

I have a few questions regarding some good ways to use sprites. Has anyone ever played the game Littlefighter2? (its written in C++ i think) It has these text files with something along the following.

Frame 1 pic:1 goto:2 wait:3 dx:0 dy:0 ifkeya:4 ifkeyb:10 ifkeyc:42
Frame 2 pic:2 goto:3 wait:3 dx:0 dy:0 ifkeya:5 ifkeyb:10 ifkeyc:20
Frame 3 pic:3 goto:1 wait:3 dx:0 dy:0 ifkeya:5 ifkeyb:10 ifkeyc:20

...
...this continues for hundreds of frames...
...
Note that these are noncompiled files that can just be edited in a text program and obviously the program reads these lines and interperates them somehow.


My question is what would be a good way to have a setup like above (in java)?
Would it be better to have text files like this or have it all in the program?
And how would I go around doing this or structure it (what would the classes/functions look like)?


I would really appriciate any answer, thanks in advance.
InflamedSpirit is offline   Reply With Quote
Old Feb 19th, 2005, 2:29 AM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
your program definatly can extract the info from a text file like this (check out class StreamTokenizer)
Text files are inefficient, however, and i dont particularly like how just anyone can go and mess around with them. Another possibility would be to create a class to store the information, save the data as objects to a file, and load it at start.

Your class would probably look somthing like:

public class SpriteData implements Serializable {
     private int pic, goto, wait, dx, dy, ifkeya, ifkeyb, ifkeyc;
     //etc, etc, etc
}

And then when you start the program you could load all of these objects onto a Vector or somthing, for easy access.
(For writing/reading objects to a file, check out class ObjectInputStream and ObjectOutputStream)

Last edited by ZenMasterJG; Feb 19th, 2005 at 2:30 AM. Reason: Screwed up my code tags. Damn you nyquil.
ZenMasterJG is offline   Reply With Quote
Old Feb 19th, 2005, 10: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 10:21 AM.
Mjordan2nd is offline   Reply With Quote
Old Feb 19th, 2005, 11:51 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I suggest using a text file (call it a database - it sounds better) and encrypting the contents. Enjoy.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC