javaFX贪吃蛇代码eclipse带有注释
时间: 2023-07-09 18:50:35 浏览: 70
下面是一个简单的JavaFX贪吃蛇代码,附带有注释,供您参考:
```java
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SnakeGame extends Application {
private static final int TILE_SIZE = 20; // 每个方格的大小
private static final int WIDTH = 20; // 游戏界面的宽度(单位:方格)
private static final int HEIGHT = 20; // 游戏界面的高度(单位:方格)
private List<Point2D> snake = new ArrayList<>(); // 蛇的身体,用一个点的列表表示
private Point2D food; // 食物的位置,用一个点表示
private Direction direction = Direction.RIGHT; // 蛇移动的方向,默认向右移动
private boolean gameOver; // 游戏是否结束
private GraphicsContext graphicsContext; // 画布的上下文
// 游戏的主循环
private AnimationTimer gameLoop = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long now) {
if (lastUpdate == 0) {
lastUpdate = now;
return;
}
if (now - lastUpdate < 1000000000 / 8) {
return;
}
lastUpdate = now;
// 更新蛇的位置
updateSnake();
// 检查蛇是否吃到了食物
checkFood();
// 重新绘制画面
draw();
}
};
// 启动游戏
private void startGame() {
// 初始化蛇的位置
snake.clear();
snake.add(new Point2D(WIDTH / 2, HEIGHT / 2));
// 初始化食物的位置
food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT));
// 标记游戏未结束
gameOver = false;
// 启动游戏循环
gameLoop.start();
}
// 更新蛇的位置
private void updateSnake() {
// 根据移动的方向,计算蛇头的新位置
Point2D head = snake.get(0);
switch (direction) {
case UP:
head = new Point2D(head.getX(), head.getY() - 1);
break;
case DOWN:
head = new Point2D(head.getX(), head.getY() + 1);
break;
case LEFT:
head = new Point2D(head.getX() - 1, head.getY());
break;
case RIGHT:
head = new Point2D(head.getX() + 1, head.getY());
break;
}
// 将蛇头插入到列表的头部
snake.add(0, head);
// 如果蛇头吃到了食物,就不需要移动蛇尾,否则需要移动
if (head.equals(food)) {
// 生成新的食物位置
food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT));
} else {
// 移除蛇尾
snake.remove(snake.size() - 1);
}
// 检查蛇是否碰到了边界或者自身
if (head.getX() < 0 || head.getX() >= WIDTH || head.getY() < 0 || head.getY() >= HEIGHT) {
gameOver = true;
}
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
gameOver = true;
break;
}
}
}
// 检查蛇是否吃到了食物
private void checkFood() {
if (snake.get(0).equals(food)) {
food = new Point2D(new Random().nextInt(WIDTH), new Random().nextInt(HEIGHT));
}
}
// 重新绘制画面
private void draw() {
// 清空画布
graphicsContext.setFill(Color.WHITE);
graphicsContext.fillRect(0, 0, WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE);
// 绘制蛇的身体
graphicsContext.setFill(Color.GREEN);
for (Point2D point : snake) {
graphicsContext.fillRect(point.getX() * TILE_SIZE, point.getY() * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
// 绘制食物
graphicsContext.setFill(Color.RED);
graphicsContext.fillRect(food.getX() * TILE_SIZE, food.getY() * TILE_SIZE, TILE_SIZE, TILE_SIZE);
// 如果游戏结束,就在画布上显示“Game Over”
if (gameOver) {
graphicsContext.setFill(Color.BLACK);
graphicsContext.fillText("Game Over", WIDTH * TILE_SIZE / 2 - 30, HEIGHT * TILE_SIZE / 2);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
// 创建画布和画布的上下文
Canvas canvas = new Canvas(WIDTH * TILE_SIZE, HEIGHT * TILE_SIZE);
graphicsContext = canvas.getGraphicsContext2D();
// 创建场景和根节点
StackPane root = new StackPane();
root.getChildren().add(canvas);
Scene scene = new Scene(root);
// 处理键盘事件
scene.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.UP && direction != Direction.DOWN) {
direction = Direction.UP;
} else if (e.getCode() == KeyCode.DOWN && direction != Direction.UP) {
direction = Direction.DOWN;
} else if (e.getCode() == KeyCode.LEFT && direction != Direction.RIGHT) {
direction = Direction.LEFT;
} else if (e.getCode() == KeyCode.RIGHT && direction != Direction.LEFT) {
direction = Direction.RIGHT;
} else if (e.getCode() == KeyCode.SPACE && gameOver) {
startGame();
}
});
// 启动游戏
startGame();
// 显示场景
primaryStage.setScene(scene);
primaryStage.setTitle("Snake Game");
primaryStage.show();
}
// 枚举类型,表示蛇移动的方向
private enum Direction {
UP, DOWN, LEFT, RIGHT
}
}
```