我正在尝试制作蛇游戏。我可以将蛇显示为正方形的屏幕,但是现在我无法为蛇的移动动画。
这是我的移动方法:
public void move() {
// Updates the snake's tail using the snakeBody ArrayList.
if (tailLength > 0) {
for (int i = tailLength - 1; i > 0; i--) {
snakeBody.get(i).x = snakeBody.get(i - 1).x;
snakeBody.get(i).y = snakeBody.get(i - 1).y;
}
snakeBody.get(0).x = x;
snakeBody.get(0).y =y;
}
// Updates the snake's position based on its speed.
x = x + dx * START_SIZE;
y = y + dy * START_SIZE;
在我的控制器类中,我创建了一个动画方法:
public void animate() {
setFocusTraversable(true);
setOnKeyPressed(e -> control(e));
KeyFrame frame = new KeyFrame(Duration.millis(2000), e -> snake.move());
animation = new Timeline(frame);
animation.setCycleCount(Timeline.INDEFINITE);
animation.getKeyFrames().add(frame);
animation.play();
}
这是我的控制方法的样子:
public void control(KeyEvent event) {
event.getCode();
if ((event.getCode() == KeyCode.A || event.getCode() == KeyCode.LEFT)) {
snake.setMoving(Direction.LEFT);
}
else if ((event.getCode() == KeyCode.D || event.getCode() == KeyCode.RIGHT)) {
snake.setMoving(Direction.RIGHT);
}
else if ((event.getCode() == KeyCode.W || event.getCode() == KeyCode.UP)) {
snake.setMoving(Direction.UP);
}
else if ((event.getCode() == KeyCode.S || event.getCode() == KeyCode.DOWN)) {
snake.setMoving(Direction.DOWN);
}
else if (event.getCode() == KeyCode.SPACE) {
if (over) {
animation.stop();
} else {
animation.pause();
paused = !paused;
}
}
}
我不确定如何在游戏屏幕上制作一系列正方形动画