You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

package tetris;
public class Block {
private final int type;
private int x, y;
private int[][] shape;
private static final int[][][] SHAPES = {
{{1, 1, 1, 1}}, {{1, 1}, {1, 1}}, {{1, 1, 1}, {0, 1, 0}},
{{1, 1, 1}, {1, 0, 0}}, {{1, 1, 1}, {0, 0, 1}},
{{0, 1, 1}, {1, 1, 0}}, {{1, 1, 0}, {0, 1, 1}}
};
public Block(int type) {
this.type = type;
this.shape = SHAPES[type];
this.x = (type == 0) ? 3 : 4;
this.y = 0;
}
public int getType() {
return type;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int[][] getShape() {
return shape.clone();
}
public void moveLeft() {
x--;
}
public void moveRight() {
x++;
}
public void moveDown() {
y++;
}
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}
public void rotate() {
int[][] newShape = new int[shape[0].length][shape.length];
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[0].length; j++) {
newShape[j][shape.length - 1 - i] = shape[i][j];
}
}
shape = newShape;
adjustPositionAfterRotation();
}
private void adjustPositionAfterRotation() {
int maxWidth = shape[0].length;
x = Math.max(0, Math.min(x, 10 - maxWidth));
}
}