JavaFX使用GridPane创建游戏板 [英] JavaFX creating a game board with GridPane

查看:112
本文介绍了JavaFX使用GridPane创建游戏板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行该应用程序时,游戏板显示正常,尽管单击图块时,文本始终显示在板[0] [0]的左上角.

When the application is run the game board shows fine, although when a tile is clicked the text always shows in the top left corner of the board [0][0].

我也在逻辑上挣扎.我有一个带有功能makeMove的int board矩阵,该函数可以向该矩阵添加一个玩家移动,尽管我无法让该玩家对矩阵进行移动,也无法在tile索引上打印相应的"O".

Also I am struggling with the logic. I have a int board matrix with a function makeMove which to add a players move to the matrix, although I can't get the player to make a move on the matrix and also print a corresponding "O" on the tile index.

我将如何创建可以传递行,col索引并更改棋盘矩阵并在GUI上打印玩家作品的功能(例如AI移动)

How would I create a function that can be passed a row, col index and alter the board matrix and print the player piece on the GUI (e.g.for AI move)

以及当玩家单击GUI磁贴以进行相应移动时.

And also when the GUI tile is clicked by the player to then make the corresponding move.

到目前为止,我已经创建了GridPane并在GUI的每个索引处添加了带有文本的Rectangle.

So far I have created a GridPane and added Rectangle with text at each index for the GUI.

我创建了一个Board类,该类构造一个int棋盘矩阵来容纳玩家的移动(P1,P2,空).

I have created a Board class which constructs an int board matrix to hold player moves (P1, P2, empty).

public class Board {

    private int[][] board_matrix;
    private int board_size;
    private int win_length;

    public Board(int board_size, int win_length) {
        this.board_matrix = new int[board_size][board_size];
        this.board_size = board_size;
        this.win_length = win_length;

        for (int i = 0; i < board_size; i++) {
            for (int j = 0; j < board_size; j++) {
                this.board_matrix[i][j] = 0;
            }
        }
    }

    public void make_move(int player, int x_pos, int y_pos) {
        if (player == 1) board_matrix[x_pos][y_pos] = 1;
        else board_matrix[x_pos][y_pos] = 2;
    }

public class BoardGUI_ extends Application {

    private final int BOARD_SIZE = 15;

    public Parent createBoard() {
        GridPane gameBoard = new GridPane();
        gameBoard.setPrefSize(755, 755);

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {

                Rectangle tile = new Rectangle(50, 50);
                tile.setFill(Color.BURLYWOOD);
                tile.setStroke(Color.BLACK);

                Text text = new Text();
                text.setFont(Font.font(40));


                GridPane.setRowIndex(tile, i);
                GridPane.setColumnIndex(tile, j);

                tile.setOnMouseClicked(event -> drawMove(text));

                gameBoard.getChildren().addAll(tile, text);
            }
        }
        return gameBoard;
    }

    public void drawMove(Text text) {
        text.setText("O");
        text.setFill(Color.BLACK);
    }

推荐答案

如果要在Rectangle对象的顶部添加Text对象,请将两者都包装在StackPane中:

If you want to add a Text object on top of a Rectangle object wrap both in a StackPane:

public Parent createBoard() {

    GridPane gameBoard = new GridPane();
    gameBoard.setPrefSize(755, 755);

    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {

            Rectangle tile = new Rectangle(50, 50);
            tile.setFill(Color.BURLYWOOD);
            tile.setStroke(Color.BLACK);

            Text text = new Text();
            text.setFont(Font.font(40));
            gameBoard.add(new StackPane(tile, text), j, i);

            //GridPane.setRowIndex(tile, i);
            //GridPane.setColumnIndex(tile, j);   
            //gameBoard.getChildren().addAll(tile, text);
            tile.setOnMouseClicked(event -> drawMove(text));
        }
    }
    return gameBoard;
}

这篇关于JavaFX使用GridPane创建游戏板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