[英]Python chess program
我正在嘗試為我的編碼項目制作一個相當簡單的國際象棋游戲,希望在某個時候也能實現一個計算機對手,有點卡在如何添加移動限制和功能以檢測此時游戲的結束。 身體是由@sloth 設計的,我已經添加了。
我已經完成了相當簡單的工作,加載了所有部件並更改了電路板顏色,但我不太確定此時該做什么。 任何幫助,將不勝感激!
import pygame
TILESIZE = 75
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
dark = False
for y in range(8):
for x in range(8):
rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(board_surf, pygame.Color('darkgreen' if dark else 'beige'), rect)
dark = not dark
dark = not dark
return board_surf
def get_square_under_mouse(board):
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos]
try:
if x >= 0 and y >= 0: return (board[y][x], x, y)
except IndexError: pass
return None, None, None
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
## BLACK PIECES ##
for x in range(0, 8):
board[1][x] = ('black', 'pawn')
for x in range(0, 1):
board[0][x] = ('black', 'rook')
for x in range(0, 8):
board[0][x] = ('black', 'rook')
for x in range(1, 2):
board[0][x] = ('black', 'horse')
for x in range(6, 7):
board[0][x] = ('black', 'horse')
for x in range(2, 3):
board[0][x] = ('black', 'bishop')
for x in range(5, 6):
board[0][x] = ('black', 'bishop')
for x in range(3, 4):
board[0][x] = ('black', 'queen')
for x in range(4, 5):
board[0][x] = ('black', 'king')
## WHITE PIECES ##
for x in range(0, 8):
board[6][x] = ('white', 'pawn')
for x in range(0, 1):
board[7][x] = ('white', 'rook')
for x in range(7, 8):
board[7][x] = ('white', 'rook')
for x in range(1, 2):
board[7][x] = ('white', 'horse')
for x in range(6, 7):
board[7][x] = ('white', 'horse')
for x in range(2, 3):
board[7][x] = ('white', 'bishop')
for x in range(5, 6):
board[7][x] = ('white', 'bishop')
for x in range(3, 4):
board[7][x] = ('white', 'queen')
for x in range(4, 5):
board[7][x] = ('white', 'king')
return board
def draw_pieces(screen, board, font, selected_piece):
sx, sy = None, None
if selected_piece:
piece, sx, sy = selected_piece
for y in range(8):
for x in range(8):
piece = board[y][x]
if piece:
selected = x == sx and y == sy
color, type = piece
s1 = font.render(type[0], True, pygame.Color('red' if selected else color))
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE+1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
screen.blit(s2, s2.get_rect(center=pos.center).move(1, 1))
screen.blit(s1, s1.get_rect(center=pos.center))
def draw_selector(screen, piece, x, y):
if piece != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
def draw_drag(screen, board, selected_piece, font):
if selected_piece:
piece, x, y = get_square_under_mouse(board)
if x != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)
color, type = selected_piece[0]
s1 = font.render(type[0], True, pygame.Color(color))
s2 = font.render(type[0], True, pygame.Color('darkgrey'))
pos = pygame.Vector2(pygame.mouse.get_pos())
screen.blit(s2, s2.get_rect(center=pos + (1, 1)))
screen.blit(s1, s1.get_rect(center=pos))
selected_rect = pygame.Rect(BOARD_POS[0] + selected_piece[1] * TILESIZE, BOARD_POS[1] + selected_piece[2] * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.line(screen, pygame.Color('red'), selected_rect.center, pos)
return (x, y)
def main():
pygame.init()
font = pygame.font.SysFont('', 64)
screen = pygame.display.set_mode((620, 620))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
selected_piece = None
drop_pos = None
while True:
piece, x, y = get_square_under_mouse(board)
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
if piece != None:
selected_piece = piece, x, y
if e.type == pygame.MOUSEBUTTONUP:
if drop_pos:
piece, old_x, old_y = selected_piece
board[old_y][old_x] = 0
new_x, new_y = drop_pos
board[new_y][new_x] = piece
selected_piece = None
drop_pos = None
screen.fill(pygame.Color('grey'))
screen.blit(board_surf, BOARD_POS)
draw_pieces(screen, board, font, selected_piece)
draw_selector(screen, piece, x, y)
drop_pos = draw_drag(screen, board, selected_piece, font)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
首先,如果您只想完成任務,最好使用 python-chess 庫。 否則,要檢查國際象棋游戲的結束,您需要查看是否有幾個可能的條件為真:
因此,僅僅因為游戲規則,它就相當復雜。
現在,如果您想選擇合法的移動,請閱讀有關 0x88 代表棋盤的方法。 它將 position 表示為兩塊板,一個包含碎片,另一個包含非法區域。 這提供了對合法動作的簡單檢查,因為您只需將棋盤與生成的動作結合來檢查合法性。 在實施滑動件、國王和通行證時要小心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.