|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 歌者文明清理员 于 2023-8-14 10:15 编辑
main.zip
(19.73 KB, 下载次数: 7)
为什么点击上面的两个棋子没有反应?
点左边就报错
- pygame 2.5.0 (SDL 2.28.0, Python 3.11.4)
- Hello from the pygame community. https://www.pygame.org/contribute.html
- Traceback (most recent call last):
- File "e:\Python123\i\main.py", line 79, in <module>
- board.push_san(move_str)
- File "C:\Program Files\Python311\Lib\site-packages\chess\__init__.py", line 3105, in push_san
- move = self.parse_san(san)
- ^^^^^^^^^^^^^^^^^^^
- File "C:\Program Files\Python311\Lib\site-packages\chess\__init__.py", line 3063, in parse_san
- move = self.find_move(square(from_file, from_rank), to_square, promotion)
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- File "C:\Program Files\Python311\Lib\site-packages\chess\__init__.py", line 2357, in find_move
- raise IllegalMoveError(f"no matching legal move for {move.uci()} ({SQUARE_NAMES[from_square]} -> {SQUARE_NAMES[to_square]}) in {self.fen()}")
- chess.IllegalMoveError: no matching legal move for e2d2 (e2 -> d2) in rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
复制代码
本帖最后由 鱼cpython学习者 于 2023-8-14 12:45 编辑
好复杂,总算给我解决了
首先是判断移动符合规则的部分,75行:
- if move[:2] == 'abcdefgh'[col] + '87654321'[row]:
复制代码
假设moves里有一个"e2e3"和"d2d3",我要把棋子从e2移到e3,那么我的chosen就是e2,target就是e3,但是你在这里写成了判断target是否等于move[:2],也就是判断target是否是其他可以移动的棋子,例如d2,那么点其他位置判断不通过,点其他棋子才判断通过。但是显然不能把棋子从e2移到d2,就会报出描述的错误
修改为:
- if move[:2] == 'abcdefgh'[chosen[1]] + '87654321'[chosen[0]] and move[2:] == 'abcdefgh'[col] + '87654321'[row]:
复制代码
修改后,点其他位置有反应,但是移动的是对面的棋子,然而我print了board,显示移动了白兵,那么就是你的摆棋子有问题,59~66行
- for col in range(8):
- for row in range(8):
- piece = board.piece_at(square(col, row))
- if piece:
- color = not piece.color
- piece_type = piece.piece_type
- image = images[(color, piece_type)]
- screen.blit(image, square_to_pos((row, col)))
复制代码
这段代码,row是从0到7,所以棋盘的遍历是从下往上。然而你的square_to_pos似乎是反着来的,导致你的color也得是反着来的,所以有那个not piece.color
修改:
- for col in range(8):
- for row in range(8):
- piece = board.piece_at(square(col, row))
- if piece:
- color = piece.color
- piece_type = piece.piece_type
- image = images[(color, piece_type)]
- screen.blit(image, square_to_pos((7 - row, col)))
复制代码
同理,下面的这个判断也要改,80行:
- if 0 <= row < 8 and 0 <= col < 8 and board.piece_at(square(col, 7 - row)):
复制代码
我不懂国际象棋,改的实在头晕脑涨
|
|