Question

I am making a scrolling game in Java , I would like a clarification on one point. I do not save the game level in any structure java , I just read a file ( . gif ) which I modified in a way that :

  • I use the color decryption to parse through every pixel to pixel and place where the
    object meets the requirements that I have established .

for example:

  .
  .
  .


int w = image.getWidth();   //store the dimensions of the level image. 
int h = image.getHeight();
for(int x = 0; x < w; x++){
        for(int y = 0; y < h; y++){  //check every single pixel with this nested loop
            int pixel = image.getRGB(x, y);   //get the pixel's rgb value
TYPE_INT_ARGB formatint red = (pixel >> 16) & 0xff;  
            int green = (pixel >> 8) & 0xff;   
            int blue = (pixel) & 0xff;

            if(red == 255 && green == 255 && blue == 0)
                controller.addPlayer((float)x, (float)y);
            else if(red == 255 && green == 255 && blue == 255)  
                controller.addTerrain(x, y);
}

as you can see i don't save the level in any structure, but I just scan the image file that represents it.

  • is a good idea to do it this way?

Naturally i store all objects with the controller class where i create an arrayList that contains all game 's objects .

 

Was it helpful?

Solution

You could make a .txt file and create a map like this:

20
10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 

the 0 would represent air and the 1 a walkable tile. the first value is the map width and the second the map height. I also recommend you to use a 2d array to store the map information. Now you can read the txt file with a BufferedReader. Hope this code below helps

private final int TILE_SIZE = 30;
private int[][] blocks;

private void loadMap(File file) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            mapWidth = Integer.parseInt(reader.readLine());
            mapHeight = Integer.parseInt(reader.readLine());
            blocks = new int[mapHeight][mapWidth];

            for(int col = 0; col < mapHeight; col ++) {
                String line = reader.readLine();
                String[] tokens = line.split(" ");
                for(int row = 0; row < numBlocksRow; row++) {
                    blocks[col][row] = Integer.parseInt(tokens[row]);
                }
            }
            reader.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

private void render(Graphics2D g) {
    for(int col = 0; col < mapHeight; col ++) {
        for(int row = 0; row < numBlocksRow; row++) {
            int block = blocks[col][row];
            Color color;
            if(block == 1) {
                color = Color.white;
            } else {
                color = Color.black;
            }
            g.fillRect(row * TILE_SIZE, col * TILE_SIZE, TILE_SIZE, TILE_SIZE);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top