Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import pygame
  2. from pygame import Color
  3.  
  4.  
  5. class Board:
  6.     def __init__(self, width, height):
  7.         self.width = width
  8.         self.height = height
  9.         self.board = [[0] * width for _ in range(height)]
  10.  
  11.         self.left = 10
  12.         self.top = 10
  13.         self.cell_size = 30
  14.  
  15.     def set_view(self, left, top, cell_size):
  16.         self.left = left
  17.         self.top = top
  18.         self.cell_size = cell_size
  19.  
  20.     def render(self, surface):
  21.         for row in range(self.height):
  22.             for col in range(self.width):
  23.                 pygame.draw.rect(surface, Color('white'),
  24.                                 [self.left + self.cell_size * col,
  25.                                 self.top + self.cell_size * row,
  26.                                 self.cell_size,
  27.                                 self.cell_size], 1)
  28.                 if self.board[row][col] == 0:
  29.                     color = Color('black')
  30.                 elif self.board[row][col] == 1:
  31.                     color = Color('red')
  32.                 elif self.board[row][col] == 2:
  33.                     color = Color('blue')
  34.                 pygame.draw.rect(surface, color,
  35.                                 [self.left + self.cell_size * col + 1,
  36.                                 self.top + self.cell_size * row + 1,
  37.                                 self.cell_size - 2,
  38.                                 self.cell_size - 2])
  39.  
  40.  
  41.     def get_cell(self, position):
  42.         x, y = position
  43.         x -= self.left
  44.         y -= self.top
  45.         x //= self.cell_size
  46.         y //= self.cell_size
  47.         return None if (y > self.height - 1 or y < 0) or (x > self.width - 1 or x < 0) else (x, y)
  48.  
  49.     def on_click(self, cell_coords):
  50.         self.board[cell_coords[1]][cell_coords[0]] += 1
  51.         if self.board[cell_coords[1]][cell_coords[0]] > 2:
  52.             self.board[cell_coords[1]][cell_coords[0]] = 0
  53.  
  54.     def get_click(self, position):
  55.         self.on_click(self.get_cell(position))
  56.  
  57.  
  58. def main():
  59.     pygame.init()
  60.     screen = pygame.display.set_mode((400, 400))
  61.     board = Board(8, 8)
  62.     running = True
  63.     while running:
  64.         for event in pygame.event.get():
  65.             if event.type == pygame.QUIT:
  66.                 running = False
  67.             if event.type == pygame.MOUSEBUTTONDOWN:
  68.                 board.get_click(event.pos)            
  69.         screen.fill((0, 0, 0))
  70.         board.render(screen)
  71.         pygame.display.flip()
  72.  
  73.  
  74. if __name__ == '__main__':
  75.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement