title: java tetris: how to make a tetris piece move as 4 distinct tiles
body: <p>I'm trying to make the game tetris in Java as a fun side project.</p>
  
  <p>My game board is a grid of tiles:</p>
  
  <p><code>grid = new Tile[height][width];</code></p>
  
  <p>And within the grid, I create a new Tile object: <code>activetile = new Tile(this,0, 0); //add new tile to "this" board</code></p>
  
  <p>Currently:</p>
  
  <ul>
  <li><p>I'm able to control a <strong>single</strong> tile- move it down, left and right</p>
  
  <pre><code> 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 
  </code></pre>
  
  <ul>
  <li>As you can see from the keyPressed method, <code>checkBottomFull()</code> will clear bottom row if full, <code>collisionCheck()</code> will generate a new piece if block hits floor or another piece below, and <code>checkEndGame()</code> will end the game if block is stuck at the top.</li>
  </ul></li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/Zsk2W.png" alt="enter image description here"></p>
  
  <hr>
  
  <p><strong>I'm struggling with the following:</strong></p>
  
  <ul>
  <li><p>To create an actual tetris piece, I was thinking I should just generate 3 other instances of Tile, and based on what piece it is (L, O, Bar, Z, etc), set their locations at the appropriate places according to activetile (the single tile I have control over), like so:</p>
  
  <pre><code>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());
  }
  </code></pre></li>
  </ul>
  
  <p>The problem with this is, my collision detection for <code>activetile</code> won't allow it to move appropriately because it will run into its other blocks. I tried to fix that in <code>keyPressed()</code> by setting the location of <code>block2, block3, block4</code> AFTER the activetile's new location has been set, like so: (so once <code>activetile</code> moves down, all the others are allowed to move down so they don't overlap)</p>
  
  <pre><code>        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);
  </code></pre>
  
  <p>This may work for going down, but it won't work for moving left or right because the tiles will overlap.    </p>
  
  <hr>
  
  <p>So, am I correctly creating a new instane of a <code>Bar</code> piece by generating new blocks like that? Is my thinking correct?</p>
  
  <hr>
  
  <p><strong>executable</strong></p>
  
  <p><a href="https://www.dropbox.com/s/oyh26dfbmsvt5c8/my_tetris_test.jar" rel="nofollow noreferrer">https://www.dropbox.com/s/oyh26dfbmsvt5c8/my_tetris_test.jar</a></p>
  
  <p><strong>link to source code zip</strong></p>
  
  <p><a href="https://www.dropbox.com/s/9kt3sl6qqo54amk/Tetris%20Two.rar" rel="nofollow noreferrer">https://www.dropbox.com/s/9kt3sl6qqo54amk/Tetris%20Two.rar</a></p>
  
  <p>Thanks!</p>