鱼C论坛

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

怎么学象棋

[复制链接]
 楼主| 发表于 半小时前 | 显示全部楼层
过默 发表于 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
回复 支持 反对

使用道具 举报

 楼主| 发表于 9 分钟前 | 显示全部楼层
过默 发表于 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
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-7-5 12:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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