请给出javafx中贪吃蛇代码
时间: 2023-11-09 15:05:52 浏览: 98
下面是一个简单的 JavaFX 贪吃蛇游戏示例代码:
```java
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SnakeGame extends Application {
private static final int BLOCK_SIZE = 10;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private int direction = KeyCode.RIGHT.getCode();
private boolean gameOver = false;
private Timeline timeline = new Timeline();
private Canvas canvas = new Canvas(WIDTH, HEIGHT);
private int foodX = (int) (Math.random() * (WIDTH / BLOCK_SIZE)) * BLOCK_SIZE;
private int foodY = (int) (Math.random() * (HEIGHT / BLOCK_SIZE)) * BLOCK_SIZE;
private int snakeLength = 3;
private int[] snakeX = new int[WIDTH * HEIGHT / BLOCK_SIZE / BLOCK_SIZE];
private int[] snakeY = new int[WIDTH * HEIGHT / BLOCK_SIZE / BLOCK_SIZE];
@Override
public void start(Stage primaryStage) {
Group root = new Group();
root.getChildren().add(canvas);
Scene scene = new Scene(root);
scene.setOnKeyPressed(event -> {
if (event.getCode().isArrowKey() && Math.abs(direction - event.getCode().getCode()) != 2) {
direction = event.getCode().getCode();
}
});
primaryStage.setScene(scene);
primaryStage.show();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(100), event -> {
if (gameOver) {
timeline.stop();
return;
}
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, WIDTH, HEIGHT);
gc.setFill(Color.RED);
gc.fillRect(foodX, foodY, BLOCK_SIZE, BLOCK_SIZE);
int dx = 0, dy = 0;
switch (direction) {
case KeyCode.LEFT:
dx = -BLOCK_SIZE;
break;
case KeyCode.RIGHT:
dx = BLOCK_SIZE;
break;
case KeyCode.UP:
dy = -BLOCK_SIZE;
break;
case KeyCode.DOWN:
dy = BLOCK_SIZE;
break;
}
int headX = snakeX[0] + dx;
int headY = snakeY[0] + dy;
if (headX < 0 || headX >= WIDTH || headY < 0 || headY >= HEIGHT) {
gameOver = true;
return;
}
for (int i = 1; i < snakeLength; i++) {
if (headX == snakeX[i] && headY == snakeY[i]) {
gameOver = true;
return;
}
}
if (headX == foodX && headY == foodY) {
foodX = (int) (Math.random() * (WIDTH / BLOCK_SIZE)) * BLOCK_SIZE;
foodY = (int) (Math.random() * (HEIGHT / BLOCK_SIZE)) * BLOCK_SIZE;
snakeLength++;
}
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] = headX;
snakeY[0] = headY;
gc.setFill(Color.LIME);
for (int i = 0; i < snakeLength; i++) {
gc.fillRect(snakeX[i], snakeY[i], BLOCK_SIZE, BLOCK_SIZE);
}
}));
timeline.play();
snakeX[0] = WIDTH / 2;
snakeY[0] = HEIGHT / 2;
}
public static void main(String[] args) {
launch(args);
}
}
```
该代码基于 JavaFX 平台,使用了画布(Canvas)实现游戏的绘制和键盘事件处理。游戏中的蛇是由一个数组表示的,每个数组元素代表蛇的一个身体部分的坐标。游戏循环使用 Timeline 实现,每个循环周期绘制一次游戏画面。在游戏画面绘制过程中,会根据玩家按键的方向更新蛇的位置,同时判断蛇是否吃到了食物,蛇是否碰到了墙壁或者自己的身体,以决定游戏是否结束。
阅读全文