parent
76fed91c2a
commit
e9b7f4cab8
@ -0,0 +1,82 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Created on Sat May 24 22:28:35 2025
|
||||||
|
|
||||||
|
@author: 18193
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode((600, 600))
|
||||||
|
pygame.display.set_caption('五子棋-EduCoder')
|
||||||
|
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 for _ in range(cell_num)] for _ 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 game_state == 1 and event.type == pygame.MOUSEBUTTONUP:
|
||||||
|
x, y = pygame.mouse.get_pos()
|
||||||
|
xi = int(round((x - space) / cell_size))
|
||||||
|
yi = int(round((y - space) / cell_size))
|
||||||
|
if 0 <= xi < cell_num and 0 <= yi < cell_num:
|
||||||
|
if (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
|
||||||
|
screen.fill((204, 153, 102))
|
||||||
|
for x in range(cell_num):
|
||||||
|
pygame.draw.line(screen, (200, 200, 200), (x * cell_size + space, space), (x * cell_size + space, cell_size * (cell_num - 1) + space), 1)
|
||||||
|
for y in range(cell_num):
|
||||||
|
pygame.draw.line(screen, (200, 200, 200), (space, y * cell_size + space), (cell_size * (cell_num - 1) + space, y * cell_size + 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)
|
||||||
|
points = [(4,4), (10,10), (10,4), (4,10), (7,7)]
|
||||||
|
for px, py in points:
|
||||||
|
if (px, py, 1) not in chess_arr and (px, py, 2) not in chess_arr:
|
||||||
|
pygame.draw.circle(screen, (0, 0, 0), (px * cell_size + space, py * cell_size + space), 2)
|
||||||
|
if game_state != 1:
|
||||||
|
myfont = pygame.font.Font(None, 60)
|
||||||
|
white = (210, 210, 0)
|
||||||
|
win_text = "Black Wins!" if game_state == 2 else "White Wins!"
|
||||||
|
textImage = myfont.render(win_text, True, white)
|
||||||
|
screen.blit(textImage, (260, 320))
|
||||||
|
pygame.display.update()
|
Loading…
Reference in new issue