[python 实例]教你如何制作五子棋

 hi,大家好,今天就来将你如何用python制作五子棋

话不多说,直接上代码!

import pygame

EMPTY = 0

BLACK = 1

WHITE = 2

black_color = [0, 0, 0]

white_color = [255, 255, 255]

class RenjuBoard(object):

    def __init__(self):

        self._board = [[]] * 15

        self.reset()

    def reset(self):

        for row in range(len(self._board)):

            self._board[row] = [EMPTY] * 15

    def move(self, row, col, is_black):

        if self._board[row][col] == EMPTY:

            self._board[row][col] = BLACK if is_black else WHITE

            return True

        return False

    def draw(self, screen):

        for index in range(1, 16):

            pygame.draw.line(screen, black_color,

                            [40, 40 * index], [600, 40 * index], 1)

            pygame.draw.line(screen, black_color,

                            [40 * index, 40], [40 * index, 600], 1)

        pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4)

        pygame.draw.circle(screen, black_color, [320, 320], 5, 0)

        pygame.draw.circle(screen, black_color, [160, 160], 5, 0)

        pygame.draw.circle(screen, black_color, [480, 480], 5, 0)

        pygame.draw.circle(screen, black_color, [480, 160], 5, 0)

        pygame.draw.circle(screen, black_color, [160, 480], 5, 0)

        for row in range(len(self._board)):

            for col in range(len(self._board[row])):

                if self._board[row][col] != EMPTY:

                    ccolor = black_color \

                        if self._board[row][col] == BLACK else white_color

                    pos = [40 * (col + 1), 40 * (row + 1)]

                    pygame.draw.circle(screen, ccolor, pos, 20, 0)

def main():

    board = RenjuBoard()

    is_black = True

    pygame.init()

    pygame.display.set_caption('五子棋')

    screen = pygame.display.set_mode([640, 640])

    screen.fill([255, 255, 0])

    board.draw(screen)

    pygame.display.flip()

    running = True

    while running:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                running = False

            elif event.type == pygame.KEYUP:

                pass

            elif event.type == pygame.MOUSEBUTTONDOWN\

                    and event.button == 1:

                x, y = event.pos

                row = round((y - 40) / 40)

                col = round((x - 40) / 40)

                if board.move(row, col, is_black):

                    is_black = not is_black

                    screen.fill([255, 255, 0])

                    board.draw(screen)

                    pygame.display.flip()

    pygame.quit()

if __name__ == '__main__':

    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值