CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Day01-15/code/Day10/renju.py
Views: 729
import pygame12EMPTY = 03BLACK = 14WHITE = 256black_color = [0, 0, 0]7white_color = [255, 255, 255]8910class RenjuBoard(object):1112def __init__(self):13self._board = [[]] * 1514self.reset()1516def reset(self):17for row in range(len(self._board)):18self._board[row] = [EMPTY] * 151920def move(self, row, col, is_black):21if self._board[row][col] == EMPTY:22self._board[row][col] = BLACK if is_black else WHITE23return True24return False2526def draw(self, screen):27for index in range(1, 16):28pygame.draw.line(screen, black_color,29[40, 40 * index], [600, 40 * index], 1)30pygame.draw.line(screen, black_color,31[40 * index, 40], [40 * index, 600], 1)32pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4)33pygame.draw.circle(screen, black_color, [320, 320], 5, 0)34pygame.draw.circle(screen, black_color, [160, 160], 5, 0)35pygame.draw.circle(screen, black_color, [480, 480], 5, 0)36pygame.draw.circle(screen, black_color, [480, 160], 5, 0)37pygame.draw.circle(screen, black_color, [160, 480], 5, 0)38for row in range(len(self._board)):39for col in range(len(self._board[row])):40if self._board[row][col] != EMPTY:41ccolor = black_color \42if self._board[row][col] == BLACK else white_color43pos = [40 * (col + 1), 40 * (row + 1)]44pygame.draw.circle(screen, ccolor, pos, 20, 0)454647def main():48board = RenjuBoard()49is_black = True50pygame.init()51pygame.display.set_caption('五子棋')52screen = pygame.display.set_mode([640, 640])53screen.fill([255, 255, 0])54board.draw(screen)55pygame.display.flip()56running = True57while running:58for event in pygame.event.get():59if event.type == pygame.QUIT:60running = False61elif event.type == pygame.KEYUP:62pass63elif event.type == pygame.MOUSEBUTTONDOWN\64and event.button == 1:65x, y = event.pos66row = round((y - 40) / 40)67col = round((x - 40) / 40)68if board.move(row, col, is_black):69is_black = not is_black70screen.fill([255, 255, 0])71board.draw(screen)72pygame.display.flip()73pygame.quit()747576if __name__ == '__main__':77main()787980