pygame 五子棋

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值