2017-07-13 4 views
-3

私は小さなテトリスクローンを書きました。ブロックはランダムですが、6ブロックまたは7ブロック後に、ランダムブロックがpaintBlock()メソッドに渡されても、同じブロックを何度も何度も描画し始めます。テトリスボードが描かれプログラムはテトリスクローンで同じブロックを描きます

クラス:

 import blocks.AbstractBlock; 
    import blocks.BlockController; 
    import scores.ScoreController; 

    import javax.swing.*; 
    import java.awt.*; 


    public class GameBoard extends JPanel { 
     public static final Integer boardWidth = 10; 
     public static final Integer boardHeight = 20; 
     public static final Integer cellWidth = 25; 
     public static final Integer cellHeight = 25; 
     private Cell[][] board = new Cell[boardWidth][boardHeight]; 
     private Integer iterations; 
     private Boolean shutdown; 
     private AbstractBlock currentBlock; 
     private Long timestamp; 

     private Thread movement = new Thread(() -> { 
      while (!shutdown) { 
        if((System.currentTimeMillis() - timestamp) > ScoreController.getDelay()) { 
         repaint(); 
         moveDown(); 
         timestamp = System.currentTimeMillis(); 
        } 
      } 
     }); 

     public void init() { 
      for (int i = 0; i < boardWidth; i++) { 
       for (int j = 0; j < boardHeight; j++) { 
        board[i][j] = new Cell(i, j); 
       } 
      } 
      BlockController.init(); 
      ScoreController.init(); 
      currentBlock = BlockController.getRandomBlock(); 
      iterations = 0; 
      timestamp = System.currentTimeMillis(); 
      shutdown = false; 
      movement.start(); 
     } 

     public synchronized void moveDown() { 
      Point position = BlockController.getBlockPosition(); 
      if (!findCollisions(position.x, position.y + 1)) { 
       BlockController.setBlockPosition(new Cell(position.x, position.y + 1)); 
       iterations += 1; 
       this.repaint(); 
      } else { 
       if (iterations > 0) { 
        secureBlock(); 
        clearRows(); 
        currentBlock = BlockController.getRandomBlock(); 
        iterations = 0; 
       } else { 
        shutdown(); 
       } 
      } 
     } 

     private Boolean findCollisions(Integer x, Integer y) { 
      for (Cell cell : currentBlock.getShapes()[BlockController.getRotationScale()]) { 
       if (cell.y + y >= boardHeight || cell.x + x >= boardWidth || cell.x + x < 0 || 
         !(board[cell.x + x][cell.y + y].getColor() == cell.getDefaultColor())) { 
        return true; 
       } 
      } 
      return false; 
     } 

     public void moveHorizontal(Integer dx) { 
      Point position = BlockController.getBlockPosition(); 
      if (!findCollisions(position.x + dx, position.y)) { 
       BlockController.setBlockPosition(new Cell(position.x + dx, position.y)); 
      } 
      repaint(); 
     } 

     private void secureBlock() { 
      Cell position = BlockController.getBlockPosition(); 
      for (Cell cell : currentBlock.getShapes()[BlockController.getRotationScale()]) { 
       board[position.x + cell.x][position.y + cell.y].setColor(currentBlock.getColor()); 
      } 
      repaint(); 
     } 

     private void clearRows() { 
      Integer rowsCleared = 0; 
      Boolean gap; 
      for (int j = boardHeight - 1; j >= 0; j--) { 
       gap = false; 
       for (int i = 0; i < boardWidth; i++) { 
        if (board[i][j].getColor() == board[i][j].getDefaultColor()) { 
         gap = true; 
         break; 
        } 
       } 
       if (!gap) { 
        deleteRow(j); 
        j = boardHeight - 1; 
        repaint(); 
        rowsCleared++; 
       } 
      } 
      if (rowsCleared != 0) { 
       ScoreController.rowsCleared(rowsCleared); 
      } 
     } 

     private void deleteRow(Integer y) { 
      for (int j = y; j > 0; j--) { 
       for (int i = 0; i < boardWidth; i++) { 
        board[i][j].setColor(board[i][j - 1].getColor()); 
       } 
      } 
     } 

     public void shutdown() { 
      shutdown = true; 
     } 

     public void rotate() { 
      BlockController.rotate(); 
      repaint(); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      paintGameBoard(g); 
      paintBlock(g); 
     } 

     private void paintGameBoard(Graphics g) { 
      for (int i = 0; i < boardWidth; i++) { 
       for (Cell cell : board[i]) { 
        g.setColor(cell.getColor()); 
        g.drawRect(cell.x * cellWidth, cell.y * cellHeight, cellWidth, cellHeight); 
        g.fillRect(cell.x * cellWidth, cell.y * cellHeight, cellWidth, cellHeight); 
       } 
      } 
      g.setColor(Color.WHITE); 
      g.drawString("Score: " + ScoreController.getScore(), 10, 20); 
      g.drawString("Level: " + ScoreController.getLevel(), 10, 40); 
      g.drawString("Rows Cleared: " + ScoreController.getRows(), 10, 60); 
     } 

     private void paintBlock(Graphics g) { 
      Cell position = BlockController.getBlockPosition(); 
      if (currentBlock != null) { 
       for (Cell cell : currentBlock.getShapes()[BlockController.getRotationScale()]) { 
        g.setColor(currentBlock.getColor()); 
        g.drawRect((position.x + cell.x) * cellWidth, (position.y + cell.y) * cellHeight, cellWidth, cellHeight); 
        g.fillRect((position.x + cell.x) * cellWidth, (position.y + cell.y) * cellHeight, cellWidth, cellHeight); 
       } 
      } 
     } 
    } 

私はこの問題の解決策を見つけることができなかったので、何か提案は歓迎されています。前もって感謝します。

Edit1:BlockControllerクラス - ランダムブロックや他のものを返すために使用されます。

+1

'BlockController'コードを表示できますか?これを使用してブロックを生成します。問題はそこにあるかもしれません。 – davidxxx

+1

コードを解析して、どこが壊れているかを確認することがあります。 [mcve]を参照してください。 – CDahn

+0

私はdebugを使ってコードを実行していましたが、BlockControllerは毎回ランダムブロックを返しましたが、同じブロックを何度も何度も何度も繰り返し描画していました。それはおそらくそれを描画するキャッチはありますか? –

答えて

0

これは何とか私のブロックを取得していたために発生しました。シングルトンの束ではなくブロックの単純なマップは、私がこれを解決するのを助けました。 この方向のチップはdavidxxxありがとうございます。

+0

うん、私の悪い。私のばかな問題に対する実際の答えを示すためにそれを編集しました。 –

関連する問題