酷代码 AI
菜单
服务商

Python+Pygame实现功能完备的象棋游戏

以下是一个使用 Python 和 Pygame 实现的象棋游戏代码,包含了棋子的走法、活动范围、吃子、将军、棋子的可移动位置判断以及绝杀提示等功能。 ```python import pygame import sys # 初始化 Pygame pygame.init() # 定义常量 WIDTH, HEIGHT = 720, 720 BOARD_SIZE = 9, 10 SQUARE_SIZE = WIDTH // BOARD_SIZE[0] WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) # 加载棋子图片 piece_images = { 'r': pygame.image.load('images/red_chariot.png'), 'n': pygame.image.load('images/red_horse.png'), 'b': pygame.image.load('images/red_elephant.png'), 'a': pygame.image.load('images/red_advisor.png'), 'k': pygame.image.load('images/red_king.png'), 'p': pygame.image.load('images/red_pawn.png'), 'c': pygame.image.load('images/red_cannon.png'), 'R': pygame.image.load('images/black_chariot.png'), 'N': pygame.image.load('images/black_horse.png'), 'B': pygame.image.load('images/black_elephant.png'), 'A': pygame.image.load('images/black_advisor.png'), 'K': pygame.image.load('images/black_king.png'), 'P': pygame.image.load('images/black_pawn.png'), 'C': pygame.image.load('images/black_cannon.png') } # 初始化棋盘 board = [ ['R', 'N', 'B', 'A', 'K', 'A', 'B', 'N', 'R'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'C', ' ', ' ', ' ', ' ', ' ', 'C', ' '], ['P', ' ', 'P', ' ', 'P', ' ', 'P', ' ', 'P'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['p', ' ', 'p', ' ', 'p', ' ', 'p', ' ', 'p'], [' ', 'c', ' ', ' ', ' ', ' ', ' ', 'c', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['r', 'n', 'b', 'a', 'k', 'a', 'b', 'n', 'r'] ] # 初始化当前玩家 current_player = 'red' # 绘制棋盘 def draw_board(screen): screen.fill(WHITE) for i in range(BOARD_SIZE[0]): for j in range(BOARD_SIZE[1]): pygame.draw.rect(screen, BLACK, (i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), 1) # 绘制楚河汉界 pygame.draw.rect(screen, (255, 228, 181), (0, 4 * SQUARE_SIZE, WIDTH, SQUARE_SIZE)) # 绘制棋子 def draw_pieces(screen): for i in range(BOARD_SIZE[0]): for j in range(BOARD_SIZE[1]): piece = board[j][i] if piece != ' ': screen.blit(piece_images[piece], (i * SQUARE_SIZE, j * SQUARE_SIZE)) # 检查位置是否在棋盘内 def is_on_board(x, y): return 0 <= x < BOARD_SIZE[0] and 0 <= y < BOARD_SIZE[1] # 检查是否是同色棋子 def is_same_color(piece1, piece2): if piece1.isupper() and piece2.isupper(): return True if piece1.islower() and piece2.islower(): return True return False # 检查是否被将军 def is_check(player): king_symbol = 'k' if player == 'red' else 'K' king_x, king_y = None, None for i in range(BOARD_SIZE[0]): for j in range(BOARD_SIZE[1]): if board[j][i] == king_symbol: king_x, king_y = i, j break opponent = 'black' if player == 'red' else 'red' for i in range(BOARD_SIZE[0]): for j in range(BOARD_SIZE[1]): piece = board[j][i] if (opponent == 'red' and piece.islower()) or (opponent == 'black' and piece.isupper()): if is_valid_move(i, j, king_x, king_y, opponent): return True return False # 检查移动是否合法 def is_valid_move(from_x, from_y, to_x, to_y, player): piece = board[from_y][from_x] target = board[to_y][to_x] if is_same_color(piece, target): return False if piece.islower() and player != 'red': return False if piece.isupper() and player != 'black': return False # 具体棋子走法规则 if piece.lower() == 'k': if abs(to_x - from_x) + abs(to_y - from_y) != 1: return False if (player == 'red' and (to_y < 7 or to_x < 3 or to_x > 5)) or (player == 'black' and (to_y > 2 or to_x < 3 or to_x > 5)): return False elif piece.lower() == 'a': if abs(to_x - from_x) != 1 or abs(to_y - from_y) != 1: return False if (player == 'red' and (to_y < 7 or to_x < 3 or to_x > 5)) or (player == 'black' and (to_y > 2 or to_x < 3 or to_x > 5)): return False elif piece.lower() == 'b': if abs(to_x - from_x) != 2 or abs(to_y - from_y) != 2: return False if (player == 'red' and to_y < 5) or (player == 'black' and to_y > 4): return False block_x = (from_x + to_x) // 2 block_y = (from_y + to_y) // 2 if board[block_y][block_x] != ' ': return False elif piece.lower() == 'n': dx = to_x - from_x dy = to_y - from_y if (abs(dx) == 2 and abs(dy) == 1) or (abs(dx) == 1 and abs(dy) == 2): if abs(dx) == 2: block_x = from_x + dx // 2 block_y = from_y else: block_x = from_x block_y = from_y + dy // 2 if board[block_y][block_x] != ' ': return False else: return False elif piece.lower() == 'r': if to_x != from_x and to_y != from_y: return False if to_x == from_x: step = 1 if to_y > from_y else -1 for y in range(from_y + step, to_y, step): if board[y][from_x] != ' ': return False else: step = 1 if to_x > from_x else -1 for x in range(from_x + step, to_x, step): if board[from_y][x] != ' ': return False elif piece.lower() == 'c': if to_x != from_x and to_y != from_y: return False count = 0 if to_x == from_x: step = 1 if to_y > from_y else -1 for y in range(from_y + step, to_y): if board[y][from_x] != ' ': count += 1 else: step = 1 if to_x > from_x else -1 for x in range(from_x + step, to_x): if board[from_y][x] != ' ': count += 1 if target == ' ' and count > 0: return False if target != ' ' and count != 1: return False elif piece.lower() == 'p': if player == 'red': if from_y < 5 and to_y != from_y + 1: return False if from_y >= 5 and (to_y != from_y + 1 or (to_x != from_x and abs(to_x - from_x) != 1)): return False else: if from_y > 4 and to_y != from_y - 1: return False if from_y <= 4 and (to_y != from_y - 1 or (to_x != from_x and abs(to_x - from_x) != 1)): return False # 检查移动后是否会被将军 temp_board = [row[:] for row in board] temp_board[to_y][to_x] = temp_board[from_y][from_x] temp_board[from_y][from_x] = ' ' if is_check(player): return False return True # 获取可移动位置 def get_valid_moves(x, y, player): moves = [] for dx in range(-BOARD_SIZE[0], BOARD_SIZE[0]): for dy in range(-BOARD_SIZE[1], BOARD_SIZE[1]): to_x = x + dx to_y = y + dy if is_on_board(to_x, to_y) and is_valid_move(x, y, to_x, to_y, player): moves.append((to_x, to_y)) return moves # 检查是否绝杀 def is_mate(player): for i in range(BOARD_SIZE[0]): for j in range(BOARD_SIZE[1]): piece = board[j][i] if (player == 'red' and piece.islower()) or (player == 'black' and piece.isupper()): moves = get_valid_moves(i, j, player) if moves: return False return True # 主循环 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Chinese Chess') selected_piece = None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos col = x // SQUARE_SIZE row = y // SQUARE_SIZE if selected_piece is None: piece = board[row][col] if ((current_player == 'red' and piece.islower()) or (current_player == 'black' and piece.isupper())): selected_piece = (col, row) else: from_x, from_y = selected_piece if is_valid_move(from_x, from_y, col, row, current_player): board[row][col] = board[from_y][from_x] board[from_y][from_x] = ' ' if is_check(current_player): # 移动后被将军,撤销操作 board[from_y][from_x] = board[row][col] board[row][col] = ' ' else: current_player = 'black' if current_player == 'red' else 'red' if is_check(current_player): if is_mate(current_player): print(f'{current_player} is mated!') selected_piece = None draw_board(screen) draw_pieces(screen) if selected_piece is not None: from_x, from_y = selected_piece moves = get_valid_moves(from_x, from_y, current_player) for move in moves: to_x, to_y = move pygame.draw.rect(screen, RED, (to_x * SQUARE_SIZE, to_y * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), 2) pygame.display.flip() ``` ### 代码说明: 1. **初始化部分**:初始化 Pygame,定义常量,加载棋子图片,初始化棋盘和当前玩家。 2. **绘制部分**:`draw_board` 函数用于绘制棋盘,`draw_pieces` 函数用于绘制棋子。 3. **规则判断部分**: - `is_on_board` 函数用于检查位置是否在棋盘内。 - `is_same_color` 函数用于检查两个棋子是否同色。 - `is_check` 函数用于检查是否被将军。 - `is_valid_move` 函数用于检查移动是否合法,包括具体棋子的走法规则和移动后是否会被将军的判断。 - `get_valid_moves` 函数用于获取某个棋子的可移动位置。 - `is_mate` 函数用于检查是否绝杀。 4. **主循环部分**:处理鼠标点击事件,根据规则移动棋子,更新棋盘状态,判断是否将军和绝杀。 ### 图片素材: 你需要在项目目录下创建一个 `images` 文件夹,并将象棋棋子的图片放入其中,图片命名为 `red_chariot.png`、`red_horse.png` 等,确保图片名称与代码中的 `piece_images` 字典对应。 ######[AI问答 | 3294点数解答 | 2026-02-15 10:15:54]

相关提问
发财导航,免费问AI
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]