Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 迷宫游戏碰撞检测_Java_Swing - Fatal编程技术网

Java 迷宫游戏碰撞检测

Java 迷宫游戏碰撞检测,java,swing,Java,Swing,我现在正在做一个迷宫游戏,我一直在努力编写碰撞检测代码。球(编号4)应仅在sandimage(指定给编号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,

我现在正在做一个迷宫游戏,我一直在努力编写碰撞检测代码。球(编号4)应仅在sandimage(指定给编号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);
        }
      }
for(int-nCount=0;nCount<208;nCount++)//for从0到208的循环
{
JGridButton[nCount]=new JButton(“”;//为for循环中的每个计数插入一个按钮
JPanelnorth.add(JGridButton[nCount]);//这行代码将按钮添加到命名面板中
JGridButton[nCount].SetbOrderPaint(false);
if(map1[nCount]==1)//如果数组中的数字=1,则按钮上的图像将设置为sandimage
{
JGridButton[nCount].setIcon(sandimage);
}
if(map1[nCount]==2)//如果数组中的数字=2,则按钮上的图像将设置为whiteimage
{
JGridButton[nCount].setIcon(白色图像);
}
if(map1[nCount]==4)//如果数组中的数字=4,则按钮上的图像将设置为goldenball
{
JGridButton[nCount].setIcon(goldenball);
}
if(map1[nCount]==3)//如果数组中的数字=3,则按钮上的图像将设置为砂岩
{
JGridButton[nCount].setIcon(砂岩);
}
}

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

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

然后
booleanistochingwhite=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
,下边界检查应为
(next
。关键是检查
map1[next]==1

对不起,在我看来实际的运动代码丢失了。我看到了地图和一个油漆代码。你打算如何让球穿过迷宫?键盘单击其中一个按钮?这是我用来让球穿过迷宫的代码:public void actionPerformed(ActionEvent事件){Object source=event.getSource();if(source==Jbuttonup){JButtoncompass.setIcon(iconCompassnorth);JTextField3.setText(“N”);JGridButton[nPosition-16]。设置图标(goldenball);JGridButton[nPosition]。设置图标(sandimage);nPosition=nPosition-16;}向下、向左和向右的代码与此类似1)不要使用
208
而是使用
map.length
.2)不要使用多个
if
语句,而是使用一个
switch
语句。3)在数字数组和按钮数组之间使用
map
会更容易。4)可以在数字和图像之间使用
map
。您好,谢谢您的帮助但是我的讲师告诉我,在这个项目中,我们不应该使用2D数组,而应该坚持使用1D数组。你也可以对1D数组使用相同的代码吗?比如这个布尔值isTouchingWhite=map[]==2;我真的不知道你的最终产品应该是什么样子……你能发布一个链接到用户需要导航的迷宫的屏幕截图吗?@Umerrama你也可以使用一个聪明的小公式将你的1d数组变成2d。数组上的位置等于x坐标加上y坐标乘以数组宽度。简而言之,
pos=x+(y*width)
您可以做
boolean-isTouchingWhite=map1[x+(y*map1\u width)]==2;
很抱歉回复太晚:(我有一个名为nPosition的整数,它告诉程序球当前在哪个按钮上,并且想要继续前进。原则上不一样吗?我如何在评论区上传图片?只需将其上传到图片托管站点并发布链接即可。