2013-03-27 84 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