java代码实现俄罗斯方块
时间: 2023-09-22 07:15:32 浏览: 43
好的,以下是一个简单的 Java 代码实现俄罗斯方块游戏:
```java
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Tetris {
private static final int ROWS = 20;
private static final int COLS = 10;
private static final int[][][] SHAPES = {
{{1, 1, 1, 1}}, // I
{{1, 1, 0}, {0, 1, 1}}, // Z
{{0, 1, 1}, {1, 1, 0}}, // S
{{1, 1, 1}, {0, 1, 0}}, // T
{{1, 1, 0}, {1, 1, 0}}, // O
{{1, 0, 0}, {1, 1, 1}}, // L
{{0, 0, 1}, {1, 1, 1}} // J
};
private static final int[] SCORES = {0, 10, 30, 50, 80};
private static int[][] board = new int[ROWS][COLS];
private static int score = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int[][] shape = SHAPES[random.nextInt(SHAPES.length)];
int row = 0, col = COLS / 2 - shape[0].length / 2;
while (true) {
if (isGameover(shape, row, col)) {
System.out.println("Game over!");
break;
}
if (isTouchBottom(shape, row)) {
addShapeToBoard(shape, row, col);
int lines = removeFullLines();
score += SCORES[lines];
System.out.println("Score: " + score);
shape = SHAPES[random.nextInt(SHAPES.length)];
row = 0;
col = COLS / 2 - shape[0].length / 2;
}
printBoard(shape, row, col);
System.out.print("Input a command (L/R/D): ");
String command = scanner.nextLine();
switch (command.toUpperCase()) {
case "L":
if (!isTouchLeft(shape, row, col)) {
col--;
}
break;
case "R":
if (!isTouchRight(shape, row, col)) {
col++;
}
break;
case "D":
if (!isTouchBottom(shape, row)) {
row++;
}
break;
default:
break;
}
}
}
private static boolean isGameover(int[][] shape, int row, int col) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
if (shape[i][j] == 1 && board[row + i][col + j] == 1) {
return true;
}
}
}
return false;
}
private static boolean isTouchBottom(int[][] shape, int row) {
return row + shape.length == ROWS || isTouchBoardBottom(shape, row);
}
private static boolean isTouchLeft(int[][] shape, int row, int col) {
return col == 0 || isTouchBoardLeft(shape, row, col);
}
private static boolean isTouchRight(int[][] shape, int row, int col) {
return col + shape[0].length == COLS || isTouchBoardRight(shape, row, col);
}
private static boolean isTouchBoardBottom(int[][] shape, int row) {
for (int i = shape.length - 1; i >= 0; i--) {
for (int j = 0; j < shape[i].length; j++) {
if (shape[i][j] == 1 && board[row + i + 1][j] == 1) {
return true;
}
}
}
return false;
}
private static boolean isTouchBoardLeft(int[][] shape, int row, int col) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
if (shape[i][j] == 1 && board[row + i][col + j - 1] == 1) {
return true;
}
}
}
return false;
}
private static boolean isTouchBoardRight(int[][] shape, int row, int col) {
for (int i = 0; i < shape.length; i++) {
for (int j = shape[i].length - 1; j >= 0; j--) {
if (shape[i][j] == 1 && board[row + i][col + j + 1] == 1) {
return true;
}
}
}
return false;
}
private static void addShapeToBoard(int[][] shape, int row, int col) {
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
if (shape[i][j] == 1) {
board[row + i][col + j] = 1;
}
}
}
}
private static int removeFullLines() {
int count = 0;
for (int i = board.length - 1; i >= 0; i--) {
if (isFullLine(board[i])) {
count++;
for (int j = i; j > 0; j--) {
board[j] = board[j - 1];
}
Arrays.fill(board[0], 0);
}
}
return count;
}
private static boolean isFullLine(int[] line) {
for (int i = 0; i < line.length; i++) {
if (line[i] == 0) {
return false;
}
}
return true;
}
private static void printBoard(int[][] shape, int row, int col) {
int[][] boardWithShape = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
boardWithShape[i][j] = board[i][j];
}
}
for (int i = 0; i < shape.length; i++) {
for (int j = 0; j < shape[i].length; j++) {
if (shape[i][j] == 1) {
boardWithShape[row + i][col + j] = 1;
}
}
}
for (int i = 0; i < boardWithShape.length; i++) {
for (int j = 0; j < boardWithShape[i].length; j++) {
System.out.print(boardWithShape[i][j] == 1 ? "#" : ".");
}
System.out.println();
}
}
}
```
在这个实现中,我们使用一个二维数组 `board` 来表示游戏界面,其中 0 表示一个空格,1 表示一个方块。我们使用一个三维数组 `SHAPES` 来存储所有可能出现的方块形状,每个方块是一个二维数组。我们使用一个一维数组 `SCORES` 来存储消除行数对应的得分,即消除 0 行得 0 分,消除 1 行得 10 分,消除 2 行得 30 分,消除 3 行得 50 分,消除 4 行得 80 分。
在 `main` 方法中,我们首先通过 `Scanner` 和 `Random` 类来读取用户输入和生成随机方块。然后我们进入一个死循环,每次循环中首先判断游戏是否结束,如果结束则输出游戏结束信息并退出循环。然后判断当前方块是否触底,如果触底则将方块加入到游戏界面中并计算得分,然后生成新的方块。接着打印游戏界面并提示用户输入命令,根据用户的命令来移动方块。循环结束后,输出最终得分。
在 `isGameover` 方法中,我们判断当前方块是否和游戏界面中的方块重叠,如果重叠则游戏结束。
在 `isTouchBottom`、`isTouchLeft` 和 `isTouchRight` 方法中,我们分别判断当前方块是否触底、触左边界和触右边界,以便在移动方块时判断是否合法。
在 `isTouchBoardBottom`、`isTouchBoardLeft` 和 `isTouchBoardRight` 方法中,我们分别判断当前方块是否和游戏界面中的方块重叠,以便在移动方块时判断是否合法。
在 `addShapeToBoard` 方法中,我们将当前方块加入到游戏界面中。
在 `removeFullLines` 方法中,我们遍历游戏界面中的每一行,如果发现某一行已经满了,则将它删除并在顶部添加一行空白行,直到没有满行为止。同时我们统计一下删除的行数,以便计算得分。
在 `isFullLine` 方法中,我们判断一行是否已经满了。
在 `printBoard` 方法中,我们将游戏界面和当前方块打印出来,用 `#` 表示方块,用 `.` 表示空格。