我正在尝试使用Eclipse中的Scene Builder创建Snake克隆。我有GUI的起始页面,单击“开始”后会弹出游戏“面板”。我的问题是,当我这样做时,蛇不会改变方向。另外,如果我将蛇的头部向上移动,那会不会使他的整个身体向上移动?
主要:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
Parent root = FXMLLoader.load(getClass().getResource("SnakeGame.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Snake");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package application;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class Controller {
static int width = 20;
static int height = 20;
static Dir direction = Dir.left;
static int cornersize = 25;
static int speed = 5;
static boolean gameOver = false;
static List<Corner> snake = new ArrayList<>();
static int foodX = 0;
static int foodY = 0;
static int foodcolor = 0;
public enum Dir {
left, right, up, down;
}
public static class Corner {
int x;
int y;
public Corner(int x, int y) {
this.x = x;
this.y = y;
}
}
@FXML
private VBox gamePane;
@FXML
void startButtonPressed(ActionEvent event) {
gamePane.setVisible(true);
// newFood();
Canvas c = new Canvas(600, 600);
GraphicsContext gc = c.getGraphicsContext2D();
gamePane.getChildren().add(c);
new AnimationTimer() {
long lastTick = 0;
public void handle(long now) {
if (lastTick == 0) {
lastTick = now;
tick(gc);
return;
}
if (now - lastTick > 1000000000 / speed) {
lastTick = now;
tick(gc);
}
}
}.start();
// Scene scene = new Scene(gamePane, 600, 600);
gamePane.addEventFilter(KeyEvent.ANY, key -> {
if (key.getCode() == KeyCode.W) {
direction = Dir.up;
}
if (key.getCode() == KeyCode.A) {
direction = Dir.left;
}
if (key.getCode() == KeyCode.S) {
direction = Dir.down;
}
if (key.getCode() == KeyCode.D) {
direction = Dir.right;
}
});
// add start snake parts
snake.add(new Corner(width / 2, height / 2));
snake.add(new Corner(width / 2, height / 2));
snake.add(new Corner(width / 2, height / 2));
// stage.setScene(scene);
// stage.setTitle("Good luck!");
// stage.show();
}
public static void tick(GraphicsContext gc) {
if (gameOver) {
gc.setFill(Color.RED);
gc.setFont(new Font("", 70));
gc.fillText("GAME OVER", 100, 300);
return;
}
for (int i = snake.size() - 1; i >= 1; i--) {
snake.get(i).x = snake.get(i - 1).x;
snake.get(i).y = snake.get(i - 1).y;
}
switch (direction) {
case up:
snake.get(0).y--;
if (snake.get(0).y < 0) {
gameOver = true;
}
break;
case down:
snake.get(0).y++;
if (snake.get(0).y > height) {
gameOver = true;
}
break;
case left:
snake.get(0).x--;
if (snake.get(0).x < 0) {
gameOver = true;
}
break;
case right:
snake.get(0).x++;
if (snake.get(0).x > width) {
gameOver = true;
}
break;
}
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, 600, 600);
// score
gc.setFill(Color.WHITE);
gc.setFont(new Font("", 30));
gc.fillText("Score: " + (speed - 5), 10, 30);
// random foodcolor
Color cc = Color.WHITE;
switch (foodcolor) {
case 0:
cc = Color.PURPLE;
break;
case 1:
cc = Color.LIGHTBLUE;
break;
case 2:
cc = Color.YELLOW;
break;
case 3:
cc = Color.PINK;
break;
case 4:
cc = Color.ORANGE;
break;
case 5:
cc = Color.AQUA;
}
gc.setFill(cc);
gc.fillOval(foodX * cornersize, foodY * cornersize, cornersize, cornersize);
// snake
for (Corner c : snake) {
gc.setFill(Color.YELLOWGREEN);
gc.fillRect(c.x * cornersize, c.y * cornersize, cornersize - 2, cornersize - 2);
}
}
@FXML
void exitButtonPressed(ActionEvent event) {
Platform.exit();
System.exit(0);
}
}