Modeling a chess board in wxpython
I'm learning wxPython to make a chess GUI, but I don't exactly know how it should be done, what widgets to use. I'm thinking that I make every square a button with the piece's picture and then position them on the board right next to each other, so I have 64 buttons on the board.
When I move one piece, I put the button on the target square and the origin square will then be replaced with a button which represents an empty square.
Is this the right way to do it in wxPython? I'm really stuck here, as there are no examples on the web for a chess GUI in wxPython.
Ah, creating a chess program with wxPython was how I taught myself python programming, some ten years ago ;-) Good luck.
First you will need to draw the grid for the board. This is easy. Then you will need a datastructure to remember the positions of all the pieces. For this I used the module here: http://www.alcyone.com/pyos/chess/. Next you need a routine that given a Board
will draw the pieces at the appropriate locations.
To move the chess pieces, you will probably want to use drag and drop. This way, you can make a square (or the piece that is drawn on that square) draggable. You will need to a routine to calculate the square from your screen coordinates eg screen_to_board()
. On the drop event you will calculate the target square using this same routine. Then you simply update the board state (use Board.move()
) and redraw the screen.
上一篇: 国际象棋棋子运动问题
下一篇: 在wxpython中建模一个国际象棋棋盘