在Java Game Project

时间:2017-11-28 15:58:42

标签: java arrays

我希望你能帮助我,因为我是一名学生,而且我的项目遇到了麻烦。该项目是一款名为Brick Breaker的游戏。

在游戏中我必须创建一块基本上是jButton的砖块,当点击它时会检查相同颜色的相邻按钮,如果有2个或更多具有相同颜色的jthey将被销毁,然后其他砖块如果列是空的,则必须折叠并且取代它们的行是空的或者靠近它。

到目前为止,我所做的是我用2d数组创建了带有彩色砖块的框架和面板,但我尝试在按钮上使用动作监听器并失败。

此外,我无法找到所有相同颜色的相邻按钮。 我无法上传图片,因为我是论坛新手,我们将不胜感激。

这是我想在java中制作的游戏的链接  http://el.y8.com/games/easy_brick

我为长篇大论道歉。

我按要求添加了代码。 注意:我对Java编程没有经验,我知道我可以改进我的代码但是我缺乏刚刚开始的经验。

public class BB {

    public static void main(String[] args) {
        int size=14;
        BB neighbours = new BB();
        JButton button[][]=new JButton[size][size];        
        ActionListener listener=(ActionEvent ae) -> {
            for(int i=0;i<size;i++){    //creating an array holding buttons and randomly coloring them
                for(int j=0;j<size;j++){
                    if (ae.getSource()==button[i][j]){
                        neighbours.checkup(button,i,j);
                        neighbours.checkdown(button,i,j);
                        neighbours.checkleft(button,i,j);
                        neighbours.checkright(button,i,j);
                        break;}
                }
            }
        };
        Random rand = new Random();
        JFrame frame =new JFrame("BrickBreaker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel= new JPanel();
        panel.setLayout(new GridLayout(size,size));

        for(int i=0;i<size;i++){    //creating an array holding buttons and randomly coloring them
            for(int j=0;j<size;j++){
                button[i][j]= new JButton();
                button[i][j].addActionListener(listener);
            int x=rand.nextInt(4);
           switch (x) {
               case 0:
                   button[i][j].setBackground(Color.BLUE); 
                   break;
               case 1:
                   button[i][j].setBackground(Color.MAGENTA); 
                   break;
               case 2:
                   button[i][j].setBackground(Color.GREEN); 
                   break;
               case 3:
                   button[i][j].setBackground(Color.YELLOW); 
                   break;
               default:
                   button[i][j].setBackground(Color.MAGENTA); 
                   break;
           }
            panel.add(button[i][j]);
        }}
        frame.add(panel);
        frame.pack();
        frame.setSize(900,900);
        frame.setVisible(true);

    }
    public void checkup(JButton[][] button,int i, int j){
        if(button[i][j].getBackground()==button[i][j-1].getBackground() && j-1>0){
            if(button[i][j]!=null)
                button[i][j]=null;
            checkleft(button,i,j-1);
            checkright(button,i,j-1);
            checkup(button,i,j-1);}
    }

    public void checkdown(JButton[][] button, int i, int j){
        if(button[i][j].getBackground()==button[i][j+1].getBackground() && j+1<14){
            if(button[i][j]!=null)
                button[i][j]=null;
            checkleft(button,i,j+1);
            checkright(button,i,j+1);
            checkdown(button,i,j+1);}
    }

    public void checkleft(JButton[][] button, int i, int j){
        if(button[i][j].getBackground()==button[i-1][j].getBackground() && i-1>0){
            if(button[i][j]!=null)
                button[i][j]=null;
            checkup(button,i-1,j);
            checkdown(button,i-1,j);
            checkleft(button,i-1,j);}
    }
    public void checkright(JButton[][] button, int i, int j){
        if(button[i][j].getBackground()==button[i+1][j].getBackground() && i+1<14){
            if(button[i][j]!=null)
                button[i][j]=null;
            checkup(button,i+1,j);
            checkdown(button,i+1,j);
            checkright(button,i+1,j);}
    }
}

0 个答案:

没有答案