Java 2D Tile Game

Started by
2 comments, last by chris_j_pook 18 years, 11 months ago
Hi, Im making a game similar to Zelda using tiles in Java... In my paint loop I have this ... public void paint(Graphics gOld) { if(backBuffer!=null) { Graphics g = backBuffer.getGraphics(); int x = 0; int y = 30; int count = 0; for(int a = 0; a <= NO_OF_TILES; a++) { g.drawImage(tileSprites[tileCode[a]], x, y, null); x+=50; count ++; if(count == 15) { count = 0; x = 0; y+= 43; } } ... Draw player sprite and enemies. ... g.dispose(); gOld.drawImage(backBuffer, 0, 0, null); gOld.dispose(); } } I have an array of BufferedImages (tileSprites) and an array of which tile should be displayed in which position of the screen (tileCode). However this code is running really slow when my character walks around it. The screen is 15x15 tiles big. Any ideas how I can improve performance here ? Cheers Chris
Advertisement
Have you tried profiling your code? Are you sure that the performance hit is in the section you describe? The code does not look too bad after all.

One thing you might try is this:

Let's suppose your character walks to the left, so you would have to

1.) move columns 0 to n-1 one column to the right
2.) draw a new first ("zeroth") column at the very left

right?

Instead of redrawing the whole grid, you might try "copying" the image you already have on the screen. Provided of course that copying the image into a buffer and then copying that buffer back onto the screen is faster than your approach.

hth,
Beren
This is most likely an issue with how you're dealing with things in Java (http://www.gamedev.net/community/forums/topic.asp?topic_id=324170). You might want to enable the 1.5 OpenGL pipeline for Java2D as this will probably solve your issue right off the bat.

You also might want to look into ditching Java2D completely and going full OpenGL via JOGL or LWJGL. This is just my opinion though.
Hi,

Thanks for the replys... I changed my images to JPGs instead of PNGs and its now working fine.

Cheers

This topic is closed to new replies.

Advertisement