如何绘制到我的 8x8 逆转(奥赛罗)游戏板上



我正在创建一个逆转游戏。我决定使用 JLabel 创建我的开发板。我遇到的问题是将四个起始部分放在板上。这只是一个2人游戏,它是1v1。在游戏开始时,每个玩家在棋盘的最中间开始 2 个棋子。在我的程序中,我为2名玩家创建2个蓝色和2个红色棋子,但无论我做什么,我都无法让这些棋子出现在游戏板上。当我运行调试器时,看起来我从来没有给每个 BoardSquare 正确的 x 和 y 值。我不确定如何获取板方块的 x 和 y 值。欢迎任何和所有的帮助/批评。几个小时以来,我一直在尝试解决此问题。谢谢。

public class ReversiRemake {
    public static void main(String[] args) {
        PlayGame game = new PlayGame();
    }
}
class BoardSquare extends JLabel {
    private int x;
    private int y;
    private int row;
    private int column;
    private MyShape circle;
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);
    BoardSquare(int row, int column) {
        addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == 1) {
                    }
                }
            });
        this.row = row;
        this.column = column;
        setBorder(b);

    }
    public void setShape(Player p){
        if(p instanceof HumanPlayer){
            circle = new MyShape(x, y, Color.blue); 
        }
        if(p instanceof aiPlayer){
            circle = new MyShape(x, y, Color.red);
        }
    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public MyShape getShape() {
        return circle;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        circle.drawShape(g2);
    }
}
class MyShape {
    private Color color;
    private int x;
    private int y;
    MyShape(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }
    MyShape(Color color) {
        this.color = color;
    }
    public void drawShape(Graphics2D g) {
        g.setPaint(color);
        g.drawOval(x + 5, y + 2, 50, 50);
        g.fillOval(x + 5, y + 2, 50, 50);
    }
    public void setColor(Player p) {
        if (p instanceof HumanPlayer) {
            color = Color.BLUE;
        }
        if (p instanceof aiPlayer) {
            color = Color.red;
        }
    }
}
class GameBoard extends JPanel {
    BoardSquare[][] BoardSquares = new BoardSquare[8][8];
    private BoardSquare bs;
    GameBoard() {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = new BoardSquare(row, column);
                add(BoardSquares[row][column]);
            }
        }
    }
    public void StartingPieces(HumanPlayer p1, aiPlayer p2) {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = bs;
                if (row == 3 && column == 3) {
                    bs.setShape(p1);
                }
                if (row == 3 && column == 4) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 3) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 4) {
                    bs.setShape(p1);
                }
            }
        }
    }
}
abstract class Player {
    int playerType;
    Color color;
    int score;
    Player(int playerType, Color color, int score) {
        this.playerType = playerType;
        this.color = color;
        this.score = score;
    }
}
class HumanPlayer extends Player {
    HumanPlayer() {
        super(1, Color.BLUE, 0);
    }
}
class aiPlayer extends Player {
    aiPlayer() {
        super(2, Color.RED, 0);
    }
}
class PlayGame {
    private HumanPlayer p1 = new HumanPlayer();
    private aiPlayer p2 = new aiPlayer();
    private GameBoard gameBoard = new GameBoard();
    private boolean p1Turn;
    private boolean p2Turn;
    PlayGame() {
        setStartingPieces();
    }
    public void setStartingPieces() {
        gameBoard.StartingPieces(p1, p2);
    }
}

我无法在发布时运行您的代码,但我相信通过目视检查该行

BoardSquares[row][column] = bs;

应该是

bs = BoardSquares[row][column];

正如上面所写的,会发生NullPointerException,并且bs从未真正被设置过。 我提供的修订版将允许在GameBoard类中设置形状。

相关内容

  • 没有找到相关文章

最新更新