首先添加依赖
pip install pygame
全部代码:
import pygame
import sys
# 初始化 Pygame
pygame.init()
# 屏幕尺寸
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 640
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BOARD_COLOR = (238, 154, 73) # 棋盘颜色
LINE_COLOR = (100, 100, 100) # 线条颜色
TEXT_COLOR = (255, 0, 0)
# 字体设置
try:
# 尝试加载系统字体(Windows示例)
font_path = "C:/Windows/Fonts/msyh.ttc" # 微软雅黑
chinese_font = pygame.font.Font(font_path, 36)
except:
# 回退方案:使用默认字体(可能仍显示方框)
chinese_font = pygame.font.Font(None, 36)
# 棋盘参数
GRID_SIZE = 40
BOARD_ROWS = 15
BOARD_COLS = 15
STONE_RADIUS = 18
# 计算棋盘起始位置
board_x = (SCREEN_WIDTH - (BOARD_COLS-1)*GRID_SIZE) // 2
board_y = (SCREEN_HEIGHT - (BOARD_ROWS-1)*GRID_SIZE) // 2
# 初始化屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("五子棋")
# 游戏状态
game_state = "choose_color" # choose_color/playing/game_over
current_player = 1 # 1: 黑棋, 2: 白棋
winner = None
# 初始化棋盘数据
board = [[0 for _ in range(BOARD_COLS)] for _ in range(BOARD_ROWS)]
def draw_board():
"""绘制棋盘"""
screen.fill(BOARD_COLOR)
# 绘制网格线
for row in range(BOARD_ROWS):
y = board_y + row * GRID_SIZE
pygame.draw.line(screen, LINE_COLOR, (board_x, y),
(board_x + (BOARD_COLS-1)*GRID_SIZE, y), 2)
for col in range(BOARD_COLS):
x = board_x + col * GRID_SIZE
pygame.draw.line(screen, LINE_COLOR, (x, board_y),
(x, board_y + (BOARD_ROWS-1)*GRID_SIZE), 2)
# 绘制棋子
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == 1:
pygame.draw.circle(screen, BLACK,
(board_x + col*GRID_SIZE, board_y + row*GRID_SIZE),
STONE_RADIUS)
elif board[row][col] == 2:
pygame.draw.circle(screen, WHITE,
(board_x + col*GRID_SIZE, board_y + row*GRID_SIZE),
STONE_RADIUS)
pygame.draw.circle(screen, LINE_COLOR,
(board_x + col*GRID_SIZE, board_y + row*GRID_SIZE),
STONE_RADIUS, 1)
def draw_choose_color():
"""绘制选择先手颜色界面"""
screen.fill(BOARD_COLOR)
# 标题
text = chinese_font.render("选择先手颜色", True, TEXT_COLOR)
text_rect = text.get_rect(center=(SCREEN_WIDTH//2, 100))
screen.blit(text, text_rect)
# 黑棋按钮
black_center = (SCREEN_WIDTH//2 - 120, SCREEN_HEIGHT//2)
pygame.draw.circle(screen, BLACK, black_center, 40)
# 白棋按钮
white_center = (SCREEN_WIDTH//2 + 120, SCREEN_HEIGHT//2)
pygame.draw.circle(screen, WHITE, white_center, 40)
pygame.draw.circle(screen, LINE_COLOR, white_center, 40, 2)
# 文字说明
black_text = chinese_font.render("黑棋先手", True, WHITE)
white_text = chinese_font.render("白棋先手", True, BLACK)
screen.blit(black_text, (SCREEN_WIDTH//2 - 160, SCREEN_HEIGHT//2 + 60))
screen.blit(white_text, (SCREEN_WIDTH//2 + 40, SCREEN_HEIGHT//2 + 60))
def get_board_pos(mouse_pos):
"""将鼠标位置转换为棋盘坐标"""
x, y = mouse_pos
col = round((x - board_x) / GRID_SIZE)
row = round((y - board_y) / GRID_SIZE)
if 0 <= col < BOARD_COLS and 0 <= row < BOARD_ROWS:
return (row, col)
return None
def check_win(row, col):
"""检查是否获胜"""
player = board[row][col]
directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
for dx, dy in directions:
count = 1
# 正向检查
i, j = row + dx, col + dy
while 0 <= i < BOARD_ROWS and 0 <= j < BOARD_COLS and board[i][j] == player:
count += 1
i += dx
j += dy
# 反向检查
i, j = row - dx, col - dy
while 0 <= i < BOARD_ROWS and 0 <= j < BOARD_COLS and board[i][j] == player:
count += 1
i -= dx
j -= dy
if count >= 5:
return True
return False
# 主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if game_state == "choose_color":
# 处理颜色选择
x, y = event.pos
# 检查黑棋按钮
if (x - (SCREEN_WIDTH//2 - 120))**2 + (y - SCREEN_HEIGHT//2)**2 <= 1600:
current_player = 1
game_state = "playing"
# 检查白棋按钮
elif (x - (SCREEN_WIDTH//2 + 120))**2 + (y - SCREEN_HEIGHT//2)**2 <= 1600:
current_player = 2
game_state = "playing"
elif game_state == "playing":
pos = get_board_pos(event.pos)
if pos:
row, col = pos
if board[row][col] == 0:
board[row][col] = current_player
if check_win(row, col):
winner = current_player
game_state = "game_over"
else:
current_player = 2 if current_player == 1 else 1
# 绘制界面
if game_state == "choose_color":
draw_choose_color()
elif game_state == "playing":
draw_board()
# 显示当前玩家
text = chinese_font.render(f"当前玩家: {'黑棋' if current_player == 1 else '白棋'}",
True, TEXT_COLOR)
screen.blit(text, (20, 20))
elif game_state == "game_over":
screen.fill(BOARD_COLOR)
text = chinese_font.render(f"{'黑棋' if winner == 1 else '白棋'} 胜利!", True, TEXT_COLOR)
text_rect = text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
screen.blit(text, text_rect)
pygame.display.update()
pygame.quit()