2013-03-07 77 views
1

我正在嘗試在Java中製作遊戲俄羅斯方塊作爲有趣的一面項目。java俄羅斯方塊:如何使俄羅斯方塊片移動爲4個不同的瓷磚

我的遊戲板是塊網格:

grid = new Tile[height][width];

和電網內,我創建了一個新的瓷磚對象:activetile = new Tile(this,0, 0); //add new tile to "this" board

目前:

  • 我能夠控制一個單個瓷磚 - 向下移動,左右移動

    public void keyPressed(KeyEvent e) { 
        int keyCode = e.getKeyCode(); 
        if(keyCode == KeyEvent.VK_DOWN) { 
         checkBottomFull(0,4); 
         collisionCheck(activetile.getX(),activetile.getY()); 
         checkEndGame(activetile.getX(), activetile.getY()); 
    
         activetile.setLocation(activetile.getX(), activetile.getY()+1); 
         System.out.println("coordinates: " + activetile.getX() + ", " + activetile.getY()); 
    
         repaint(); 
        } 
         ...right key and left key code omitted 
    
    • 你可以從的keyPressed方法看,checkBottomFull()將清除底行,如果滿了,collisionCheck()會如果塊擊中地面或低於另一塊生成一個新的作品,並checkEndGame()將結束遊戲,如果塊卡在頂部。

enter image description here


我用下面的掙扎:

  • 要創建一個實際的俄羅斯方塊一塊,我想我應該只產生3其他Tile的實例,並基於它是什麼(L,O,Bar,Z等),將它們的位置設置爲合適的pl根據activetile王牌(單瓦我有控制),像這樣:

    if (piece == "Bar") { 
        block2 = new Tile(this, activetile.getX(), activetile.getY()); 
        block3 = new Tile(this, activetile.getX()+2, activetile.getY()); 
        block4 = new Tile(this, activetile.getX()+3, activetile.getY()); 
    } 
    

這樣做的問題是,我的碰撞檢測activetile不會允許它適當地移動,因爲它會運行進入其他區塊。我嘗試在keyPressed()中通過設置block2, block3, block4的位置來解決這個問題,之後設置好了激活人的新位置,如下所示:(所以一旦activetile向下移動,所有其他人都可以向下移動,因此它們不會重疊)

 activetile.setLocation(activetile.getX(), activetile.getY()+1); 
     block2.setLocation(activetile.getX(), activetile.getY()+1); 
     block3.setLocation(activetile.getX(), activetile.getY()+1); 
     block4.setLocation(activetile.getX(), activetile.getY()+1); 

這可能會下降,但它不會左右移動,因爲瓷磚會重疊。


所以,我在正確創建通過生成這樣的新塊Bar片的新instane?我的想法是否正確?


可執行

https://www.dropbox.com/s/oyh26dfbmsvt5c8/my_tetris_test.jar

鏈接到源代碼zip

https://www.dropbox.com/s/9kt3sl6qqo54amk/Tetris%20Two.rar

謝謝!

+1

爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-07 06:06:14

+0

@AndrewThompson我已經上傳了一個可運行的jar到dropbox以及作爲zip的源代碼。你可以試試嗎? – Growler 2013-03-07 06:15:54

+0

@AndrewThompson我已經讀過它,我認爲我的EXE實現了所有這些東西。沒關係,如果你不想看看它。 – Growler 2013-03-07 06:25:43

回答