Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import Color
- class Board:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.board = [[0] * width for _ in range(height)]
- self.left = 10
- self.top = 10
- self.cell_size = 30
- def set_view(self, left, top, cell_size):
- self.left = left
- self.top = top
- self.cell_size = cell_size
- def render(self, surface):
- for row in range(self.height):
- for col in range(self.width):
- pygame.draw.rect(surface, Color('white'),
- [self.left + self.cell_size * col,
- self.top + self.cell_size * row,
- self.cell_size,
- self.cell_size], 1)
- if self.board[row][col] == 0:
- color = Color('black')
- elif self.board[row][col] == 1:
- color = Color('red')
- elif self.board[row][col] == 2:
- color = Color('blue')
- pygame.draw.rect(surface, color,
- [self.left + self.cell_size * col + 1,
- self.top + self.cell_size * row + 1,
- self.cell_size - 2,
- self.cell_size - 2])
- def get_cell(self, position):
- x, y = position
- x -= self.left
- y -= self.top
- x //= self.cell_size
- y //= self.cell_size
- return None if (y > self.height - 1 or y < 0) or (x > self.width - 1 or x < 0) else (x, y)
- def on_click(self, cell_coords):
- self.board[cell_coords[1]][cell_coords[0]] += 1
- if self.board[cell_coords[1]][cell_coords[0]] > 2:
- self.board[cell_coords[1]][cell_coords[0]] = 0
- def get_click(self, position):
- self.on_click(self.get_cell(position))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((400, 400))
- board = Board(8, 8)
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- board.get_click(event.pos)
- screen.fill((0, 0, 0))
- board.render(screen)
- pygame.display.flip()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement