javafx贪吃蛇代码eclipse
时间: 2023-07-09 15:50:34 浏览: 54
以下是一个简单的 JavaFX 贪吃蛇游戏的代码示例,可以在 Eclipse IDE 中使用:
Snake.java:
```java
import java.util.ArrayList;
import java.util.List;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
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 javafx.util.Duration;
public class Snake extends Application {
private static final int BLOCK_SIZE = 20;
private static final int WIDTH = 20 * BLOCK_SIZE;
private static final int HEIGHT = 15 * BLOCK_SIZE;
private List<Block> snake = new ArrayList<>();
private int foodX, foodY;
private Direction direction = Direction.RIGHT;
private boolean gameOver = false;
private GraphicsContext gc;
@Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.GREEN);
StackPane root = new StackPane();
root.getChildren().add(canvas);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, WIDTH, HEIGHT);
scene.setOnKeyPressed(e -> {
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.UP && direction != Direction.DOWN) {
direction = Direction.UP;
} else if (e.getCode() == KeyCode.DOWN && direction != Direction.UP) {
direction = Direction.DOWN;
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Snake");
primaryStage.show();
startGame();
}
private void startGame() {
snake.add(new Block(10, 10));
snake.add(new Block(10, 9));
snake.add(new Block(10, 8));
createFood();
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(100), e -> {
gc.clearRect(0, 0, WIDTH, HEIGHT);
if (gameOver) {
gc.setFill(Color.RED);
gc.fillText("Game Over", WIDTH / 2 - 50, HEIGHT / 2);
return;
}
moveSnake();
drawSnake();
drawFood();
checkCollision();
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
private void moveSnake() {
for (int i = snake.size() - 1; i > 0; i--) {
Block prev = snake.get(i - 1);
Block curr = snake.get(i);
curr.setX(prev.getX());
curr.setY(prev.getY());
}
Block head = snake.get(0);
switch (direction) {
case LEFT:
head.decrementX();
break;
case RIGHT:
head.incrementX();
break;
case UP:
head.decrementY();
break;
case DOWN:
head.incrementY();
break;
}
}
private void drawSnake() {
gc.setFill(Color.GREEN);
for (Block block : snake) {
gc.fillRect(block.getX() * BLOCK_SIZE, block.getY() * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
private void createFood() {
foodX = (int) (Math.random() * (WIDTH / BLOCK_SIZE));
foodY = (int) (Math.random() * (HEIGHT / BLOCK_SIZE));
}
private void drawFood() {
gc.setFill(Color.RED);
gc.fillRect(foodX * BLOCK_SIZE, foodY * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
private void checkCollision() {
Block head = snake.get(0);
// Check if snake has hit the wall
if (head.getX() < 0 || head.getX() >= WIDTH / BLOCK_SIZE || head.getY() < 0 || head.getY() >= HEIGHT / BLOCK_SIZE) {
gameOver = true;
return;
}
// Check if snake has hit itself
for (int i = 1; i < snake.size(); i++) {
if (head.getX() == snake.get(i).getX() && head.getY() == snake.get(i).getY()) {
gameOver = true;
return;
}
}
// Check if snake has eaten the food
if (head.getX() == foodX && head.getY() == foodY) {
snake.add(new Block(-1, -1));
createFood();
}
}
private enum Direction {
LEFT, RIGHT, UP, DOWN
}
private class Block {
private int x;
private int y;
public Block(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void incrementX() {
x++;
}
public void decrementX() {
x--;
}
public void incrementY() {
y++;
}
public void decrementY() {
y--;
}
}
public static void main(String[] args) {
launch(args);
}
}
```
运行此代码将启动一个简单的贪吃蛇游戏窗口。注意,此代码未经过优化,仅用于演示。