You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pygame
# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((600, 600)) # 设置窗口长宽
pygame.display.set_caption('五子棋-EduCoder') # 设置 pygame 窗口名
space = 20 # 四周留下的边距
cell_size = 40 # 每个格子大小
cell_num = 15 # 设置线条数
chess_arr = [] # 创建列表记录落子位置
def get_one_dire_num(lx, ly, dx, dy, m):
tx = lx
ty = ly
s = 0
while True:
tx += dx
ty += dy
if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0:
return s
s += 1
def check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)]
for x, y, c in chess_arr:
if c == flag:
m[y][x] = 1
lx = chess_arr[-1][0]
ly = chess_arr[-1][1]
dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
[(-1, 1), (1, -1)]]
for dire1, dire2 in dire_arr:
dx, dy = dire1
num1 = get_one_dire_num(lx, ly, dx, dy, m)
dx, dy = dire2
num2 = get_one_dire_num(lx, ly, dx, dy, m)
if num1 + num2 + 1 >= 5:
return True
return False
flag = 1
game_state = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
if game_state == 1:
xi = int(round((x - space) * 1.0 / cell_size))
yi = int(round((y - space) * 1.0 / cell_size))
if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi, 1) not in chess_arr and (xi, yi, 2) not in chess_arr:
chess_arr.append((xi, yi, flag))
if check_win(chess_arr, flag):
game_state = 2 if flag == 1 else 3
else:
flag = 2 if flag == 1 else 1
else:
if 200 <= x <= 400 and 360 <= y <= 410:
chess_arr = []
game_state = 1
flag = 1
elif 200 <= x <= 400 and 430 <= y <= 480:
pygame.quit()
exit()
screen.fill((204, 153, 102))
for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space),
(x + space, cell_size * (cell_num - 1) + space), 1)
for y in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space),
(cell_size * (cell_num - 1) + space, y + space), 1)
for x, y, c in chess_arr:
chess_color = (30, 30, 30) if c == 1 else (225, 225, 225)
pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16)
if (4, 4, 1) not in chess_arr and (4, 4, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1)
if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1)
if (10, 4, 1) not in chess_arr and (10, 4, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1)
if (4, 10, 1) not in chess_arr and (4, 10, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1)
if (7, 7, 1) not in chess_arr and (7, 7, 2) not in chess_arr:
pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1)
if game_state != 1:
# 半透明遮罩层
overlay = pygame.Surface((680, 680), pygame.SRCALPHA)
overlay.fill((255, 255, 255, 180))
screen.blit(overlay, (0,0))
myfont = pygame.font.Font(None, 60)
white = 210, 210, 0
win_text = "%s wins" % ('Black' if game_state == 2 else 'White')
textImage = myfont.render(win_text, True, white)
# 修改文字位置x坐标从220改为200y坐标从320改为280
screen.blit(textImage, (200, 280)) # 主要修改位置
button_font = pygame.font.Font(None, 36)
pygame.draw.rect(screen, (0, 200, 0), (200, 360, 200, 50))
text_play = button_font.render('Play Again', True, (255, 255, 255))
screen.blit(text_play, (220, 370))
pygame.draw.rect(screen, (200, 0, 0), (200, 430, 200, 50))
text_quit = button_font.render('Quit', True, (255, 255, 255))
screen.blit(text_quit, (260, 440))
pygame.display.update()