1
\$\begingroup\$

I would like to create a 2d game in Java, but tutorials online for game libraries such as LWJGL are usually filled with errors once I compile the project. Most people do not explain very well, and just go on very quickly. Can someone please provide me a couple examples with source code, and somewhere to have a starting point? I used to have trouble with making a GUI and you guys helped me through, hopefully you can do the same again!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Unfortunately this question is considered off topic on this site (It is very, very broad). You are welcome to discuss it in the chatroom though (Once you've hit 20 rep for the general room at least) \$\endgroup\$ Jul 18, 2012 at 22:43

3 Answers 3

4
\$\begingroup\$

A very simple and effective method to write small software rendered games is to use the methods and tools swing offers. Begin a new Swing project and add a single component to the window. That is the component everything is being rendered into. It might look like this:

public class SwingRenderTarget extends JComponent {
    private final BufferedImage image;

    ...

    @Override
    public void paint() {
        swapBuffers();
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, canvasWidth, canvasHeight);

        g.drawImage(image, 0, 0, canvasWidth, canvasHeight, null);
    }

    private void swapBuffers() {
        System.arraycopy(currentPixels, 0, ((DataBufferInt)image.getRaster().getDataBuffer()).getData(), 0, gameSizeX*gameSizeY);
    }
}

What it does is rendering a single BufferedImage to the screen that contains all the game data. But how to draw images to the BufferedImage? This is simple! Create an array of ints (called currentPixels in this example) and just treat every int as a single pixel of your screen! In the method swapBuffers this array of pixels is written to the previously created BufferedImage which is then rendered to screen.

How about an example how to render a simple Sprite to the screen? First of all create a Sprite class:

public interface Sprite {

    int[] getPixels();

    int getHeight();

    int getWidth();
}

The int array will serve as the pixels of the Sprite. You can fill the array programmatically or just load any bitmap from disk. Then when it's time to draw that Sprite just copy the pixels from the Sprite over to our pixel array in the SwingRenderTarget. Here is an example to fill the whole screen. To use smaller Sprites one has to copy one row from the Sprite pixel array to the SwingRenderTarget pixel array at a time:

@Override
public void drawFullscreenBitmap(Sprite sprite) {
    int[] data = sprite.getPixels();

    System.arraycopy(data, 0, currentPixels, 0, data.length);
}

Throw in a game loop that has a fixed timestep and there you go: a basic game in no time!

\$\endgroup\$
1
\$\begingroup\$

I would suggest using Slick 2d (Slick is built on top of the lwjgl, so you have a great community you can turn to with problems). I just did a little game with it and it worked just fine. It's really easy to grasp and provides all the groundwork, e. g., you don't have to worry about buffers and such. Just define your graphics (images) and draw them. Furthermore, Slick provides states so you can really easy develop the single parts of your game indepent from one another (main menu, options, gameplay and so on).

The states themselves provide you with two alternating methods, update() and render(), so you can divide your code effectively:

public void update(){
     // your gamelogic, such as collision-detection, controls, etc.
     // simple example for handling input:
     Input in = getInput();
     if(in.isKeyPressed(Input.KEY_DOWN)){
          player.move("down");
     }
     player.checkCollision();
}
public void render(){
     // draw your assets
     drawUI();
     drawEnvironment();
     player.draw();
}

Slick also handles all input, it even has support for controllers. On the page, there are several good tutorials for beginners, explaining how a simple game could look.

\$\endgroup\$
0
\$\begingroup\$

I think starting with an existing simple game, then adding to it or changing it is a good route to take. So, find a 2D opensource game to start with as a base. Perhaps you can find one that suits you on one of the game jam sites like ludum dare? I know they usually post their code for people to look at when they're done. This also makes sure that everything is included so you don't have issues with all those compile errors you were talking about.

\$\endgroup\$
2
  • \$\begingroup\$ Ludum Dare is a great source for source code to look at, but beware: since the games are written within 48 or 72 hours a lot of the code is not pretty and maybe not the best start for a beginner in the topic. \$\endgroup\$
    – thalador
    Jul 18, 2012 at 20:25
  • \$\begingroup\$ Thanks thalador. Good to know! I was able to learn a few things and get some basic "boiler plate" type code from there. Since people often post their "this is what I have to start with" code, which is usually everything you need to get started. \$\endgroup\$
    – GameDev-er
    Jul 18, 2012 at 20:29

Not the answer you're looking for? Browse other questions tagged .