public class Cell {
private int row;
private int col;
private BufferedImage image;
public Cell() {
}
public Cell(int row, int col, BufferedImage image) {
this.row = row;
this.col = col;
this.image = image;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
@Override
public String toString() {
return "Cell{" +
"row=" + row +
", col=" + col +
", image=" + image +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Cell)) {
return false;
}
Cell cell = (Cell) o;
return getRow() == cell.getRow() &&
getCol() == cell.getCol() &&
Objects.equals(getImage(), cell.getImage());
}
@Override
public int hashCode() {
return Objects.hash(getRow(), getCol(), getImage());
}
public void left(){
col--;
}
public void right(){
col++;
}
public void down(){
row++;
}
}
public class Tetromino {
public Cell[] cells = new Cell[4];
protected State[] states;
protected int count = 10000;
public void moveLeft() {
for (Cell cell : cells) {
cell.left();
}
}
public void moveRight() {
for (Cell cell : cells) {
cell.right();
}
}
public void moveDrop() {
for (Cell cell : cells) {
cell.down();
}
}
public static Tetromino randomOne() {
int num = (int) (Math.random() * 7);
Tetromino tetromino = null;
switch (num) {
case 0:
tetromino = new I();
break;
case 1:
tetromino = new J();
break;
case 2:
tetromino = new L();
break;
case 3:
tetromino = new O();
break;
case 4:
tetromino = new S();
break;
case 5:
tetromino = new T();
break;
case 6:
tetromino = new Z();
break;
}
return tetromino;
}
public void rotateRight() {
if (states.length == 0) {
return;
}
count++;
State s = states[count % states.length];
Cell cell = cells[0];
int row = cell.getRow();
int col = cell.getCol();
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
public void rotateLeft() {
if (states.length == 0) {
return;
}
count--;
State s = states[count % states.length];
Cell cell = cells[0];
int row = cell.getRow();
int col = cell.getCol();
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
protected class State {
int row0, col0, row1, col1, row2, col2, row3, col3;
public State() {
}
public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
public int getRow0() {
return row0;
}
public void setRow0(int row0) {
this.row0 = row0;
}
public int getCol0() {
return col0;
}
public void setCol0(int col0) {
this.col0 = col0;
}
public int getRow1() {
return row1;
}
public void setRow1(int row1) {
this.row1 = row1;
}
public int getCol1() {
return col1;
}
public void setCol1(int col1) {
this.col1 = col1;
}
public int getRow2() {
return row2;
}
public void setRow2(int row2) {
this.row2 = row2;
}
public int getCol2() {
return col2;
}
public void setCol2(int col2) {
this.col2 = col2;
}
public int getRow3() {
return row3;
}
public void setRow3(int row3) {
this.row3 = row3;
}
public int getCol3() {
return col3;
}
public void setCol3(int col3) {
this.col3 = col3;
}
@Override
public String toString() {
return "State{" +
"row0=" + row0 +
", col0=" + col0 +
", row1=" + row1 +
", col1=" + col1 +
", row2=" + row2 +
", col2=" + col2 +
", row3=" + row3 +
", col3=" + col3 +
'}';
}
}
}
public class I extends Tetromino {
public I() {
cells[0] = new Cell(0,4, Tetris.I);
cells[1] = new Cell(0,3, Tetris.I);
cells[2] = new Cell(0,5, Tetris.I);
cells[3] = new Cell(0,6, Tetris.I);
states =new State[2];
states[0]=new State(0,0,0,-1,0,1,0,2);
states[1]=new State(0,0,-1,0,1,0,2,0);
}
}
public class J extends Tetromino {
public J() {
cells[0] = new Cell(0,4, Tetris.J);
cells[1] = new Cell(0,3, Tetris.J);
cells[2] = new Cell(0,5, Tetris.J);
cells[3] = new Cell(1,5, Tetris.J);
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,1);
states[1]=new State(0,0,-1,0,1,0,1,-1);
states[2]=new State(0,0,0,1,0,-1,-1,-1);
states[3]=new State(0,0,1,0,-1,0,-1,1);
}
}
public class L extends Tetromino {
public L() {
cells[0] = new Cell(0,4, Tetris.L);
cells[1] = new Cell(0,3, Tetris.L);
cells[2] = new Cell(0,5, Tetris.L);
cells[3] = new Cell(1,3, Tetris.L);
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,-1);
states[1]=new State(0,0,-1,0,1,0,-1,-1);
states[2]=new State(0,0,0,1,0,-1,-1,1);
states[3]=new State(0,0,1,0,-1,0,1,1);
}
}
public class O extends Tetromino {
public O() {
cells[0] = new Cell(0, 4, Tetris.O);
cells[1] = new Cell(0, 5, Tetris.O);
cells[2] = new Cell(1, 4, Tetris.O);
cells[3] = new Cell(1, 5, Tetris.O);
states = new State[0];
}
}
public class S extends Tetromino {
public S() {
cells[0] = new Cell(0,4, Tetris.S);
cells[1] = new Cell(0,5, Tetris.S);
cells[2] = new Cell(1,3, Tetris.S);
cells[3] = new Cell(1,4, Tetris.S);
states =new State[2];
states[0]=new State(0,0,0,1,1,-1,1,0);
states[1]=new State(0,0,1,0,-1,-1,0,-1);
}
}
public class S extends Tetromino {
public S() {
cells[0] = new Cell(0,4, Tetris.S);
cells[1] = new Cell(0,5, Tetris.S);
cells[2] = new Cell(1,3, Tetris.S);
cells[3] = new Cell(1,4, Tetris.S);
states =new State[2];
states[0]=new State(0,0,0,1,1,-1,1,0);
states[1]=new State(0,0,1,0,-1,-1,0,-1);
}
}
public class T extends Tetromino {
public T() {
cells[0] = new Cell(0,4, Tetris.T);
cells[1] = new Cell(0,3, Tetris.T);
cells[2] = new Cell(0,5, Tetris.T);
cells[3] = new Cell(1,4, Tetris.T);
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,0);
states[1]=new State(0,0,-1,0,1,0,0,-1);
states[2]=new State(0,0,0,1,0,-1,-1,0);
states[3]=new State(0,0,1,0,-1,0,0,1);
}
}
public class Z extends Tetromino {
public Z() {
cells[0] = new Cell(1,4, Tetris.Z);
cells[1] = new Cell(0,3, Tetris.Z);
cells[2] = new Cell(0,4, Tetris.Z);
cells[3] = new Cell(1,5, Tetris.Z);
states =new State[2];
states[0]=new State(0,0,-1,-1,-1,0,0,1);
states[1]=new State(0,0,-1,1,0,1,1,0);
}
}
public class Tetris extends JPanel {
private Tetromino currentOne = Tetromino.randomOne();
private Tetromino nextOne = Tetromino.randomOne();
private Cell[][] wall = new Cell[18][9];
private static final int CELL_SIZE = 48;
int[] scores_pool = {0, 1, 2, 5, 10};
private int totalScore = 0;
private int totalLine = 0;
public static final int PLING = 0;
public static final int STOP = 1;
public static final int OVER = 2;
private int game_state;
String[] show_state = {"P[pause]", "C[continue]", "S[replay]"};
public static BufferedImage I;
public static BufferedImage J;
public static BufferedImage L;
public static BufferedImage O;
public static BufferedImage S;
public static BufferedImage T;
public static BufferedImage Z;
public static BufferedImage background;
static {
try {
I = ImageIO.read(new File("images/I.png"));
J = ImageIO.read(new File("images/J.png"));
L = ImageIO.read(new File("images/L.png"));
O = ImageIO.read(new File("images/O.png"));
S = ImageIO.read(new File("images/S.png"));
T = ImageIO.read(new File("images/T.png"));
Z = ImageIO.read(new File("images/Z.png"));
background = ImageIO.read(new File("images/background.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null);
g.translate(22, 15);
paintWall(g);
paintCurrentOne(g);
paintNextOne(g);
paintSource(g);
paintState(g);
}
public void start() {
game_state = PLING;
KeyListener l = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_DOWN:
sortDropActive();
break;
case KeyEvent.VK_LEFT:
moveleftActive();
break;
case KeyEvent.VK_RIGHT:
moveRightActive();
break;
case KeyEvent.VK_UP:
rotateRightActive();
break;
case KeyEvent.VK_SPACE:
hadnDropActive();
break;
case KeyEvent.VK_P:
if (game_state == PLING) {
game_state = STOP;
}
break;
case KeyEvent.VK_C:
if (game_state == STOP) {
game_state = PLING;
}
break;
case KeyEvent.VK_S:
game_state = PLING;
wall = new Cell[18][9];
currentOne = Tetromino.randomOne();
nextOne = Tetromino.randomOne();
totalScore = 0;
totalLine = 0;
break;
}
}
};
this.addKeyListener(l);
this.requestFocus();
while (true) {
if (game_state == PLING) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (camDrop()) {
currentOne.moveDrop();
} else {
landToWall();
destroyLine();
if (isGameOver()) {
game_state = OVER;
} else {
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
}
repaint();
}
}
public void rotateRightActive() {
currentOne.rotateRight();
if (outOFBounds() || coincide()) {
currentOne.rotateLeft();
}
}
public void hadnDropActive() {
while (true) {
if (camDrop()) {
currentOne.moveDrop();
} else {
break;
}
}
landToWall();
destroyLine();
if (isGameOver()) {
game_state = OVER;
} else {
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
public void sortDropActive() {
if (camDrop()) {
currentOne.moveDrop();
} else {
landToWall();
destroyLine();
if (isGameOver()) {
game_state = OVER;
} else {
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
}
private void landToWall() {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
wall[row][col] = cell;
}
}
public boolean camDrop() {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
if (row == wall.length - 1) {
return false;
} else if (wall[row + 1][col] != null) {
return false;
}
}
return true;
}
public void destroyLine() {
int line = 0;
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
if (isFullLine(row)) {
line++;
for (int i = row; i > 0; i--) {
System.arraycopy(wall[i - 1], 0, wall[i], 0, wall[0].length);
}
wall[0] = new Cell[9];
}
}
totalScore += scores_pool[line];
totalLine += line;
}
public boolean isFullLine(int row) {
Cell[] cells = wall[row];
for (Cell cell : cells) {
if (cell == null) {
return false;
}
}
return true;
}
public boolean isGameOver() {
Cell[] cells = nextOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
if (wall[row][col] != null) {
return true;
}
}
return false;
}
private void paintState(Graphics g) {
if (game_state == PLING) {
g.drawString(show_state[PLING], 500, 660);
} else if (game_state == STOP) {
g.drawString(show_state[STOP], 500, 660);
} else {
g.drawString(show_state[OVER], 500, 660);
g.setColor(Color.RED);
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 60));
g.drawString("GAME OVER!", 30, 400);
}
}
private void paintSource(Graphics g) {
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 30));
g.drawString("分数: " + totalScore, 500, 250);
g.drawString("行数: " + totalLine, 500, 430);
}
private void paintNextOne(Graphics g) {
Cell[] cells = nextOne.cells;
for (Cell cell : cells) {
int x = cell.getCol() * CELL_SIZE + 370;
int y = cell.getRow() * CELL_SIZE + 25;
g.drawImage(cell.getImage(), x, y, null);
}
}
private void paintCurrentOne(Graphics g) {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int x = cell.getCol() * CELL_SIZE;
int y = cell.getRow() * CELL_SIZE;
g.drawImage(cell.getImage(), x, y, null);
}
}
private void paintWall(Graphics g) {
for (int i = 0; i < wall.length; i++) {
for (int j = 0; j < wall[i].length; j++) {
int x = j * CELL_SIZE;
int y = i * CELL_SIZE;
Cell cell = wall[i][j];
if (cell == null) {
g.drawRect(x, y, CELL_SIZE, CELL_SIZE);
} else {
g.drawImage(cell.getImage(), x, y, null);
}
}
}
}
public boolean outOFBounds() {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int col = cell.getCol();
int row = cell.getRow();
if (row < 0 || row > wall.length - 1 || col < 0 || col > wall[0].length-1) {
return true;
}
}
return false;
}
public void moveleftActive() {
currentOne.moveLeft();
if (outOFBounds() || coincide()) {
currentOne.moveRight();
}
}
public void moveRightActive() {
currentOne.moveRight();
if (outOFBounds() || coincide()) {
currentOne.moveLeft();
}
}
public boolean coincide() {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
if (wall[row][col] != null) {
return true;
}
}
return false;
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("俄罗斯方块");
Tetris panel = new Tetris();
jFrame.add(panel);
jFrame.setVisible(true);
jFrame.setSize(810, 940);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.start();
}
}