Search code examples
javaslick2d

Why wont the whole tile map show?


I'm trying to create a very simple tile map system, I had issues a few weeks ago and asked here, but lately I have rewritten it and it stopped working correctly.

Please note that I'm using slick2D so if you want to reproduce this then you have to put the code in your main render loop.

Array

public static int[][] map = {{1,1,1,1,1,2,1,1,1,1,1,1,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,0,0,0,0,0,0,0,0,0,0,0,1},
                              {1,1,1,1,1,1,1,0,0,0,0,0,1}};

Tile map loop.

int x = 0;
int y = 0;
int I = 0;
int II = 0;
while(y <= 7){
    while( x <= 12){
        if(map[y][x] == 0){
            Image img = new Image("res/tile1.png");
            img.draw(II,I);
        }
        if(map[y][x] == 1){
            Image img = new Image("res/tile0.png");
            img.draw(II,I);
        }
        if(map[y][x] == 2){
            Image img = new Image("res/tile3.jpg");
            img.draw(II,I);
        }
        x++;
        II = x * 100;
    }
    y++;
    I = y * 100;
}

Screenshot http://puu.sh/iIf9r/42c3b6f4db.png

Thanks.


Solution

  • For what I understood from your code you like to print your images in a rectangle 7x12. If I understood well you have to reset x before each row so before the while( x <= 12){

    int x = 0;
    int y = 0;
    int I = 0;
    int II = 0;
    while(y <= 7){
        x = 0;
        while( x <= 12){
            if(map[y][x] == 0){
                Image img = new Image("res/tile1.png");
                img.draw(II,I);
            }
            if(map[y][x] == 1){
                Image img = new Image("res/tile0.png");
                img.draw(II,I);
            }
            if(map[y][x] == 2){
                Image img = new Image("res/tile3.jpg");
                img.draw(II,I);
            }
            x++;
            II = x * 100;
        }
        y++;
        I = y * 100;
    }
    

    Note to have a better performance is not needed to create a new Image each time. Create the tiles before all and reuse them.