用链式反应打破砖块

时间:2012-03-22 18:00:19

标签: java

我正在开发一个java游戏,只是为了好玩。这是一种破坏某种类型的球砖。

enter image description here

这是一个关卡,当球撞到其中一个橙色砖块时,我想创建一个连锁反应,以爆炸所有其他非灰色(不可破碎)的砖块,并且在砖块被爆炸的范围内。

因此,如果没有灰砖,它将清除这个级别的所有内容。

我想我应该把那块砖的左边,右边,上面和下面的砖块都要求用那些砖块开始相同的处理。


//自我注意:阅读Enums和List

当一个爆炸性细胞被球击中时,它会调用explodeMyAdjecentCells();

//这是在Cell类

public void explodeMyAdjecentCells() {

    exploded = true;

    ballGame.breakCell(x, y, imageURL[thickness - 1][0]);

    cellBlocks.explodeCell(getX() - getWidth(),getY());
    cellBlocks.explodeCell(getX() + getWidth(),getY());
    cellBlocks.explodeCell(getX(),getY() - getHeight());
    cellBlocks.explodeCell(getX(),getY() + getHeight());

    remove();

    ballGame.playSound("src\\ballgame\\Sound\\cellBrakes.wav", 100.0f, 0.0f, false, 0.0d);

}

//这是CellHandler->(CellBlocks)

public void explodeCell(int _X, int _Y) {


    for(int c = 0; c < cells.length; c++){

        if(cells[c] != null && !cells[c].hasExploded()) {

            if(cells[c].getX() == _X && cells[c].getY() == _Y) {

                int type = cells[c].getThickness();

                if(type != 7 && type != 6 && type != 2) {

                    cells[c].explodeMyAdjecentCells();
                }


            }
        }
    }

}

它成功删除了我所有相邻的单元格,

但是在explodeMyAdjecentCells()方法中,我有这行代码

ballGame.breakCell(x, y, imageURL[thickness - 1][0]);

//

此行告诉ParticleHandler创建爆炸单元格的25个小图像(粒子)。

我的所有细胞被移除后,粒子手不会为所有移除的细胞创建粒子。


问题现在解决了,它真的很愚蠢。 我设置了particleHandler来创建最多1500个粒子。我的上帝,我怎么没看到!

private int particleCellsMax = 1500;
private int particleCellsMax = 2500;
对于所有帮助人员来说,如果有人需要,我会上传创建粒子的来源,以获得乐趣。

将图像分割成部分的源代码取自: Kalani's Tech Blog

//粒子处理程序

public void breakCell(int _X, int _Y, String URL) {

    File file = new File(URL);

    try {

        FileInputStream fis = new FileInputStream(file);
        BufferedImage image = ImageIO.read(fis);

    int rows = 5;
    int colums = 5;

    int parts = rows * colums;

    int partWidth = image.getWidth() / colums;
    int partHeight = image.getHeight() / rows;

    int count = 0;

    BufferedImage imgs[] = new BufferedImage[parts];

    for(int x = 0; x < colums; x++) {

        for(int y = 0; y < rows; y++) {

            imgs[count] = new BufferedImage(partWidth, partHeight, image.getType());

            Graphics2D g = imgs[count++].createGraphics();

            g.drawImage(image, 0, 0, partWidth, partHeight, partWidth * y, partHeight * x, partWidth * y + partWidth, partHeight * x + partHeight, null);
            g.dispose();
        }
    }

    int numParts = imgs.length;
    int c = 0;

    for(int iy = 0; iy < rows; iy ++) {

        for(int ix = 0; ix < colums; ix++) {

            if(c < numParts) {

                Image imagePart = Toolkit.getDefaultToolkit().createImage(imgs[c].getSource());
                createCellPart(_X + ((image.getWidth() / colums) * ix), _Y + ((image.getHeight() / rows) * iy), c, imagePart);

                c++;

            } else {

                break;
            }
        }
    }


    } catch(IOException io) {}
}

2 个答案:

答案 0 :(得分:2)

您可以考虑以更OO的方式查看此内容,并使用'tell do not ask'。所以你会看到一个Brick类,它会知道它的颜色是什么,以及它的相邻块。然后你会告诉第一个块爆炸,它会知道如果它是橙色(也许考虑使用枚举 - 不仅仅是数字),那么它会告诉它相邻的块'链反应'(或类似的东西) ),然后这些块将决定做什么(在橙色块的情况下爆炸 - 并调用它们的相邻块,或者在灰色块的情况下不会。

我知道它与你目前所做的完全不同,但希望能给你一个更好的结构化程序。

答案 1 :(得分:1)

我想象一种方法可以递归地获得相似颜色的所有触摸细胞。 然后你可以很容易地操作该列表(所有触摸块),并打破所有未被破坏的列表。

另请注意,你的getAdjentCell()方法有副作用(它会破坏),根据名称不是很直观。

// I agree with Matt that color (or type) should probably be an enum, 
// or at least a class.  int isn't very descriptive
public enum CellType { GRAY, RED, ORANGE }

public class Cell{
....
    public final CellType type;

    /**
     * Recursively find all adjacent cells that have the same type as this one.
     */
    public List<Cell> getTouchingSimilarCells() {
        List<Cell> result = new ArrayList<Cell>();
        result.add(this);
        for (Cell c : getAdjecentCells()) {
            if (c != null && c.type == this.type) {
                result.addAll(c.getTouchingSimilarCells());
            }
        }
        return result;
    }

    /**
     * Get the 4 adjacent cells (above, below, left and right).<br/>
     * NOTE: a cell may be null in the list if it does not exist.
     */
    public List<Cell> getAdjecentCells() {
        List<Cell> result = new ArrayList<Cell>();
        result.add(cellBlock(this.getX() + 1, this.getY()));
        result.add(cellBlock(this.getX() - 1, this.getY()));
        result.add(cellBlock(this.getX(), this.getY() + 1));
        result.add(cellBlock(this.getX(), this.getY() - 1));
        return result;
    }
}