为什么我的瓷砖地图显示是否正确?我的、瓷砖、是否正确、地图

2023-09-07 14:49:09 作者:失心不失稳

我试图加载和渲染Slick2D水平(瓦图)从文本文件。该文本文件看起来是这样的:

  RES / RPGSheet 32​​x32.png(与此地图使用的地形设置)
5(有多少砖宽地图)
5(有多少砖高大地图)
16(有多少砖宽的地形设置为)
16(有多少砖高大的地形设置为)
4,1 4,1 4,1 4,1 4,1
4,1 1,1 2,0 4,1 4,1
4,1 4,1 1,1 4,1 4,1
4,1 1,1 4,1 1,1 4,1
4,1 4,1 4,1 4,1 4,1
 

首先,我将解释我tilemaps是如何工作的:

每个坐标表示平铺在那里位于地形设置图像上。所以0,0是在地图的左上角的瓦片,0,1是它下面的瓦片,和1,0是瓦到它的右

地砖不平已经干了怎么修复

现在的问题...

由于某些原因,该tilemaps不呈现在屏幕上正确。我相信这是正确读取这些文件,但它不是使他们正确出于某种原因。例如,上面的tilemap的应显示是这样的:

  W W W WW¯¯
 W¯¯摹ðW W
 W W G WW¯¯
 W¯¯G W G W
 W W W WW¯¯

 (如果W是水的瓷砖,G是草和D是污垢)
 

但是,相反,它呈现瓷砖的屏幕在这个阵型:

  W W W WW¯¯
 W¯¯G W G W
 宽深摹W W
 W¯¯W W G W
 W W W WW¯¯
 

为什么会出现这种情况?

下面是我的code:

 公共类播放扩展BasicGameState {

INT X;
诠释Ÿ;

形象画像;
SpriteSheet地形设置;

INT MapWidth;
INT MapHeight;

INT TilesAcross;
INT TilesTall;

INT TileWidth;
INT TileHeight;

的String [] COORDS;

INT [] tilesX;

INT [] tilesY;

字符串TileDir;

形象片;

字符串映射;

INT renderCount = 0;

   公共无效的init(GameContainer GC,StateBasedGame SBG)抛出SlickException {

       诠释计数= 0;

       图=新的String(RES / DefaultMap.txt);

        尝试(的BufferedReader在=新的BufferedReader(新的FileReader(图))){
            串线;

            TileDir =新的String(in.readLine());
            MapWidth =的Integer.parseInt(in.readLine());
            MapHeight =的Integer.parseInt(in.readLine());
            TilesAcross =的Integer.parseInt(in.readLine());
            TilesTall =的Integer.parseInt(in.readLine());

            的System.out.println((调试)地形设置目录:+ TileDir);
            的System.out.println((调试)地图宽:+ MapWidth);
            的System.out.println((调试)地图身高:+ MapHeight);
            的System.out.println((调试)地形设置为+ TilesAcross +X+ TilesTall +砖);


            tilesX =新INT [MapWidth * MapHeight]。
            tilesY =新INT [MapWidth * MapHeight]。

            而((行= in.readLine())!= NULL){
                的String []值= line.split();
                对于(字符串五:值){
                    的coords = v.split(,);

                    tilesX [计数] =的Integer.parseInt(COORDS [0]);
                    tilesY [计数] =的Integer.parseInt(COORDS [1]);

                    的System.out.println(坐标:+ tilesX [统计] +,+ tilesY [计数]);

                    算上++;
                }
            }

                }赶上(IOException异常E){
                    e.printStackTrace();
                }

                图像=新的图像(TileDir);
                TileWidth = image.getWidth()/ TilesAcross;
                TileHeight = image.getHeight()/ TilesTall;
                地形设置=新SpriteSheet(图像,TileWidth,TileHeight);
                的System.out.println((调试)瓷砖宽:+ TileWidth);
                的System.out.println((调试)瓷砖高度:+ TileHeight);

        }

   公开播放(INT状态){
   }

   公共无效渲染(GameContainer GC,StateBasedGame SBG,图形g)抛掷SlickException {
       tileset.startUse();
       为(renderCount = 0; renderCount&所述; MapWidth * MapHeight;){
       的for(int i = 0; I< MapWidth;我++){
           对于(INT J = 0; J< MapHeight; J ++){
                   tileset.getSubImage(tilesX [renderCount],tilesY [renderCount])drawEmbedded(I * TileWidth,J * TileHeight,TileWidth,TileHeight)。
                   renderCount ++;
           }
           }
       }
       tileset.endUse();
   }

   公共无效更新(GameContainer GC,StateBasedGame SBG,INT三角洲)抛出SlickException {
   }

   公众诠释的getID(){
      返回3;
   }
}
 

