import pygame
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
GRID_SIZE = 40
BOARD_SIZE = 15
PIECE_RADIUS = GRID_SIZE // 2 - 2
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN=(144,238,144)
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("python版五子棋")
board = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]
running = True
turn = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
col = x // GRID_SIZE
row = y // GRID_SIZE
if 0 <= row < BOARD_SIZE and 0 <= col < BOARD_SIZE and board[row][col] == 0:
board[row][col] = turn
turn = 3 - turn
screen.fill(GREEN)
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
pygame.draw.rect(screen, BLACK, (col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE), 2)
if board[row][col] == 1:
pygame.draw.circle(screen, BLACK, (col * GRID_SIZE + GRID_SIZE // 2, row * GRID_SIZE + GRID_SIZE // 2), PIECE_RADIUS)
elif board[row][col] == 2:
pygame.draw.circle(screen, WHITE, (col * GRID_SIZE + GRID_SIZE // 2, row * GRID_SIZE + GRID_SIZE // 2), PIECE_RADIUS)
pygame.display.flip()
pygame.quit()
这个小程序要求两个用户依次点击棋盘格子,黑子先下。