检测到碰撞后重新定位图像

时间:2019-06-25 23:09:42

标签: javafx

我已经弄清楚了如何检测矩形和图像的冲突,但是我遇到的问题是当我调用该方法来重新定位图像时,它连续连续执行10次,然后将不再起作用。

我正在用JavaFX开发我的第一款游戏。我正在尝试制作基本的蛇游戏,但一直无法弄清蛇与它碰撞后重新放置食物的问题。

public class Main extends Application {

    Stage window;
    Scene mainScene;

    private final int WIDTH = 500;
    private final int HEIGHT = 500;

    Timeline timeline;

    private Direction action = Direction.RIGHT;

    private Rectangle snakeHead;
    private final int speed = 3;
    private int xSpeed = 3;
    private int ySpeed = 3;

    private int snakeW = 20;
    private int snakeH = 20;
    private BoundingBox snakeBox;

    private BoundingBox foodBox;

    private ImageView food;
    private Random rand = new Random();

    private double foodX;
    private double foodY;

    enum Direction {
        LEFT,RIGHT,UP,DOWN;
    }

    private Parent createContent(){
        Pane root = new Pane();

        //food = new Food();
        food = new ImageView(new Image("resources/apple.png"));
        food.setFitHeight(25);
        food.setFitWidth(25);
        food.setPreserveRatio(true);
        newFood();
        foodBox = new BoundingBox(foodX,foodY,20,20);


        snakeHead = new Rectangle(snakeW,snakeH);
        snakeHead.setTranslateX(200);
        snakeHead.setTranslateY(200);

        snakeBox = new BoundingBox(snakeHead.getTranslateX(),snakeHead.getTranslateY(),snakeW,snakeH);

        timeline = new Timeline(new KeyFrame(Duration.millis(16), e-> {
            //Snake movement
            if(action == Direction.LEFT) {snakeHead.setTranslateX(snakeHead.getTranslateX() - xSpeed);}
            if(action == Direction.RIGHT) {snakeHead.setTranslateX(snakeHead.getTranslateX() + xSpeed);}
            if(action == Direction.UP) {snakeHead.setTranslateY(snakeHead.getTranslateY() - ySpeed);}
            if(action == Direction.DOWN) {snakeHead.setTranslateY(snakeHead.getTranslateY() + ySpeed);}

            //Stops snake at edges of screen
            if(snakeHead.getTranslateX() <= 0){
                xSpeed = 0;
                if(action == Direction.RIGHT){xSpeed = speed;}
            }
            if(snakeHead.getTranslateX() >= WIDTH - snakeW){
                xSpeed = 0;
                if(action == Direction.LEFT){xSpeed = speed;}
            }
            if(snakeHead.getTranslateY() <= 0){
                ySpeed = 0;
                if(action == Direction.DOWN){ySpeed = speed;}
            }
            if(snakeHead.getTranslateY() >= HEIGHT - snakeH){
                ySpeed = 0;
                if(action == Direction.UP){ySpeed = speed;}
            }

            //TODO: Detect Collisions
                     if(foodBox.intersects(snakeHead.getTranslateX(),snakeHead.getTranslateY (),snakeW,snakeH)){
                newFood();
                System.out.println("Collision");
            }

        }));



        timeline.setCycleCount(Timeline.INDEFINITE);

        root.getChildren().addAll(snakeHead,food);

        return root;
    }

    private void newFood() {
        foodX = rand.nextInt(500);
        foodY = rand.nextInt(500);

        food.setTranslateX(foodX);
        food.setTranslateY(foodY);

        System.out.println("X " + foodX);
        System.out.println("Y " + foodY);
    }

    private void startGame() {
        timeline.play();
    }

    private void stopGame() {
        timeline.stop();
    }


    @Override
    public void start(Stage primaryStage) throws Exception{
        window = primaryStage;
        mainScene = new Scene(createContent(),WIDTH,HEIGHT);

        mainScene.setOnKeyPressed(e-> {
            switch(e.getCode()) {
                case UP: action = Direction.UP; break;
                case DOWN: action = Direction.DOWN; break;
                case LEFT: action = Direction.LEFT; break;
                case RIGHT: action = Direction.RIGHT; break;
            }
        });

        window.setTitle("Snake");
        window.setResizable(false);
        window.setScene(mainScene);
        window.show();

        startGame();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我要寻找的是矩形碰到重新定位的苹果时。我已经为此苦苦挣扎了一段时间,不知道该怎么办。我还是一个相当新的程序员。

1 个答案:

答案 0 :(得分:1)

您可以检查蛇与食物的boundsInParent的交集。

if(food.getBoundsInParent().intersects(snakeHead.getBoundsInParent())){
    newFood();
    System.out.println("Collision");
}