鱼C论坛

 找回密码
 立即注册
楼主: 过默

怎么学象棋

[复制链接]
 楼主| 发表于 3 小时前 | 显示全部楼层
过默 发表于 2026-7-5 11:43
Traceback (most recent call last):
  File "C:\Program Files\Python38\qipan.py", line 176, in
   ...
  1. import pygame
  2. import sys

  3. pygame.init()
  4. WIDTH, HEIGHT = 640, 720
  5. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  6. pygame.display.set_caption("Chinese Chess - Full")
  7. clock = pygame.time.Clock()

  8. WHITE = (255, 255, 255)
  9. BLACK = (0, 0, 0)
  10. BROWN = (200, 160, 100)
  11. RED = (200, 0, 0)

  12. MARGIN = 40
  13. SIZE = 60

  14. # 棋子数据:名字,列,行,颜色
  15. pieces = [
  16.     ('R', 0, 0, RED), ('马', 1, 0, RED), ('E', 2, 0, RED), ('A', 3, 0, RED),
  17.     ('K', 4, 0, RED), ('A', 5, 0, RED), ('E', 6, 0, RED), ('H', 7, 0, RED),
  18.     ('R', 8, 0, RED), ('C', 1, 2, RED), ('C', 7, 2, RED), ('S', 0, 3, RED),
  19.     ('S', 2, 3, RED), ('S', 4, 3, RED), ('S', 6, 3, RED), ('S', 8, 3, RED),
  20.     ('R', 0, 9, BLACK), ('H', 1, 9, BLACK), ('E', 2, 9, BLACK), ('A', 3, 9, BLACK),
  21.     ('K', 4, 9, BLACK), ('A', 5, 9, BLACK), ('E', 6, 9, BLACK), ('H', 7, 9, BLACK),
  22.     ('R', 8, 9, BLACK), ('C', 1, 7, BLACK), ('C', 7, 7, BLACK), ('S', 0, 6, BLACK),
  23.     ('S', 2, 6, BLACK), ('S', 4, 6, BLACK), ('S', 6, 6, BLACK), ('S', 8, 6, BLACK)
  24. ]

  25. selected_piece = None
  26. turn = 'red'

  27. def draw_board():
  28.     screen.fill(BROWN)
  29.     # 横线
  30.     for row in range(10):
  31.         y = MARGIN + row * SIZE
  32.         pygame.draw.line(screen, BLACK, (MARGIN, y), (MARGIN + 8 * SIZE, y), 2)
  33.     # 竖线
  34.     for col in range(9):
  35.         x = MARGIN + col * SIZE
  36.         top = MARGIN
  37.         bottom = MARGIN + 9 * SIZE
  38.         if col == 0 or col == 8:
  39.             pygame.draw.line(screen, BLACK, (x, top), (x, bottom), 2)
  40.         else:
  41.             # 上半
  42.             pygame.draw.line(screen, BLACK, (x, top), (x, top + 4 * SIZE), 2)
  43.             # 下半
  44.             pygame.draw.line(screen, BLACK, (x, top + 5 * SIZE), (x, bottom), 2)
  45.     # 九宫斜线
  46.     for (left, top) in [(MARGIN + 3 * SIZE, MARGIN), (MARGIN + 3 * SIZE, MARGIN),
  47.                         (MARGIN + 3 * SIZE, MARGIN + 7 * SIZE), (MARGIN + 3 * SIZE, MARGIN + 7 * SIZE)]:
  48.         right = left + 2 * SIZE
  49.         bottom = top + 2 * SIZE
  50.         pygame.draw.line(screen, BLACK, (left, top), (right, bottom), 2)
  51.         pygame.draw.line(screen, BLACK, (left, bottom), (right, top), 2)
  52.     # 楚河汉界
  53.     font = pygame.font.Font("simsun", 30)
  54.     text1 = font.render("CHU HE", True, BLACK)
  55.     text2 = font.render("HAN JIE", True, BLACK)
  56.     screen.blit(text1, (MARGIN + 1.5 * SIZE, MARGIN + 4 * SIZE))
  57.     screen.blit(text2, (MARGIN + 4.5 * SIZE, MARGIN + 4 * SIZE))

  58. def draw_pieces():
  59.     font = pygame.font.Font(None, 32)
  60.     for name, col, row, color in pieces:
  61.         x = MARGIN + col * SIZE
  62.         y = MARGIN + row * SIZE
  63.         pygame.draw.circle(screen, WHITE, (x, y), SIZE // 2 - 4)
  64.         pygame.draw.circle(screen, color, (x, y), SIZE // 2 - 4, 2)
  65.         text = font.render(name, True, color)
  66.         text_rect = text.get_rect(center=(x, y))
  67.         screen.blit(text, text_rect)

  68. def get_click_pos(mouse_pos):
  69.     x, y = mouse_pos
  70.     col = round((x - MARGIN) / SIZE)
  71.     row = round((y - MARGIN) / SIZE)
  72.     if 0 <= col <= 8 and 0 <= row <= 9:
  73.         return col, row
  74.     return None, None

  75. def move_piece(start_idx, end_col, end_row):
  76.     global pieces, turn
  77.     name, col, row, color = pieces[start_idx]
  78.     # 判断目标位置是否有棋子(吃子或空位)
  79.     target_idx = None
  80.     for i, (n, c, r, colr) in enumerate(pieces):
  81.         if c == end_col and r == end_row:
  82.             target_idx = i
  83.             break

  84.     # ----------------- 走法合法性检查 -----------------
  85.     def is_valid_move(name, col, row, end_col, end_row, pieces_list):
  86.         dx = end_col - col
  87.         dy = end_row - row
  88.         # 马走日字
  89.         if name in ('M', 'H'):  # 根据你的棋子名字调整,红马='M',黑马='H'
  90.             if (abs(dx), abs(dy)) not in [(1,2), (2,1)]:
  91.                 return False  # 不是日字
  92.             # 计算蹩马腿位置
  93.             if abs(dx) == 1:  # 横向走1,纵向走2
  94.                 leg_col, leg_row = col, row + dy//2
  95.             else:             # 横向走2,纵向走1
  96.                 leg_col, leg_row = col + dx//2, row
  97.             # 检查该位置是否有棋子
  98.             for (n, c, r, clr) in pieces_list:
  99.                 if c == leg_col and r == leg_row:
  100.                     return False  # 蹩马腿
  101.             return True
  102.         else:
  103.             # 其他棋子暂时允许任意移动(后续可扩展)
  104.             return True

  105.     if not is_valid_move(name, col, row, end_col, end_row, pieces):
  106.         return False  # 不合法,取消移动

  107.     # ----------------- 执行移动(含吃子) -----------------
  108.     if target_idx is not None:
  109.         target_color = pieces[target_idx][3]
  110.         if target_color != color:  # 吃子
  111.             pieces.pop(target_idx)
  112.             # 重新找到自己的索引(因为列表变动)
  113.             new_idx = None
  114.             for i, (n, c, r, colr) in enumerate(pieces):
  115.                 if c == col and r == row and colr == color:
  116.                     new_idx = i
  117.                     break
  118.             if new_idx is not None:
  119.                 pieces[new_idx] = (name, end_col, end_row, color)
  120.             return True
  121.         else:
  122.             return False  # 不能吃己方棋子
  123.     else:
  124.         # 空位直接移动
  125.         pieces[start_idx] = (name, end_col, end_row, color)
  126.         return True

  127. def handle_click(mouse_pos):
  128.     global selected_piece, turn
  129.     click_col, click_row = get_click_pos(mouse_pos)
  130.     if click_col is None:
  131.         return
  132.     if selected_piece is None:
  133.         # 尝试选中当前回合方的棋子
  134.         for i, (name, col, row, color) in enumerate(pieces):
  135.             if col == click_col and row == click_row:
  136.                 if (turn == 'red' and color == RED) or (turn == 'black' and color == BLACK):
  137.                     selected_piece = i
  138.                 break
  139.     else:
  140.         # 尝试移动
  141.         if move_piece(selected_piece, click_col, click_row):
  142.             # 成功移动,切换回合
  143.             turn = 'black' if turn == 'red' else 'red'
  144.         # 无论移动成功与否,清除选中状态
  145.         selected_piece = None

  146. def main():
  147.     running = True
  148.     while running:
  149.         for event in pygame.event.get():
  150.             if event.type == pygame.QUIT:
  151.                 running = False
  152.             elif event.type == pygame.MOUSEBUTTONDOWN:
  153.                 handle_click(event.pos)
  154.         draw_board()
  155.         draw_pieces()
  156.         pygame.display.flip()
  157.         clock.tick(30)
  158.     pygame.quit()
  159.     sys.exit()

  160. if __name__ == "__main__":
  161.     main()
复制代码

Traceback (most recent call last):
  File "C:\Program Files\Python38\qipan.py", line 176, in <module>
    main()
  File "C:\Program Files\Python38\qipan.py", line 169, in main
    draw_pieces()
  File "C:\Program Files\Python38\qipan.py", line 66, in draw_pieces
    font = pygame.font.Font("simsun", 32)
FileNotFoundError: No file 'simsun' found in working directory 'C:\Program Files\Python38'.
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 3 小时前 | 显示全部楼层
过默 发表于 2026-7-5 11:45
Traceback (most recent call last):
  File "C:\Program Files\Python38\qipan.py", line 176, in
...

import pygame
import sys

pygame.init()
WIDTH, HEIGHT=640, 720
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygmae.display.set_caption("Chinese Chess - Full")
clock = pygame.time.Clock()

WHITE=
是什么?怎么调用显示棋盘
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2 小时前 | 显示全部楼层
FishC 发表于 2026-7-5 09:08
这个错误是因为在调用 pygame.font.SysFont 时,内部字体检测函数意外得到了整数而非字符串。通常是因 ...

怎么下载,pip安装字体吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2 小时前 | 显示全部楼层
过默 发表于 2026-07-05 13:01
FishC 发表于 2026-7-5 09:08
这个错误是因为在调用  ...


不是 pip 安装,字体是系统文件,不是 Python 模块。你需要手动下载一个中文字体文件,比如 “simhei.ttf”(黑体)或 “msyh.ttf”(微软雅黑)。把下载好的 .ttf 文件放到你的 Python 脚本同一个文件夹里(或者指定路径)。然后用 pygame.font.Font("simhei.ttf", 30) 替换原来的 pygame.font.SysFont

如果不想下载,也可以直接用系统里已有的字体文件路径。Windows 下通常有:


  1. font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", 30)
复制代码


Mac 或 Linux 路径不同,请自行百度搜索。这样就能显示中文了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2 小时前 | 显示全部楼层
FishC 发表于 2026-7-5 13:02
不是 pip 安装,字体是系统文件,不是 Python 模块。你需要手动下载一个中文字体文件,比如 “simhei.t ...

  1. import pygame
  2. import sys

  3. pygame.init()
  4. WIDTH, HEIGHT = 640, 720
  5. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  6. pygame.display.set_caption("中国象棋 - 极简版")
  7. clock = pygame.time.Clock()

  8. # 颜色
  9. WHITE = (255, 255, 255)
  10. BLACK = (0, 0, 0)
  11. BROWN = (200, 160, 100)
  12. RED = (200, 0, 0)

  13. # 棋盘参数
  14. MARGIN = 40
  15. SIZE = 60  # 格子大小
  16. COLS = 8   # 横向8个格子,9条竖线
  17. ROWS = 9   # 纵向9个格子,10条横线

  18. # 棋子数据示例(位置用行列表示)
  19. pieces = [
  20.     ("帅", 4, 0, RED), ("士", 3, 0, RED), ("相", 2, 0, RED),
  21.     ("车", 0, 0, RED), ("马", 1, 0, RED), ("炮", 1, 2, RED),
  22.     ("兵", 0, 3, RED), ("兵", 2, 3, RED), ("兵", 4, 3, RED),
  23.     ("将", 4, 9, BLACK), ("士", 3, 9, BLACK), ("象", 2, 9, BLACK),
  24.     ("车", 0, 9, BLACK), ("马", 1, 9, BLACK), ("炮", 1, 7, BLACK),
  25.     ("卒", 0, 6, BLACK), ("卒", 2, 6, BLACK), ("卒", 4, 6, BLACK),
  26. ]

  27. def draw_board():
  28.     # 画背景
  29.     screen.fill(BROWN)
  30.     # 画横线
  31.     for row in range(10):
  32.         y = MARGIN + row * SIZE
  33.         pygame.draw.line(screen, BLACK, (MARGIN, y), (MARGIN + 8 * SIZE, y), 2)
  34.     # 画竖线(注意第8列只有上下两段)
  35.     for col in range(9):
  36.         x = MARGIN + col * SIZE
  37.         top = MARGIN
  38.         bottom = MARGIN + 9 * SIZE
  39.         if col == 0 or col == 8:
  40.             pygame.draw.line(screen, BLACK, (x, top), (x, bottom), 2)
  41.         else:
  42.             # 河界上段(0~4行)
  43.             pygame.draw.line(screen, BLACK, (x, top), (x, top + 4 * SIZE), 2)
  44.             # 河界下段(5~9行)
  45.             pygame.draw.line(screen, BLACK, (x, top + 5 * SIZE), (x, bottom), 2)
  46.     # 画九宫斜线(左上右下)
  47.     for (left, top) in [(3*SIZE+MARGIN, 0), (3*SIZE+MARGIN, 5*SIZE+MARGIN)]:
  48.         right = left + 2 * SIZE
  49.         bottom = top + 2 * SIZE
  50.         pygame.draw.line(screen, BLACK, (left, top), (right, bottom), 2)
  51.         pygame.draw.line(screen, BLACK, (left, bottom), (right, top), 2)
  52.     # 写“楚河 汉界”
  53.    
  54.     font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", 30)

  55.     text1 = font.render("楚 河", True, BLACK)
  56.     text2 = font.render("汉 界", True, BLACK)
  57.     screen.blit(text1, (MARGIN + 1.5 * SIZE, MARGIN + 4 * SIZE))
  58.     screen.blit(text2, (MARGIN + 4.5 * SIZE, MARGIN + 4 * SIZE))

  59. def draw_pieces():
  60.    
  61.     font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", 30)

  62.     for name, col, row, color in pieces:
  63.         x = MARGIN + col * SIZE
  64.         y = MARGIN + row * SIZE
  65.         pygame.draw.circle(screen, WHITE, (x, y), SIZE // 2 - 4)
  66.         pygame.draw.circle(screen, color, (x, y), SIZE // 2 - 4, 2)
  67.         text = font.render(name, True, color)
  68.         text_rect = text.get_rect(center=(x, y))
  69.         screen.blit(text, text_rect)

  70. def main():
  71.     running = True
  72.     while running:
  73.         for event in pygame.event.get():
  74.             if event.type == pygame.QUIT:
  75.                 running = False
  76.         draw_board()
  77.         draw_pieces()
  78.         pygame.display.flip()
  79.         clock.tick(30)
  80.     pygame.quit()
  81.     sys.exit()

  82. if __name__ == "__main__":
  83.     main()
复制代码

只显示一半棋子了,上面有两根斜线
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 1 小时前 | 显示全部楼层
FishC 发表于 2026-7-5 13:02
不是 pip 安装,字体是系统文件,不是 Python 模块。你需要手动下载一个中文字体文件,比如 “simhei.t ...

  1. import pygame
  2. import sys

  3. pygame.init()
  4. WIDTH, HEIGHT = 640, 720
  5. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  6. pygame.display.set_caption("中国象棋 - 极简版")
  7. clock = pygame.time.Clock()

  8. # 颜色
  9. WHITE = (255, 255, 255)
  10. BLACK = (0, 0, 0)
  11. BROWN = (200, 160, 100)
  12. RED = (200, 0, 0)

  13. # 棋盘参数
  14. MARGIN = 40
  15. SIZE = 60  # 格子大小
  16. COLS = 8   # 横向8个格子,9条竖线
  17. ROWS = 9   # 纵向9个格子,10条横线

  18. # 棋子数据示例(位置用行列表示)
  19. pieces = [
  20.     ("帅", 4, 0, RED), ("士", 3, 0, RED), ("相", 2, 0, RED),
  21.     ("车", 0, 0, RED), ("马", 1, 0, RED), ("炮", 1, 2, RED),
  22.     ("兵", 0, 3, RED), ("兵", 2, 3, RED), ("兵", 4, 3, RED),
  23.     ("将", 4, 9, BLACK), ("士", 3, 9, BLACK), ("象", 2, 9, BLACK),
  24.     ("车", 0, 9, BLACK), ("马", 1, 9, BLACK), ("炮", 1, 7, BLACK),
  25.     ("卒", 0, 6, BLACK), ("卒", 2, 6, BLACK), ("卒", 4, 6, BLACK),
  26. ]

  27. def draw_board():
  28.     # 画背景
  29.     screen.fill(BROWN)
  30.     # 画横线
  31.     for row in range(10):
  32.         y = MARGIN + row * SIZE
  33.         pygame.draw.line(screen, BLACK, (MARGIN, y), (MARGIN + 8 * SIZE, y), 2)
  34.     # 画竖线(注意第8列只有上下两段)
  35.     for col in range(9):
  36.         x = MARGIN + col * SIZE
  37.         top = MARGIN
  38.         bottom = MARGIN + 9 * SIZE
  39.         if col == 0 or col == 8:
  40.             pygame.draw.line(screen, BLACK, (x, top), (x, bottom), 2)
  41.         else:
  42.             # 河界上段(0~4行)
  43.             pygame.draw.line(screen, BLACK, (x, top), (x, top + 4 * SIZE), 2)
  44.             # 河界下段(5~9行)
  45.             pygame.draw.line(screen, BLACK, (x, top + 5 * SIZE), (x, bottom), 2)
  46.     # 画九宫斜线(左上右下)
  47.     for (left, top) in [(3*SIZE+MARGIN, 0), (3*SIZE+MARGIN, 5*SIZE+MARGIN)]:
  48.         right = left + 2 * SIZE
  49.         bottom = top + 2 * SIZE
  50.         pygame.draw.line(screen, BLACK, (left, top), (right, bottom), 2)
  51.         pygame.draw.line(screen, BLACK, (left, bottom), (right, top), 2)
  52.     # 写“楚河 汉界”
  53.    
  54.     font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", 30)

  55.     text1 = font.render("楚 河", True, BLACK)
  56.     text2 = font.render("汉 界", True, BLACK)
  57.     screen.blit(text1, (MARGIN + 1.5 * SIZE, MARGIN + 4 * SIZE))
  58.     screen.blit(text2, (MARGIN + 4.5 * SIZE, MARGIN + 4 * SIZE))

  59. def draw_pieces():
  60.    
  61.     font = pygame.font.Font("C:/Windows/Fonts/simhei.ttf", 30)

  62.     for name, col, row, color in pieces:
  63.         x = MARGIN + col * SIZE
  64.         y = MARGIN + row * SIZE
  65.         pygame.draw.circle(screen, WHITE, (x, y), SIZE // 2 - 4)
  66.         pygame.draw.circle(screen, color, (x, y), SIZE // 2 - 4, 2)
  67.         text = font.render(name, True, color)
  68.         text_rect = text.get_rect(center=(x, y))
  69.         screen.blit(text, text_rect)

  70. def main():
  71.     running = True
  72.     while running:
  73.         for event in pygame.event.get():
  74.             if event.type == pygame.QUIT:
  75.                 running = False
  76.         draw_board()
  77.         draw_pieces()
  78.         pygame.display.flip()
  79.         clock.tick(30)
  80.     pygame.quit()
  81.     sys.exit()

  82. if __name__ == "__main__":
  83.     main()
复制代码


卒把帅吃了,没提示
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 1 小时前 | 显示全部楼层
过默 发表于 2026-07-05 13:23
FishC 发表于 2026-7-5 13:02
不是 pip 安装,字体是 ...


你的代码目前只画了棋盘和棋子,没有写任何吃子判断或胜负规则。所以“卒把帅吃了”只是画面上被覆盖,程序并不知道游戏结束。你需要添加一个检测函数,每次移动后检查对方的帅(将)是否还在棋盘上。

简单方法:在 pieces 列表里,每次走棋后遍历,看是否还有“帅”或“将”。如果对方已经没了,就弹出胜利提示。

例如:


  1. def check_winner(pieces):
  2.     red_king = any(p[0] == '帅' for p in pieces)
  3.     black_king = any(p[0] == '将' for p in pieces)
  4.     if not red_king:
  5.         return "黑方胜!"
  6.     if not black_king:
  7.         return "红方胜!"
  8.     return None
复制代码


在你的主循环中,每次移动后调用这个函数,如果返回非 None,就用 pygame 显示文字并暂停游戏。

另外,你提到的“卒走过了河可以横吃”等规则,也只是视觉上画了,没有逻辑实现。建议先做一个简单的【点击选择 - 点击移动】交互框架,每一步都验证合法性。

如果想快速看到效果,可以先在 draw_pieces 里临时硬编码一个测试:初始就把黑将去掉,看提示是否弹出。调试通过后再完善真实走法。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-7-5 15:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表