用javafx和GUI的知识写一个俄罗斯方块小程序,要求有代码
时间: 2024-02-27 19:54:47 浏览: 74
俄罗斯方块java小程序
4星 · 用户满意度95%
好的,下面是一个简单的俄罗斯方块小程序的代码示例,使用JavaFX和GUI实现:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Tetris extends Application {
private static final int BLOCK_SIZE = 30; // 方块大小
private static final int WIDTH = 10; // 游戏区域宽度
private static final int HEIGHT = 20; // 游戏区域高度
private int[][] board = new int[WIDTH][HEIGHT]; // 游戏区域
private int[][][] blocks = new int[][][] { // 方块形状
// I
{{1, 1, 1, 1}},
// J
{{1, 1, 1}, {0, 0, 1}},
// L
{{1, 1, 1}, {1, 0, 0}},
// O
{{1, 1}, {1, 1}},
// S
{{0, 1, 1}, {1, 1, 0}},
// T
{{1, 1, 1}, {0, 1, 0}},
// Z
{{1, 1, 0}, {0, 1, 1}}
};
private int currentBlockType; // 当前方块类型
private int currentBlockX; // 当前方块x坐标
private int currentBlockY; // 当前方块y坐标
private int currentRotation; // 当前方块旋转角度
private int score; // 得分
private Canvas canvas;
private Label scoreLabel;
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
canvas = new Canvas(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
root.setCenter(canvas);
scoreLabel = new Label("Score: 0");
root.setBottom(scoreLabel);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Tetris");
primaryStage.show();
new Thread(() -> {
while (true) {
update();
draw();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void update() {
if (currentBlockType == 0) {
newBlock();
} else {
// 检查能否下落
if (canMove(currentBlockType, currentBlockX, currentBlockY + 1, currentRotation)) {
currentBlockY++;
} else {
// 固定方块
fixBlock();
// 消除满行
clearLines();
// 新方块
newBlock();
}
}
}
private void draw() {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
// 绘制游戏区域
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
if (board[x][y] != 0) {
gc.setFill(Color.BLACK);
gc.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
// 绘制当前方块
int[][] block = blocks[currentBlockType][currentRotation];
for (int x = 0; x < block.length; x++) {
for (int y = 0; y < block[x].length; y++) {
if (block[x][y] != 0) {
gc.setFill(Color.RED);
gc.fillRect((currentBlockX + x) * BLOCK_SIZE, (currentBlockY + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
// 更新得分
scoreLabel.setText("Score: " + score);
}
private void newBlock() {
currentBlockType = (int) (Math.random() * blocks.length);
currentBlockX = WIDTH / 2 - blocks[currentBlockType][0].length / 2;
currentBlockY = 0;
currentRotation = 0;
}
private boolean canMove(int blockType, int x, int y, int rotation) {
int[][] block = blocks[blockType][rotation];
for (int i = 0; i < block.length; i++) {
for (int j = 0; j < block[i].length; j++) {
if (block[i][j] != 0) {
int px = x + i;
int py = y + j;
if (px < 0 || px >= WIDTH || py < 0 || py >= HEIGHT || board[px][py] != 0) {
return false;
}
}
}
}
return true;
}
private void fixBlock() {
int[][] block = blocks[currentBlockType][currentRotation];
for (int i = 0; i < block.length; i++) {
for (int j = 0; j < block[i].length; j++) {
if (block[i][j] != 0) {
int x = currentBlockX + i;
int y = currentBlockY + j;
board[x][y] = currentBlockType + 1;
}
}
}
}
private void clearLines() {
int lines = 0;
for (int y = 0; y < HEIGHT; y++) {
boolean full = true;
for (int x = 0; x < WIDTH; x++) {
if (board[x][y] == 0) {
full = false;
break;
}
}
if (full) {
for (int i = y; i > 0; i--) {
for (int x = 0; x < WIDTH; x++) {
board[x][i] = board[x][i - 1];
}
}
for (int x = 0; x < WIDTH; x++) {
board[x][0] = 0;
}
lines++;
y--;
}
}
score += lines * lines;
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序实现了一个简单的俄罗斯方块游戏,包括显示游戏区域、绘制方块、检测碰撞、消除行、更新得分等功能。你可以在JavaFX的基础上进行进一步的扩展和优化。
阅读全文