import pygame
WHITE = [255, 255, 255]
BLACK = [0, 0, 0]
layout = [[0] * 15 for _ in range(15)]
black = 1
white = 2
black_turn = True
class Layout(object):
def __init__(self, x, y, color, radius):
self._x = x
self._y = y
self._color = color
self._radius = radius
def is_victory(x1, y1):
count = 1
#上下
for i in range(1, 5):
if y1 + i > 14:
break
elif layout[x1][y1] == layout[x1][y1 + i]:
count += 1
else:
break
for i in range(1, 5):
if y1 - i < 0:
break
elif layout[x1][y1] == layout[x1][y1 - i]:
count += 1
else:
break
print(count)
if count >= 5:
print('%s胜利' % ('黑棋' if black_turn else '白棋'))
return True
#左右
count = 1
for i in range(1, 5):
if x1 + i > 14:
break
elif layout[x1][y1] == layout[x1 + i][y1]:
count += 1
else:
break
for i in range(1, 5):
if x1 - i < 0:
break
elif layout[x1][y1] == layout[x1 - i][y1]:
count += 1
else:
break
if count >= 5:
print('%s胜利' % ('黑棋' if black_turn else '白棋'))
return True
#斜方向
count = 1
for i in range(1, 5):
if x1 + i > 14 or y1 + i > 14:
break
elif layout[x1][y1] == layout[x1 + i][y1 + i]:
count += 1
else:
break
for i in range(1, 5):
if x1 - i < 0 or y1 - i < 0:
break
elif layout[x1][y1] == layout[x1 - i][y1 - i]:
count += 1
else:
break
if count >= 5:
print('%s胜利' % ('黑棋' if black_turn else '白棋'))
return True
count = 1
for i in range(1, 5):
if x1 + i > 14 or y1 - i < 0:
break
elif layout[x1][y1] == layout[x1 + i][y1 - i]:
count += 1
else:
break
for i in range(1, 5):
if x1 - i < 0 or y1 + i > 14:
break
elif layout[x1][y1] == layout[x1 - i][y1 + i]:
count += 1
else:
break
if count >= 5:
print('%s胜利' % ('黑棋' if black_turn else '白棋'))
return True
def main():
global black_turn, layout
pygame.display.init()
screen = pygame.display.set_mode((640, 640))
pygame.display.set_caption('五子棋')
screen.fill([160, 80, 0])
for i in range(15):
pygame.draw.line(screen, [0, 0, 0], (40 * (i + 1), 40), (40 * (i + 1), 600), 1)
pygame.draw.line(screen, [0, 0, 0], (40, 40 * (i + 1)), (600, 40 * (i + 1)), 1)
pygame.draw.rect(screen, [0, 0, 0], [35, 35, 570, 570], 4)
pygame.draw.circle(screen, [0, 0, 0], (320, 320), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (160, 160), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (160, 480), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (480, 480), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (480, 160), 5, 0)
running = True
is_refresh = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and is_refresh:
pos = event.pos
x, y = pos[0], pos[1]
if 40 < x < 600 and 40< y < 600:
row = round(x / 40)
col = round(y / 40)
if layout[row - 1][col - 1] == 0:
layout[row - 1][col - 1] = black if black_turn else white
x1 = row - 1
y1 = col - 1
pygame.draw.circle(screen, BLACK if black_turn else WHITE, [row * 40, col * 40], 20, 0)
if is_victory(x1, y1):
is_refresh = False
black_turn = not black_turn
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_F2:
layout = [[0] * 15 for _ in range(15)]
screen.fill([160, 80, 0])
for i in range(15):
pygame.draw.line(screen, [0, 0, 0], (40 * (i + 1), 40), (40 * (i + 1), 600), 1)
pygame.draw.line(screen, [0, 0, 0], (40, 40 * (i + 1)), (600, 40 * (i + 1)), 1)
pygame.draw.rect(screen, [0, 0, 0], [35, 35, 570, 570], 4)
pygame.draw.circle(screen, [0, 0, 0], (320, 320), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (160, 160), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (160, 480), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (480, 480), 5, 0)
pygame.draw.circle(screen, [0, 0, 0], (480, 160), 5, 0)
is_refresh = True
print(event.key)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()