简体   繁体   English

用Java创建图块地图

[英]Creating a Tile Map in Java

I want to know how to make tile based map in java. 我想知道如何在Java中制作基于图块的地图。 I want to make a basic 2d platformer type or top-down type.I have already researched, but none helped much. 我想做一个基本的2d平台游戏类型或自顶向下的类型。我已经研究过了,但是没有什么帮助。 However, what I have learned is that I have to make tile objects and a two-dimensional matrix to store position of tiles. 但是,我了解到的是,我必须制作图块对象和一个二维矩阵来存储图块的位置。

Also there are many programs available on the internet for making maps. 互联网上也有许多制作地图的程序。 i do not want to use them. 我不想使用它们。

You could use a two dimensional array to store your map tiles like so, 您可以像这样使用二维数组来存储地图图块,

int[][] map =
{
{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, 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, 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},
};

Then access your map positions like map[1][1] . 然后访问您的地图位置,例如map[1][1] Or change the variables like map[0][0] = 1. 或者更改变量,例如map[0][0] = 1。

Instead of writing out an entire array like that, you may prefer to use a loop to generate your 0's, or 1's or whatever it is you want your tiles to be. 与其写出像这样的整个数组,不如使用循环生成0或1或您想要的平铺对象。

like so, 像这样

int[][] tileMap = new int[10][10];

for(int i = 0; i < tileMap.length; i++) {
    for(int j = 0; j < tileMap[0].length; j++) {
       tileMap[i][j] = 0;//put whatever number you want in 
       //here and it will insert it instead of 0's
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM