Making a 2D grid in Slick2d

I’m trying to make a 2D grid to move on. It’s going to be 10x10 and that will not change. When a player clicks a square in the grid, the player gets moved to that square. How can i accomplish this?

I currently have a working board, where the player can just move to any coordinates he wants. There is no squares to stick to :slight_smile:

Just round the coordinates up to your grid size:


int x = xpos - (xpos % gridsize);
int y = ypos - (ypos % gridsize);

Thanks for your response.

This did the trick


        Felt [][] board = new Felt[16][12];        

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {
                int x = i;
                int y = j;
                board[i][j] = new Felt(x, y);
            }
        }

Here is how I would optimize that code. I am not the most experienced so take my feedback lightly.

 // Assuming the board dimensions will never change during the game.
private final byte boardWidth = 16;
private final byte boardHeight = 12;
      
private Felt[][] board = new Felt[boardWidth][boardHeight]; 
      
for (int x = 0; x < boardWidth ; x++) {
    for (int y = 0; y < boardHeight ; y++) {
        board[x][y] = new Felt(x, y);
    }
}

EDIT: Are you using standard Java2D, perhaps Slick2D or?