我在玩Java游戏只是为了好玩。这是某种破砖游戏。



这是一个级别,当球击中橙色的砖块之一时,我想创建一个连锁反应,以爆炸所有其他非灰色(牢不可破)且在爆炸砖块触及范围内的砖块。

因此,它将清除此级别中的所有内容,而无需使用灰色砖块。

我想我应该向正在爆炸的其他砖块询问砖块的左,右,上和下,然后使用这些单元格开始相同的过程。



//注意自己:阅读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不会为所有删除的单元格创建粒子。



这个问题到现在就解决了,真是愚蠢。
我已经设置了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) {}
}

最佳答案

我想像一种方法可以递归地获得所有相似颜色的接触单元。
然后,您可以轻松地在该列表(包括所有碰到的块)上进行操作,并破坏所有尚未破坏的块。

还要注意,您的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;
    }
}

10-08 03:59