2013-03-27 94 views
0

我在Java中製作俄羅斯方塊......我能夠得到一個單一的俄羅斯方塊瓷磚移動就好...但如果我嘗試將整塊移動(由多個瓷磚),任何移動到當前存在的瓷磚(當前俄羅斯方塊的瓷磚)位置的瓷磚被設置爲空。俄羅斯方塊,移動一塊

我的目標是:在電路板上設置當前瓷磚空所有的瓷磚(只使用2瓦現在來測試)

if (keycode == KeyEvent.VK_DOWN) { 
    newX = tile.getX();  //tile x 
    newY = tile.getY()+1; //tile y 
    newX2 = tile2.getX(); //tile 2 x 
    newY2 = tile2.getY()+1; //tile 2 y 

2)的

1)計算新的位置(基本上,拾取所有瓦片掉在板)

board.setTileAt(null, tile.getX(), tile.getY()); 
board.setTileAt(null, tile2.getX(), tile2.getY()); 

局setTileAt方法供參考:

public void setTileAt(Tile tile, int x, int y) { 
    grid[x][y] = tile; 
} 

3)執行有效的移動檢查(是否在邊界內移動?和......是電網[X] [Y]空?)

4)最後,如果有效,在新位置設置的瓷磚背面板上

tile.setLocation(newX, newY); 
tile2.setLocation(newX2, newY2); 

enter image description here

輸出:

Game running... 
original: 1, 1 
new: 1, 2 
original: 1, 2 
new: 1, 3 

有什麼想法?我的邏輯是把棋子的單個棋子拿下來,然後在新的位置替換它們是否正確?

謝謝!


編輯:

增加有效舉措檢查Board類:

在邊界?

public boolean isValidCoordinate(int x, int y) { 
    return x >= 0 && y >= 0 && x < width && y < height; 
} 

是空缺嗎?

public boolean isOpen(int x, int y) { 
    return isValidCoordinate(x, y) && getTileAt(x, y) == null; 
} 

在一塊上課,我設置當前瓦片位置爲空,如果ISOPEN是真實的......另外,我設置新的位置...

public void move(int keycode) { 
    //move 
    int newX, newY, newX2, newY2; 
    if (keycode == KeyEvent.VK_DOWN) { 
     newX = tile.getX(); 
     newY = tile.getY()+1; 
     newX2 = tile2.getX(); 
     newY2 = tile2.getY()+1; 

     if (board.isOpen(newX2, newY2) && board.isOpen(newX, newY)) { 
      board.setTileAt(null, tile.getX(), tile.getY()); 
      board.setTileAt(null, tile2.getX(), tile2.getY()); 

      System.out.println("original: " + tile.getX() + ", " + tile.getY()); 
      System.out.println("new: " + newX + ", " + newY); 
      System.out.println("original: " + tile2.getX() + ", " + tile2.getY()); 
      System.out.println("new: " + newX2 + ", " + newY2); 
      tile.setLocation(newX, newY); 
      tile2.setLocation(newX2, newY2); 
     }    
    } 
} 
+1

爲什麼不把所有的標題都放在一個單獨的「片段」中,並將其作爲單個單元移動。這只是標題的渲染(因爲他們會,在技術上是在一塊內的相同位置) – MadProgrammer 2013-03-27 19:36:45

回答

1

你有正確的步驟,但錯誤的順序。

1)計算的所有圖塊(只使用2瓦現在來測試)

2)進行有效的舉措檢查(是移動的界限?和...的新職位是電網[X] [Y]空?)

3)如果板上有效,設置當前瓦片爲空(基本上,拾取所有瓦片掉在板)

4)最後,如果有效,將瓷磚背面板上的新位置

祝你好運。

+0

謝謝,但我試過這樣做,現在都沒有移動。我編輯了上面的代碼。 – Growler 2013-03-27 18:57:16