|
|
- import pygame
- import sys
- pygame.init()
- WIDTH, HEIGHT = 640, 720
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Chinese Chess")
- 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, BLACK), ("H", 1, 0, BLACK), ("E", 2, 0, BLACK),
- ("A", 3, 0, BLACK), ("K", 4, 0, BLACK), ("A", 5, 0, BLACK),
- ("E", 6, 0, BLACK), ("H", 7, 0, BLACK), ("R", 8, 0, BLACK),
- ("C", 1, 2, BLACK), ("C", 7, 2, BLACK),
- ("P", 0, 3, BLACK), ("P", 2, 3, BLACK), ("P", 4, 3, BLACK),
- ("P", 6, 3, BLACK), ("P", 8, 3, BLACK),
- ("R", 0, 9, RED), ("H", 1, 9, RED), ("E", 2, 9, RED),
- ("A", 3, 9, RED), ("K", 4, 9, RED), ("A", 5, 9, RED),
- ("E", 6, 9, RED), ("H", 7, 9, RED), ("R", 8, 9, RED),
- ("C", 1, 7, RED), ("C", 7, 7, RED),
- ("P", 0, 6, RED), ("P", 2, 6, RED), ("P", 4, 6, RED),
- ("P", 6, 6, RED), ("P", 8, 6, RED)
- ]
- 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 + 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(None, 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
- def handle_click(col, row):
- global selected_piece, turn
- # 检查点击位置是否有棋子
- index = None
- for i, (name, pc, pr, pcolor) in enumerate(pieces):
- if pc == col and pr == row:
- index = i
- break
- if selected_piece is None:
- # 没有选中任何棋子,尝试选中一个己方棋子
- if index is not None and pieces[index][3] == (RED if turn == 'red' else BLACK):
- selected_piece = index
- else:
- # 已选中一个棋子,尝试移动
- name, sc, sr, scolor = pieces[selected_piece]
- # 简单判断:不能吃己方棋子
- if index is not None and pieces[index][3] == scolor:
- # 点击了己方另一个棋子,切换选择
- selected_piece = index
- return
- # 执行移动(不校验规则)
- pieces[selected_piece] = (name, col, row, scolor)
- # 如果目标有对方棋子,吃掉
- if index is not None and pieces[index][3] != scolor:
- pieces.pop(index)
- # 切换回合
- turn = 'black' if turn == 'red' else 'red'
- selected_piece = None
-
- def move_piece(start_idx, end_col, end_row):
-   global pieces
-     name, col, row, color = pieces[start_idx]
-     # 判断是否能吃子
-     for i, (n, c, r, colr) in enumerate(pieces):
-         if c == end_col and r == end_row:
-             if colr != color:  # 敌方
-                 pieces.pop(i)
-                 # 因为i可能小于start_idx,需要调整
-                 if i < start_idx:
-                     start_idx -= 1
-                 pieces[start_idx] = (name, end_col, end_row, color)
-                 return True
-             else:
-                 return False  # 友方不能吃
-     # 空位
-     pieces[start_idx] = (name, end_col, end_row, color)
-     return True
- def main():
- global selected_piece, turn
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1: # 左键
- pos = get_click_pos(event.pos)
- if pos is not None:
- handle_click(pos[0], pos[1])
- draw_board()
- # 高亮选中的棋子
- if selected_piece is not None:
- name, col, row, color = pieces[selected_piece]
- x = MARGIN + col * SIZE
- y = MARGIN + row * SIZE
- pygame.draw.circle(screen, (255, 255, 0), (x, y), SIZE // 2 - 2, 3)
- draw_pieces()
- pygame.display.flip()
- clock.tick(30)
- pygame.quit()
- sys.exit()
- if __name__ == "__main__":
- main()
复制代码
怎么弄?什么main |
|