2012-03-22 34 views
1

我正在用java開發一款遊戲,只是爲了好玩。這是一款破球遊戲。用連鎖反應打破磚塊

enter image description here

這裏是一個電平,當球擊中橙磚之一我想創建連鎖反應爆炸是NOT灰色(牢不可破),並且內的磚的到達所有其他磚正在爆炸。

因此,它將清除這個層面上沒有灰磚的所有東西。

我在想,我應該問磚塊,它正在爆炸其他磚磚的左,右,上,下,然後開始與那些細胞相同的過程。


//注意自我:當一個爆炸性的細胞被擊中球上枚舉和列表

讀了它調用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 - >(牢房)

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; 

THX所有幫助的人,我會上傳源樂趣創建粒子youst如果有人需要它。

的分支圖像分成幾部分的源代碼是摘自: 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) {} 
} 

回答

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; 
    } 
} 
+0

謝謝,我意識到枚舉是更好的方法。堅韌我對枚舉和使用列表幾乎沒有經驗,我不得不閱讀它。我重寫了我的代碼一點(查看頂部),我一直在一直存在的問題是,所有的細胞都被刪除,但並非所有的細胞都被一個效果(粒子處理程序)刪除..你會這麼善意查看更新後的代碼。 thx – 2012-03-22 22:48:44

+0

太棒了!我很高興你明白了。 – JohnnyK 2012-03-23 13:58:08

2

你可以考慮在一個更面向對象的方式看這個,並使用「告訴唐'不問'。所以你會看看有一個Brick類,它會知道它的顏色和它的相鄰塊。然後你會告訴第一個Block爆炸,然後它會知道如果它是Orange(也許考慮使用Enums這個 - 不僅僅是數字),那麼它會告訴它的相鄰塊'連鎖反應'(或類似的東西),然後這些塊將決定做什麼(或者在橙色塊的情況下爆炸 - 並且調用它們的相鄰塊,或者不在灰色塊的情況下)。

我知道它與你做的很不一樣目前,但會給你一個更好的結構化程序,希望

+0

謝謝,我意識到枚舉是更好的方式去。堅韌我對枚舉和使用列表幾乎沒有經驗,我不得不閱讀它。 – 2012-03-22 22:38:35