Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import pygame
  2.  
  3. from pygame import Rect, time
  4.  
  5. from random import randint
  6. class board:
  7. def __init__(self, width, height):
  8. self.width = width
  9. self.height = height
  10. self.board = [[(0, 0, 0)] * height for _ in range(width)]
  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):
  21. for i in range(self.width):
  22. for j in range(self.height):
  23. a = Rect(self.left + self.cell_size * i + 1, self.top + self.cell_size * j + 1, self.cell_size - 1, self.cell_size - 1)
  24. screen.fill((randint(1, 255), randint(1, 255), randint(1, 255)), a)
  25. pygame.draw.rect(screen, pygame.Color("white"), (
  26. self.left + self.cell_size * i, self.top + self.cell_size * j, self.cell_size, self.cell_size),
  27. 1)
  28.  
  29.  
  30. def get_cell(self, mouse_pos):
  31. b, c = mouse_pos
  32. b -= self.left
  33. c -= self.top
  34. if b < 0 or c < 0:
  35. return [-1, -1]
  36. b //= self.cell_size
  37. c //= self.cell_size
  38. if b >= self.width or c >= self.height:
  39. return [-1, -1]
  40. return [b, c]
  41.  
  42. def on_click(self, cell_coords):
  43. self.board[cell_coords[0]][cell_coords[1]] = (randint(1, 255), randint(1, 255), randint(1, 255))
  44.  
  45. def get_click(self, mouse_pos):
  46.  
  47. b, c = self.get_cell(mouse_pos)
  48. self.on_click((b, c))
  49.  
  50. pygame.init()
  51. a = board(38, 21)
  52. a.set_view(10, 10, 50)
  53. width = 1920
  54. height = 1080
  55. size = (width, height)
  56. v = 0
  57. screen = pygame.display.set_mode(size)
  58. clock = pygame.time.Clock()
  59. running = True
  60. while running:
  61. for event in pygame.event.get():
  62. if event.type == pygame.QUIT:
  63. running = False
  64. if event.type == pygame.MOUSEBUTTONDOWN:
  65. a.get_click((event.pos))
  66. a.render()
  67. clock.tick(1000)
  68. pygame.display.flip()
  69. v += 1
  70. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement