|
|
过默 发表于 2026-7-5 11:43
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))
- pygame.display.set_caption("Chinese Chess - Full")
- clock = pygame.time.Clock()
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- BROWN = (200, 160, 100)
- RED = (200, 0, 0)
- MARGIN = 40
- SIZE = 60
- # 棋子数据:名字,列,行,颜色
- pieces = [
- ('R', 0, 0, RED), ('马', 1, 0, RED), ('E', 2, 0, RED), ('A', 3, 0, RED),
- ('K', 4, 0, RED), ('A', 5, 0, RED), ('E', 6, 0, RED), ('H', 7, 0, RED),
- ('R', 8, 0, RED), ('C', 1, 2, RED), ('C', 7, 2, RED), ('S', 0, 3, RED),
- ('S', 2, 3, RED), ('S', 4, 3, RED), ('S', 6, 3, RED), ('S', 8, 3, RED),
- ('R', 0, 9, BLACK), ('H', 1, 9, BLACK), ('E', 2, 9, BLACK), ('A', 3, 9, BLACK),
- ('K', 4, 9, BLACK), ('A', 5, 9, BLACK), ('E', 6, 9, BLACK), ('H', 7, 9, BLACK),
- ('R', 8, 9, BLACK), ('C', 1, 7, BLACK), ('C', 7, 7, BLACK), ('S', 0, 6, BLACK),
- ('S', 2, 6, BLACK), ('S', 4, 6, BLACK), ('S', 6, 6, BLACK), ('S', 8, 6, BLACK)
- ]
- selected_piece = None
- turn = 'red'
- def draw_board():
- screen.fill(BROWN)
- # 横线
- for row in range(10):
- y = MARGIN + row * SIZE
- pygame.draw.line(screen, BLACK, (MARGIN, y), (MARGIN + 8 * SIZE, y), 2)
- # 竖线
- for col in range(9):
- x = MARGIN + col * SIZE
- top = MARGIN
- bottom = MARGIN + 9 * SIZE
- if col == 0 or col == 8:
- pygame.draw.line(screen, BLACK, (x, top), (x, bottom), 2)
- else:
- # 上半
- pygame.draw.line(screen, BLACK, (x, top), (x, top + 4 * SIZE), 2)
- # 下半
- pygame.draw.line(screen, BLACK, (x, top + 5 * SIZE), (x, bottom), 2)
- # 九宫斜线
- for (left, top) in [(MARGIN + 3 * SIZE, MARGIN), (MARGIN + 3 * SIZE, MARGIN),
- (MARGIN + 3 * SIZE, MARGIN + 7 * SIZE), (MARGIN + 3 * SIZE, MARGIN + 7 * SIZE)]:
- right = left + 2 * SIZE
- bottom = top + 2 * SIZE
- pygame.draw.line(screen, BLACK, (left, top), (right, bottom), 2)
- pygame.draw.line(screen, BLACK, (left, bottom), (right, top), 2)
- # 楚河汉界
- font = pygame.font.Font("simsun", 30)
- text1 = font.render("CHU HE", True, BLACK)
- text2 = font.render("HAN JIE", True, BLACK)
- screen.blit(text1, (MARGIN + 1.5 * SIZE, MARGIN + 4 * SIZE))
- screen.blit(text2, (MARGIN + 4.5 * SIZE, MARGIN + 4 * SIZE))
- def draw_pieces():
- font = pygame.font.Font(None, 32)
- for name, col, row, color in pieces:
- x = MARGIN + col * SIZE
- y = MARGIN + row * SIZE
- pygame.draw.circle(screen, WHITE, (x, y), SIZE // 2 - 4)
- pygame.draw.circle(screen, color, (x, y), SIZE // 2 - 4, 2)
- text = font.render(name, True, color)
- text_rect = text.get_rect(center=(x, y))
- screen.blit(text, text_rect)
- def get_click_pos(mouse_pos):
- x, y = mouse_pos
- col = round((x - MARGIN) / SIZE)
- row = round((y - MARGIN) / SIZE)
- if 0 <= col <= 8 and 0 <= row <= 9:
- return col, row
- return None, None
- def move_piece(start_idx, end_col, end_row):
- global pieces, turn
- name, col, row, color = pieces[start_idx]
- # 判断目标位置是否有棋子(吃子或空位)
- target_idx = None
- for i, (n, c, r, colr) in enumerate(pieces):
- if c == end_col and r == end_row:
- target_idx = i
- break
- # ----------------- 走法合法性检查 -----------------
- def is_valid_move(name, col, row, end_col, end_row, pieces_list):
- dx = end_col - col
- dy = end_row - row
- # 马走日字
- if name in ('M', 'H'): # 根据你的棋子名字调整,红马='M',黑马='H'
- if (abs(dx), abs(dy)) not in [(1,2), (2,1)]:
- return False # 不是日字
- # 计算蹩马腿位置
- if abs(dx) == 1: # 横向走1,纵向走2
- leg_col, leg_row = col, row + dy//2
- else: # 横向走2,纵向走1
- leg_col, leg_row = col + dx//2, row
- # 检查该位置是否有棋子
- for (n, c, r, clr) in pieces_list:
- if c == leg_col and r == leg_row:
- return False # 蹩马腿
- return True
- else:
- # 其他棋子暂时允许任意移动(后续可扩展)
- return True
- if not is_valid_move(name, col, row, end_col, end_row, pieces):
- return False # 不合法,取消移动
- # ----------------- 执行移动(含吃子) -----------------
- if target_idx is not None:
- target_color = pieces[target_idx][3]
- if target_color != color: # 吃子
- pieces.pop(target_idx)
- # 重新找到自己的索引(因为列表变动)
- new_idx = None
- for i, (n, c, r, colr) in enumerate(pieces):
- if c == col and r == row and colr == color:
- new_idx = i
- break
- if new_idx is not None:
- pieces[new_idx] = (name, end_col, end_row, color)
- return True
- else:
- return False # 不能吃己方棋子
- else:
- # 空位直接移动
- pieces[start_idx] = (name, end_col, end_row, color)
- return True
- def handle_click(mouse_pos):
- global selected_piece, turn
- click_col, click_row = get_click_pos(mouse_pos)
- if click_col is None:
- return
- if selected_piece is None:
- # 尝试选中当前回合方的棋子
- for i, (name, col, row, color) in enumerate(pieces):
- if col == click_col and row == click_row:
- if (turn == 'red' and color == RED) or (turn == 'black' and color == BLACK):
- selected_piece = i
- break
- else:
- # 尝试移动
- if move_piece(selected_piece, click_col, click_row):
- # 成功移动,切换回合
- turn = 'black' if turn == 'red' else 'red'
- # 无论移动成功与否,清除选中状态
- selected_piece = None
- def main():
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN:
- handle_click(event.pos)
- draw_board()
- draw_pieces()
- pygame.display.flip()
- clock.tick(30)
- pygame.quit()
- sys.exit()
- if __name__ == "__main__":
- 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'.
>>>
|
|