如何从Android工作室中的数组输出交互式游戏地图

时间:2016-12-10 21:18:01

标签: java android

我是完全新手,但我无法在互联网上找到如何从阵列输出游戏地图。地图必须是互动的。

我想将游戏地图输出到4x4网格的画布上

public int [] gameBoard1 = {1,0,0,1,0,0,0,1,2,0,0,1,0,0,4,0};

1 个答案:

答案 0 :(得分:0)

制作二维数组会更容易

int gameBoard[][] = {
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0}

};

然后创建一个双循环来读取每一行

for(int i = 0;i<4;i++) {
//i - index of a row, we have 4 rows so we want to loop through all of them
        for (int j = 0; j < 4; j++) {
        //j - position of an element in that row, every row has 4 positions so we want to
        //loop through all of them on every row
            if(gameBoard[i][j] == 2){
                //draw something at i,j coordinates
                //example:
                canvas.drawRect(j * 50, i * 50, (j + 1) * 50, (i + 1) * 50, new Paint());
            }
        }
    }

请记住,2d数组是一个数组数组,因此当你调用它时它的Board [数组索引] [此数组中元素的位置],这意味着当你读取2d数组的坐标时它们被反转。这就是你检查“i,j”并在“j,i”画画的原因。