Some game programming advice

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Tharwen
Posts: 34
Joined: Mon Nov 02, 2009 10:57 am UTC
Location: Elsewhere

Some game programming advice

Postby Tharwen » Thu May 26, 2011 9:48 pm UTC

'lo all.

I'm making a 2D game in Java. I have two things I need to decide on:

1. What format can you recommend for the object sprites? In other projects, I've used BufferedImages and they've worked perfectly fine, but I wonder if there's any particular reason to choose something else for displaying dozens of 30x30 (ish) sprites at varying scales.

2. I'd like a pathfinding algorithm. My game world is a large grid, which several NPCs will be pathing around in. Some of the squares will be filled in and therefore inaccessible, so they need to be able to find a fairly efficient route (it doesn't matter if it's a bit off) to their destinations (Think Dwarf Fortress if you want the basic idea). Any suggestions?

Thanks
[Insert witty signature about inserting a witty signature here here]

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Some game programming advice

Postby Xanthir » Thu May 26, 2011 11:00 pm UTC

For 2, is there anything wrong with the classic A*?
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Tharwen
Posts: 34
Joined: Mon Nov 02, 2009 10:57 am UTC
Location: Elsewhere

Re: Some game programming advice

Postby Tharwen » Thu May 26, 2011 11:06 pm UTC

Xanthir wrote:For 2, is there anything wrong with the classic A*?

I have no idea. I will look it up.

Wish me luck!
[Insert witty signature about inserting a witty signature here here]

themandotcom
Posts: 64
Joined: Tue Jun 05, 2007 10:50 pm UTC
Contact:

Re: Some game programming advice

Postby themandotcom » Fri May 27, 2011 2:45 am UTC

A*, or less generally Dijkstra's Algorithm, is the "best" way to find the minimum path when there are no non-negative weights.
e^pi*i=WHAT??

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Some game programming advice

Postby phlip » Fri May 27, 2011 7:49 am UTC

themandotcom wrote:when there are no non-negative weights.

I think you have either one too many or one too few negatives, there.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Tharwen
Posts: 34
Joined: Mon Nov 02, 2009 10:57 am UTC
Location: Elsewhere

Re: Some game programming advice

Postby Tharwen » Fri May 27, 2011 1:20 pm UTC

Many thanks. I did some reading about it last night, and now I think I've got it. I'm familiar with Dijkstra's algorithm already though.

Any word on the image formats, or does it not really matter?
[Insert witty signature about inserting a witty signature here here]

User avatar
Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Some game programming advice

Postby Xanthir » Fri May 27, 2011 8:37 pm UTC

Tharwen wrote:Many thanks. I did some reading about it last night, and now I think I've got it. I'm familiar with Dijkstra's algorithm already though.

Yup, they're very nearly the same thing. Dijkstra's is just pathfinding with a breadth-first-search, so the important data-structure is a queue (new nodes get put at the end of the search list). A* is identical, except you add an estimate of the distance left (the estimate must always underestimate, or else you can sometimes get bad paths) and use a priority queue sorted by total estimated distance (distance so far + estimate) instead of a normal queue.

Any word on the image formats, or does it not really matter?

No clue, but I doubt it matters that much. Sprites tend to be friendly with GIFs or PNGs.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Some game programming advice

Postby Steax » Sat May 28, 2011 12:49 pm UTC

If you intend to draw images at multiple sizes a lot, you may want to just make more files for each size (or cache them). Otherwise I don't think you can go wrong with plain old images.
In Minecraft, I use the username Rirez.

User avatar
BotoBoto
Posts: 191
Joined: Mon Mar 09, 2009 9:31 pm UTC
Contact:

Re: Some game programming advice

Postby BotoBoto » Sat May 28, 2011 3:33 pm UTC

@steax, maybe the .ico format is suitable? As it has all sizes in the file ready.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Some game programming advice

Postby Steax » Sat May 28, 2011 4:11 pm UTC

I'm not so familiar with how the .ico format works, but I'm not so fond of it. I think the best option would be SVGs, solves all those problems at once, but that's not always possible, depending on how the original art was made.
In Minecraft, I use the username Rirez.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Some game programming advice

Postby Berengal » Sat May 28, 2011 5:04 pm UTC

The stored format isn't particularly important as long as you can read it. PNG, GIF and BMP are all widely used for sprites (though BMP lacks alpha. Usually 0xFF00FF is translated to fully transparent when using BMP, so you might have to do that after you've loaded the image).

BufferedImage works just fine after it's loaded, although you might want to wrap it in some other class if you want to provide sprite-map functionality. If you're using the java.awt classes (the ones in the standard library) for drawing then scaling should be basically free (just apply the scale transformation before you start drawing), although it might not look too pretty unless you're scaling by 2 or 4 (8 can work too, but at that point the pixels start to become too big and in-your-face in my opinion). For other scales it's best to upscale beforehand and treat the different scaled versions as different images. Another possibility is to use an upscaling algorithm like hq4x, although most of these are restricted to powers-of-two scaling as well.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Tharwen
Posts: 34
Joined: Mon Nov 02, 2009 10:57 am UTC
Location: Elsewhere

Re: Some game programming advice

Postby Tharwen » Sun May 29, 2011 4:41 pm UTC

Berengal wrote:The stored format isn't particularly important as long as you can read it. PNG, GIF and BMP are all widely used for sprites (though BMP lacks alpha. Usually 0xFF00FF is translated to fully transparent when using BMP, so you might have to do that after you've loaded the image).

BufferedImage works just fine after it's loaded, although you might want to wrap it in some other class if you want to provide sprite-map functionality. If you're using the java.awt classes (the ones in the standard library) for drawing then scaling should be basically free (just apply the scale transformation before you start drawing), although it might not look too pretty unless you're scaling by 2 or 4 (8 can work too, but at that point the pixels start to become too big and in-your-face in my opinion). For other scales it's best to upscale beforehand and treat the different scaled versions as different images. Another possibility is to use an upscaling algorithm like hq4x, although most of these are restricted to powers-of-two scaling as well.

Thanks for responding (thanks to everyone else too).

I'm only planning on scaling down (e.g. zooming out to look at the entire level), and letting the player decide how far to zoom. Would that cause any issues?
[Insert witty signature about inserting a witty signature here here]

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Some game programming advice

Postby sourmìlk » Mon May 30, 2011 7:37 am UTC

GIMP allows you to export BMP images with alpha using 32 bit pixel depth (last 8 bits are alpha).
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.


Return to “Coding”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests