请用javafx写一段贪吃蛇代码,要求能暂停、重启和退出,并通过按钮调整游戏难度
时间: 2023-07-15 17:11:48 浏览: 68
以下是一份简单的贪吃蛇游戏代码,使用JavaFX实现,支持暂停、重启和退出,并可通过按钮调整游戏难度。
```java
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
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 WIDTH = 600;
private static final int HEIGHT = 600;
private static final int CELL_SIZE = 20;
private static final int SNAKE_INIT_LENGTH = 3;
private static final int FOOD_COUNT = 2;
private static final int[] DELTAS = {-1, 0, 1, 0, -1};
private Canvas canvas;
private GraphicsContext gc;
private List<Point> snake;
private List<Point> food;
private AnimationTimer timer;
private boolean isPaused;
private boolean isOver;
private int direction;
private int score;
private int speed;
@Override
public void start(Stage primaryStage) throws Exception {
canvas = new Canvas(WIDTH, HEIGHT);
gc = canvas.getGraphicsContext2D();
snake = new ArrayList<>();
food = new ArrayList<>();
isPaused = false;
isOver = false;
direction = 0;
score = 0;
speed = 6;
BorderPane root = new BorderPane(canvas);
Button pauseButton = new Button("Pause");
Button restartButton = new Button("Restart");
Button quitButton = new Button("Quit");
Button speedButton = new Button("Speed: " + speed);
pauseButton.setOnAction(event -> isPaused = !isPaused);
restartButton.setOnAction(event -> restart());
quitButton.setOnAction(event -> System.exit(0));
speedButton.setOnAction(event -> {
speed = speed % 10 + 1;
speedButton.setText("Speed: " + speed);
});
root.setTop(pauseButton);
root.setLeft(restartButton);
root.setRight(quitButton);
root.setBottom(speedButton);
Scene scene = new Scene(root);
scene.setOnKeyPressed(event -> {
switch (event.getCode()) {
case UP:
if (direction != 2) direction = 0;
break;
case RIGHT:
if (direction != 3) direction = 1;
break;
case DOWN:
if (direction != 0) direction = 2;
break;
case LEFT:
if (direction != 1) direction = 3;
break;
case P:
isPaused = !isPaused;
break;
case R:
restart();
break;
case Q:
System.exit(0);
break;
case S:
speed = speed % 10 + 1;
speedButton.setText("Speed: " + speed);
break;
default:
break;
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Snake Game");
primaryStage.show();
restart();
timer = new AnimationTimer() {
private long lastTime = 0;
@Override
public void handle(long now) {
if (isPaused || isOver) return;
if (now - lastTime < 1000000000 / speed) return;
lastTime = now;
update();
draw();
}
};
timer.start();
}
private void restart() {
snake.clear();
food.clear();
for (int i = 0; i < SNAKE_INIT_LENGTH; i++) {
snake.add(new Point(WIDTH / 2, HEIGHT / 2 + i * CELL_SIZE));
}
for (int i = 0; i < FOOD_COUNT; i++) {
spawnFood();
}
direction = 0;
score = 0;
isPaused = false;
isOver = false;
}
private void spawnFood() {
Random random = new Random();
int x = random.nextInt(WIDTH / CELL_SIZE) * CELL_SIZE;
int y = random.nextInt(HEIGHT / CELL_SIZE) * CELL_SIZE;
Point p = new Point(x, y);
if (snake.contains(p) || food.contains(p)) {
spawnFood();
} else {
food.add(p);
}
}
private void update() {
Point head = snake.get(0);
Point newHead = new Point(head.x + DELTAS[direction] * CELL_SIZE,
head.y + DELTAS[direction + 1] * CELL_SIZE);
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT
|| snake.contains(newHead)) {
isOver = true;
return;
}
snake.add(0, newHead);
if (food.contains(newHead)) {
food.remove(newHead);
spawnFood();
score++;
} else {
snake.remove(snake.size() - 1);
}
}
private void draw() {
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, WIDTH, HEIGHT);
gc.setFill(Color.BLACK);
gc.fillText("Score: " + score, 20, 20);
gc.setStroke(Color.BLACK);
gc.strokeRect(0, 0, WIDTH, HEIGHT);
gc.setFill(Color.GREEN);
for (Point p : snake) {
gc.fillRect(p.x, p.y, CELL_SIZE, CELL_SIZE);
}
gc.setFill(Color.RED);
for (Point p : food) {
gc.fillOval(p.x + CELL_SIZE / 4, p.y + CELL_SIZE / 4,
CELL_SIZE / 2, CELL_SIZE / 2);
}
if (isPaused) {
gc.setFill(Color.BLACK);
gc.fillText("PAUSED", WIDTH / 2 - 30, HEIGHT / 2);
}
if (isOver) {
gc.setFill(Color.BLACK);
gc.fillText("GAME OVER", WIDTH / 2 - 40, HEIGHT / 2);
}
}
private static class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Point)) return false;
Point p = (Point) obj;
return x == p.x && y == p.y;
}
}
public static void main(String[] args) {
launch(args);
}
}
```
该代码使用JavaFX的Canvas和GraphicsContext实现绘制,通过AnimationTimer实现游戏循环。游戏逻辑包括蛇的移动、食物的生成和吃掉、游戏结束等。同时,通过按钮和键盘事件处理实现暂停、重启、退出和调整游戏难度等功能。
阅读全文