I am trying to make a Tetris game in java.
I realized where:
a new block is created when it hits the floor or its y + 1 is not zero (which means that there is another block below it)
public void collisionCheck(int x, int y) {
if (activetile.getY() == this.height-2 || getTileAt(x, y+1) != null) {
activetile = new Tile(this, 0, 0);
}
}
The line is cleared when the bottom line is filled with non-zero values, or Tetris fragments (for y = 4 (gender), go through x to x = 4 and check if not all are)
public void checkBottomFull(int x, int y) {
while (getTileAt(x,y) != null) {
say("(" + x + ", " + y +")");
if (x == 3) {
say("row is full");
for (int i = 0; i < 4; i++) {
for (int j = 5; j > 0; j--) {
grid[j][i] = getTileAt(i,j-1);
grid[j-1][i] = null;
}
}
break;
}
x++;
}
}
Now I use the keys to move the block:
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_DOWN) {
activetile.setLocation(activetile.getX(), activetile.getY()+1);
System.out.println("coordinates: " + activetile.getX() + ", " + activetile.getY());
collisionCheck(activetile.getX(),activetile.getY());
checkBottomFull(0,4);
repaint();
}
}
There are two problems that I am having:
1) In the picture you will notice that I completely threw the block to the floor ... and the line cleared. After its removal, it will generate a block in the upper left corner (x = 0, y = 1), which I do not control.
2) , , ..., , , JFrame... , .

FYI: , [j] [i] (, [i] [j]), grid = new Tile[height][width];
?
!