我的javafx游戏的问题

时间:2017-04-20 16:13:41

标签: java javafx logic

我现在有两个问题。最大的问题是左上角的游戏部分没有移动,而右下角则是。接下来的问题是我希望这些碎片能够在选定的瓷砖上卡入到位。任何帮助将不胜感激,谢谢!

 package test;

import javafx.application.*;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Board extends Application {


Pane player1 = new Pane();
Pane player2 = new Pane();
TilePane tp = new TilePane();
final int TILE_SIZE = 50;
int[][] logic = {{1, 1, 1, 1, 0, 0, 0, 0,
                  1, 1, 1, 0, 0, 0, 0, 0,
                  1, 1, 0, 0, 0, 0, 0, 0,
                  1, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 2,
                  0, 0, 0, 0, 0, 0, 2, 2,
                  0, 0, 0, 0, 0, 2, 2, 2,
                  0, 0, 0, 0, 2, 2, 2, 2}};


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

public void start(Stage ps) {

    StackPane sp = new StackPane();

    setTile();  
    setPlayer1Pieces();
    setPlayer2Pieces();


    sp.getChildren().addAll(tp, player1, player2);
    Scene s = new Scene(sp, 390, 390);

    ps.setScene(s);
    ps.setTitle("PP2 Halma Project");
    ps.setResizable(false);
    ps.show();
}

public void setTile() {
    final int size = 8;

    for (int k = 0; k < size; k++) {

        if (k == 0 || k % 2 == 0) {

            for (int i = 0; i < size; i++) {

                if (i == 0 || i % 2 == 0) {
                    Rectangle r = new Rectangle();
                    r.setWidth(50);
                    r.setHeight(50);
                    r.setFill(Color.web("lemonchiffon"));
                    tp.getChildren().add(r);
                }

                if (i % 2 == 1) {
                    Rectangle r = new Rectangle();
                    r.setWidth(50);
                    r.setHeight(50);
                    r.setFill(Color.web("brown"));
                    tp.getChildren().add(r);
                }
            }
        }

        if (k % 2 == 1) {

            for (int i = 0; i < size; i++) {

                if (i == 0 || i % 2 == 0) {
                    Rectangle r = new Rectangle();
                    r.setWidth(50);
                    r.setHeight(50);
                    r.setFill(Color.web("brown"));
                    tp.getChildren().add(r);
                }

                if (i % 2 == 1) {
                    Rectangle r = new Rectangle();
                    r.setWidth(50);
                    r.setHeight(50);
                    r.setFill(Color.web("lemonchiffon"));
                    tp.getChildren().add(r);
                }
            }
        }
    }
}

public void setPlayer2Pieces() {
    int count = 4;

    do {
        for (int i = 4; i >= 1; i--) {
            for (int j = 1; j < i; j++) {

                if (count == 4) {
                    Circle c = new Circle();
                    c.setRadius(20);
                    c.setCenterX(375);
                    c.setCenterY(225);
                    c.setFill(Color.web("#B7B8B6"));
                    c.setOnMouseDragged(e -> {
                        c.setCenterX(e.getSceneX());
                        c.setCenterY(e.getSceneY());
                    });

                    player2.getChildren().add(c);
                }

                if (count == 3) {
                    for (int x = 375; x > 275; x-= 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(275);
                        c.setFill(Color.web("#B7B8B6"));

                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player2.getChildren().add(c);
                    }
                }

                if (count == 2) {
                    for (int x = 375; x > 225; x-= 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(325);
                        c.setFill(Color.web("#B7B8B6"));

                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player2.getChildren().add(c);
                    }
                }

                if (count == 1) {
                    for (int x = 375; x > 175; x-= 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(375);
                        c.setFill(Color.web("#B7B8B6"));

                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player2.getChildren().add(c);
                    }
                }
                count--;
            }   
        }
    } while (count > 0);
}

public void setPlayer1Pieces() {
    int count = 1;
    do {
        for (int i = 4; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {

                if (count == 1) {
                    for (int x = 25; x < 200; x += 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(25);
                        c.setFill(Color.web("#626D71"));
                        c.setCursor(Cursor.MOVE);
                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player1.getChildren().add(c);
                    }
                }

                if (count == 2) {
                    for (int x = 25; x < 150; x += 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(75);
                        c.setFill(Color.web("#626D71"));
                        c.setCursor(Cursor.MOVE);
                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player1.getChildren().add(c);
                    }
                }

                if (count == 3) {
                    for (int x = 25; x < 100; x += 50) {
                        Circle c = new Circle();
                        c.setRadius(20);
                        c.setCenterX(x);
                        c.setCenterY(125);
                        c.setFill(Color.web("#626D71"));
                        c.setCursor(Cursor.MOVE);
                        c.setOnMouseDragged(e -> {
                            c.setCenterX(e.getSceneX());
                            c.setCenterY(e.getSceneY());
                        });
                        player1.getChildren().add(c);
                    }
                }

                if (count == 4) {
                    Circle c = new Circle();
                    c.setRadius(20);
                    c.setCenterX(25);
                    c.setCenterY(175);
                    c.setFill(Color.web("#626D71"));
                    c.setCursor(Cursor.MOVE);
                    c.setOnMouseDragged(e -> {
                        c.setCenterX(e.getSceneX());
                        c.setCenterY(e.getSceneY());
                    });
                    player1.getChildren().add(c);
                }
            }
            count ++;
        }
    } while (count < 5);
}
}

0 个答案:

没有答案