解决方案

 的(renderCount = 0; renderCount< MapWidth * MapHeight;){
   的for(int i = 0; I< MapWidth;我++){
       对于(INT J = 0; J< MapHeight; J ++){
               tileset.getSubImage(tilesX [renderCount],tilesY [renderCount])drawEmbedded(I * TileWidth,J * TileHeight,TileWidth,TileHeight)。
               renderCount ++;
       }
       }
 

这是一个pretty的怪片code,它似乎正进行渲染砖柱明智的,但你阵列配置的直线明智的。尝试将其替换为:

 的(INT列= 0;线474; MapHeight;列++)
{
    对于(INT线= 0;柱< MapWidth;行++)
    {
        tileset.getSubImage(tilesX [行* MapWidth +列],tilesY [行* MapWidth +列])drawEmbedded(I * TileWidth,J * TileHeight,TileWidth,TileHeight)。
    }
}
 

I am trying to load and render levels (tile maps) in Slick2D from text files. The text files look something like this:

res/RPGSheet 32x32.png     (tileset to be used with this map)
5                          (How many tiles wide the map is)
5                          (How many tiles tall the map is)
16                         (How many tiles wide the tileset is)
16                         (How many tiles tall the tileset is)
4,1 4,1 4,1 4,1 4,1
4,1 1,1 2,0 4,1 4,1
4,1 4,1 1,1 4,1 4,1
4,1 1,1 4,1 1,1 4,1
4,1 4,1 4,1 4,1 4,1

First I'll explain how my tilemaps work:

Each coordinate indicates where the tile is located on the tileset image. So 0,0 is the tile in the upper left hand corner of the map, 0,1 is the tile below it, and 1,0 is the tile to the right of it.

Now for the problem...

For some reason, the tilemaps don't render to the screen correctly. I am sure that it is reading the files correctly, but it's not rendering them correctly for some reason. For example, the tilemap above should display like this:

 W W W W W
 W G D W W
 W W G W W
 W G W G W
 W W W W W

 (If W is a water tile, G is grass and D is dirt)

But instead, it renders the tiles to the screen in this formation:

 W W W W W
 W G W G W
 W D G W W
 W W W G W
 W W W W W

Why does this happen?

Here is my code:

public class Play extends BasicGameState{

int x;
int y;

Image image;
SpriteSheet tileset;

int MapWidth;
int MapHeight;

int TilesAcross;
int TilesTall;

int TileWidth;
int TileHeight;

String[] coords;

int[] tilesX;

int[] tilesY;

String TileDir;

Image tile;

String map;

int renderCount = 0;

   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{

       int count = 0;

       map = new String("res/DefaultMap.txt");

        try(BufferedReader in = new BufferedReader(new FileReader(map))){
            String line;

            TileDir = new String(in.readLine());
            MapWidth = Integer.parseInt(in.readLine());
            MapHeight = Integer.parseInt(in.readLine());
            TilesAcross = Integer.parseInt(in.readLine());
            TilesTall = Integer.parseInt(in.readLine());

            System.out.println("(DEBUG)Tileset Directory: " + TileDir);
            System.out.println("(DEBUG)Map Width: " + MapWidth);
            System.out.println("(DEBUG)Map Height: " + MapHeight);
            System.out.println("(DEBUG)Tileset is " + TilesAcross + "x" + TilesTall + " Tiles");


            tilesX = new int[MapWidth * MapHeight];
            tilesY = new int[MapWidth * MapHeight];

            while((line=in.readLine())!=null){
                String[] values = line.split(" ");
                for(String v:values){
                    coords = v.split(",");

                    tilesX[count] = Integer.parseInt(coords[0]);
                    tilesY[count] = Integer.parseInt(coords[1]);

                    System.out.println("Coords: " + tilesX[count] + "," + tilesY[count]);

                    count++;
                }
            }

                }catch(IOException e){
                    e.printStackTrace();
                }

                image = new Image(TileDir);
                TileWidth = image.getWidth() / TilesAcross;
                TileHeight = image.getHeight() / TilesTall;
                tileset = new SpriteSheet(image, TileWidth, TileHeight);
                System.out.println("(DEBUG)Tile Width: " + TileWidth);
                System.out.println("(DEBUG)Tile Height: " + TileHeight);

        }

   public Play(int state){
   }

   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
       tileset.startUse();
       for(renderCount = 0; renderCount < MapWidth * MapHeight;){
       for(int i = 0; i < MapWidth; i++){
           for(int j = 0; j < MapHeight; j++){
                   tileset.getSubImage(tilesX[renderCount], tilesY[renderCount]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight);
                   renderCount++;
           }
           }
       }
       tileset.endUse();
   }

   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
   }

   public int getID(){
      return 3;
   }
}

解决方案

for(renderCount = 0; renderCount < MapWidth * MapHeight;){
   for(int i = 0; i < MapWidth; i++){
       for(int j = 0; j < MapHeight; j++){
               tileset.getSubImage(tilesX[renderCount], tilesY[renderCount]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight);
               renderCount++;
       }
       }

This is a pretty strange piece of code and it seems you are rendering the tiles column-wise but you array is arranged line-wise. Try to replace it with:

for(int column = 0; line < MapHeight; column++)
{
    for(int line = 0; column < MapWidth; line++)
    {
        tileset.getSubImage(tilesX[line*MapWidth + column], tilesY[line*MapWidth + column]).drawEmbedded(i * TileWidth, j * TileHeight, TileWidth, TileHeight);
    }
}