How to draw a 4x4 grid using square sprites using libgdx?

705 Views Asked by At

Hi want to develop a game puzzle game similar to flow free. However i want to use square sprites to create the grid can anyone show me how to position the square sprites to make it look similar to 4 x 4 grid. The square sprites are 100 x 100.

1

There are 1 best solutions below

3
On

You have to look into Tilemaps. If you have a single square sprite with it's borders a different color we could tile this next to each other to create a grid.

We can draw your 100x100 tiles next to each other in a 4x4 grid by using a double loop.

    tileWidth = 100;
    tileHeight = 100;
    gridWidth = 4;
    gridHeight = 4;

    for (int y = 0; y < gridHeight; y++)
    {
        for (int x = 0; x < gridWidth; x++)
        {
            //Draw function (sprite, position X, position Y)
            draw(tileTexture, x * tileWidth, y * tileHeight);
        }
    }