|
|
Traceback (most recent call last):
File "C:\Program Files\Python38\qipan.py", line 155, in <module>
main()
File "C:\Program Files\Python38\qipan.py", line 146, in main
handle_click(event.pos)
File "C:\Program Files\Python38\qipan.py", line 130, in handle_click
success=move_piece(seleted_piece,click_col, click_row)
NameError: name 'seleted_piece' is not defined
- 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, RED), ('H', 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),
- ('P', 0, 3, RED), ('P', 2, 3, RED), ('P', 4, 3, RED), ('P', 6, 3, RED), ('P', 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),
- ('p', 0, 6, BLACK), ('p', 2, 6, BLACK), ('p', 4, 6, BLACK), ('p', 6, 6, BLACK), ('p', 8, 6, BLACK),
- ]
- selected_piece = None # 当前选中棋子的索引
- turn = 'red' # 'red' 或 'black'
- 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 get_piece_at(col, row):
- for i, (n, c, r, colr) in enumerate(pieces):
- if c == col and r == row:
- return i, (n, colr)
- 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
- if target_idx is not None:
- target_color = pieces[target_idx]
- if target_color != color: # 吃到敌方棋子
- print("吃子! 吃掉", pieces[target_idx][0]) # 控制台打印
- 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 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=='blank' and color==BLACK):
- selected_piece=i
- break
- else:
- success=move_piece(seleted_piece,click_col, click_row)
- if success:
- #
- turn='black' if turn == 'red' else 'red'
- #
- selected_piece = None
- def main():
- global selected_piece
- 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: # 左键
- handle_click(event.pos)
- draw_board()
- draw_pieces()
- pygame.display.flip()
- clock.tick(30)
- pygame.quit()
- sys.exit()
- if __name__ == "__main__":
- main()
复制代码 |
|