迷宫游戏碰撞检测



我现在正在制作一个迷宫游戏,我一直在努力编写碰撞检测的代码。球(数字 4)应该只在沙像(分配给数字 1)上移动,但不应该在白色图像(数组中的数字 2)上移动。这是我用于地图的数组:

int [] map1 = {  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                     3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

这是分配给数组中数字的按钮的代码:

  for( int nCount= 0; nCount < 208; nCount++ ) //for loop which goes from 0 to 208
        {
            JGridButton[nCount] = new JButton(""); //inserts a button for every count in the for loop
            JPanelnorth.add(JGridButton[nCount]); //this line of code adds the buttons to the named panel
            JGridButton[nCount].setBorderPainted(false);
        if(map1[nCount]==1) // the image on the button will be set to sandimage, if the number in the array = 1
        {
            JGridButton[nCount].setIcon(sandimage);
        }
        if(map1[nCount]==2) // the image on the button will be set to whiteimage, if the number in the array = 2
        {
            JGridButton[nCount].setIcon(whiteimage);
        }
        if(map1[nCount]== 4) // the image on the button will be set to goldenball, if the number in the array = 4
        {
            JGridButton[nCount].setIcon(goldenball);
        }
        if(map1[nCount]== 3) // the image on the button will be set to sandstone, if the number in the array = 3
        {
            JGridButton[nCount].setIcon(sandstone);
        }
      }

你需要做的是让它得到球要去的部分的坐标。因此,如果用户按下向下箭头,它会得到坐标,检查数组中的该项是否为白色,如果不是,则移动。

此外,您绝对应该为地图使用 2D 数组而不是 1D 数组,以便您可以通过 x,y 访问位置。 int[][] map1 = {firstRow of stuff},{second row of stuff}, {etc};然后boolean isTouchingWhite = map1[x][y] == 2;

在我看来,

您在移动石头之前没有检查边界。以下是我将如何从您显示的内容中移动代码:

public void actionPerformed(ActionEvent event) 
{
    Object source = event.getSource(); 
    int next = nPosition - 16;
    if(source == Jbuttonup && next >= 0 && map1[next] == 1) 
    {
        JButtoncompass.setIcon(iconCompassnorth); 
        JTextField3.setText("N");
        // update the map
        map1[next] = 4;
        map1[nPosition] = 1;
        nPosition = next; 
        // let the paint method draw the updated map.
        repaint(); 
    }
}

对于左边界,我将实现为(next >= 0 && (nPosition % 16) != 0),右边界检查应该是(next < map1.length && (next % 16) != 0)的,向下边界检查应该是(next < map1.length)的。关键是要检查map1[next] == 1

最新更新