javafx贪吃蛇代码
时间: 2023-07-03 16:29:45 浏览: 105
贪吃蛇 java代码
5星 · 资源好评率100%
以下是一个简单的JavaFX贪吃蛇游戏代码示例:
Snake.java:
```java
import java.util.LinkedList;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Snake {
private LinkedList<Rectangle> body;
private int direction;
private int speed;
private int length;
private Color color;
public Snake(int x, int y, int direction, int speed, int length, Color color) {
this.body = new LinkedList<>();
this.body.add(new Rectangle(x, y, 20, 20));
this.direction = direction;
this.speed = speed;
this.length = length;
this.color = color;
this.body.get(0).setFill(color);
}
public void move() {
Rectangle head = this.body.getFirst();
Rectangle tail = this.body.removeLast();
tail.setX(head.getX());
tail.setY(head.getY());
switch (this.direction) {
case 0:
tail.setX(tail.getX() - 20);
break;
case 1:
tail.setX(tail.getX() + 20);
break;
case 2:
tail.setY(tail.getY() - 20);
break;
case 3:
tail.setY(tail.getY() + 20);
break;
}
this.body.addFirst(tail);
}
public boolean eatFood(Food food) {
if (this.body.getFirst().getBoundsInParent().intersects(food.getBoundsInParent())) {
this.length++;
Rectangle newBody = new Rectangle(this.body.getLast().getX(), this.body.getLast().getY(), 20, 20);
newBody.setFill(this.color);
this.body.addLast(newBody);
return true;
}
return false;
}
public boolean checkCollision() {
Rectangle head = this.body.getFirst();
for (int i = 1; i < this.body.size(); i++) {
if (head.getBoundsInParent().intersects(this.body.get(i).getBoundsInParent())) {
return true;
}
}
return head.getX() < 0 || head.getX() > 580 || head.getY() < 0 || head.getY() > 380;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getLength() {
return this.length;
}
public LinkedList<Rectangle> getBody() {
return this.body;
}
}
```
Food.java:
```java
import java.util.Random;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Food extends Rectangle {
private int score;
public Food(int x, int y, int score) {
super(x, y, 20, 20);
this.score = score;
this.setFill(Color.RED);
}
public void respawn() {
Random random = new Random();
int x = random.nextInt(29) * 20;
int y = random.nextInt(19) * 20;
this.setX(x);
this.setY(y);
}
public int getScore() {
return this.score;
}
}
```
Main.java:
```java
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private static final int SCORE_X = 10;
private static final int SCORE_Y = 20;
private static final int SPEED = 5;
private static final int LENGTH = 3;
private Pane root;
private Snake snake;
private Food food;
private Label scoreLabel;
private int score;
private boolean gameOver;
@Override
public void start(Stage primaryStage) {
root = new Pane();
Scene scene = new Scene(root, WIDTH, HEIGHT);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Snake Game");
snake = new Snake(WIDTH / 2, HEIGHT / 2, 1, SPEED, LENGTH, Color.GREEN);
food = new Food(0, 0, 10);
food.respawn();
score = 0;
scoreLabel = new Label("Score: " + score);
scoreLabel.setLayoutX(SCORE_X);
scoreLabel.setLayoutY(SCORE_Y);
root.getChildren().addAll(snake.getBody());
root.getChildren().add(food);
root.getChildren().add(scoreLabel);
scene.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.LEFT && snake.getDirection() != 1) {
snake.setDirection(0);
} else if (event.getCode() == KeyCode.RIGHT && snake.getDirection() != 0) {
snake.setDirection(1);
} else if (event.getCode() == KeyCode.UP && snake.getDirection() != 3) {
snake.setDirection(2);
} else if (event.getCode() == KeyCode.DOWN && snake.getDirection() != 2) {
snake.setDirection(3);
}
});
AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long now) {
if (now - lastUpdate >= 1000000000 / snake.getSpeed()) {
snake.move();
if (snake.checkCollision()) {
gameOver = true;
stop();
}
if (snake.eatFood(food)) {
score += food.getScore();
scoreLabel.setText("Score: " + score);
food.respawn();
}
lastUpdate = now;
}
}
};
timer.start();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
这段代码实现了一个简单的JavaFX贪吃蛇游戏,包括蛇的移动、吃食物、碰撞检测等基本功能。可以根据个人需求进行修改和扩展。
阅读全文