Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import Rect, time
- from random import randint
- class board:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.board = [[(0, 0, 0)] * height for _ in range(width)]
- 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):
- for i in range(self.width):
- for j in range(self.height):
- a = Rect(self.left + self.cell_size * i + 1, self.top + self.cell_size * j + 1, self.cell_size - 1, self.cell_size - 1)
- screen.fill((randint(1, 255), randint(1, 255), randint(1, 255)), a)
- pygame.draw.rect(screen, pygame.Color("white"), (
- self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size),
- 1)
- def get_cell(self, mouse_pos):
- b, c = mouse_pos
- b -= self.left
- c -= self.top
- if b < 0 or c < 0:
- return [-1, -1]
- b //= self.cell_size
- c //= self.cell_size
- if b >= self.width or c >= self.height:
- return [-1, -1]
- return [b, c]
- def on_click(self, cell_coords):
- self.board[cell_coords[0]][cell_coords[1]] = (randint(1, 255), randint(1, 255), randint(1, 255))
- def get_click(self, mouse_pos):
- b, c = self.get_cell(mouse_pos)
- self.on_click((b, c))
- pygame.init()
- a = board(38, 21)
- a.set_view(10, 10, 50)
- width = 1920
- height = 1080
- size = (width, height)
- v = 0
- screen = pygame.display.set_mode(size)
- clock = pygame.time.Clock()
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- a.get_click((event.pos))
- a.render()
- clock.tick(1000)
- pygame.display.flip()
- v += 1
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement