3
\$\begingroup\$

Suppose I wanted to code a game grid in java. Not an abstract text one but an actual grid with graphics and 'cells'. Would I have to draw line by line or is there a more efficient way? Also how would I go moving units (objects) around the grid, moving them and repainting their positions?

  public static int [] [] map = {{2,0,1,0,0}, // e.g. 2 is base
                                   {1,0,0,0,0},
                                   {0,0,0,0,0},  //1 is unit
                                   {0,0,0,0,0},
                                   {0,0,0,0,2}};

I still wonder if there is a resemblance between 2D arrays and actual game grids as I can just paint coordinates and lines. However I feel there is a better way and want to implement a structure like above (the code) to emulate a map like this:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ This is a bit vague, what is the purpose of your game grid? Do you want to create a tiled map where each cell of the grid is bound to a tile? Anyway, 2D arrays (or 1D arrays actually) seems the perfect data structure to implement grids in general since you can easily access cells using his coordinates. Regarding moving entities on top of the grid, you can decide to make them move cell by cell or let them move freely around the grid, it depends of what you want to do. \$\endgroup\$ – nathan Aug 20 '13 at 16:46
  • \$\begingroup\$ I want each cell to represent a tile, where a unit moves to on his turn. Consider it like the Civilization game. \$\endgroup\$ – BDillan Aug 20 '13 at 17:55
  • \$\begingroup\$ Ok I have an idea of making a 2D array of tiles compromised of Image Sprites. I still don't know if this is plausible. \$\endgroup\$ – BDillan Aug 20 '13 at 18:24
  • \$\begingroup\$ Do you know libgdx? \$\endgroup\$ – Wolfrevo Kcats Aug 20 '13 at 18:48
  • \$\begingroup\$ Are you asking about rendering tiles? It looks like you've already figured out a way to represent the grid itself in code. There are a hobajillion articles online about tiles and sprites, Java-specific even, did you try searching for any? \$\endgroup\$ – Sean Middleditch Aug 20 '13 at 18:52
2
\$\begingroup\$

You should read about tiled maps, tiles and tilesets.

Your tiled map is a grid. It's simply a 2D array where each element of the array is a tile. Using a 2D array in this case seems perfect since you can easily access a tile at a given position by doing mapArray[x][y].

Now you need to define a way to represent the tiles of your map. Here come the concept of tileset. A tileset is basically an image containing a collection of tiles, here is an example of a random tileset i picked on the internet:

enter image description here

Since you know that the tiles of your map will always have the same dimensions (16x16, 32x32 etc.), you can easily extract the tiles from the tileset and assign an id to each of them. The first tile in the tileset could have the id 0, the second one, the id 1 and so on. Then, why not do something like tileset[id] to get actual tile's image?

This way, your map could be a 2D array of integers representing tile's id. When creating your map, you will need to link your map array with the tileset(s) used to render it.

If you decide to use frameworks such as Slick2D or LibGDX, they often have build in facilities to manipulate tiled maps.

Plus, some tiled map editors exists:

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

A way you could do it is an array of images which would most likely require a library. I'm a C++ coder but say the library has an object called an Image. You could make an array of this a new Image object and set specific values of that array to different images. Like...

Image map[] = {{"something.png"},{"another.png"}} ...

And then maybe you could make another normal int array to actually set-up the map layout while using values of the int array to apply to the values of the Image array but you couldn't set them equal to each other since they're different types.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.