Guest User

Untitled

a guest
Jan 18th, 2019
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import copy
  2. import pygame
  3.  
  4. pygame.init()
  5. size = 420, 420
  6. screen = pygame.display.set_mode(size)
  7. clock = pygame.time.Clock()
  8.  
  9.  
  10. class Board:
  11. # создание поля
  12. def __init__(self, width, height):
  13. self.width = width
  14. self.height = height
  15. self.board = [[0] * width for _ in range(height)]
  16. # значения по умолчанию
  17. self.left = 10
  18. self.top = 10
  19. self.cell_size = 30
  20.  
  21. def render(self):
  22. for y in range(self.height):
  23. for x in range(self.width):
  24. pygame.draw.rect(screen, pygame.Color(255, 255, 255),
  25. (x * self.cell_size + self.left, y * self.cell_size + self.top, self.cell_size,
  26. self.cell_size), 1)
  27.  
  28. # настройка внешнего вида
  29. def set_view(self, left, top, cell_size):
  30. self.left = left
  31. self.top = top
  32. self.cell_size = cell_size
  33.  
  34. # cell - кортеж (x, y)
  35. def on_click(self, cell):
  36. # заглушка для реальных игровых полей
  37. pass
  38.  
  39. def get_cell(self, mouse_pos):
  40. cell_x = (mouse_pos[0] - self.left) // self.cell_size
  41. cell_y = (mouse_pos[1] - self.top) // self.cell_size
  42. if cell_x < 0 or cell_x >= self.width or cell_y < 0 or cell_y >= self.height:
  43. return None
  44. return cell_x, cell_y
  45.  
  46. def get_click(self, mouse_pos):
  47. cell = self.get_cell(mouse_pos)
  48. if cell and cell < (self.width, self.height):
  49. self.on_click(cell)
  50.  
  51.  
  52. class Lines(Board):
  53. def __init__(self, width, height):
  54. super().__init__(width, height)
  55. self.selected_cell = None
  56.  
  57. board = Lines(10, 10)
  58. board.set_view(10, 10, 40)
  59.  
  60. ticks = 0
  61.  
  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.  
  70. screen.fill((0, 0, 0))
  71. board.render()
  72. if ticks == 50:
  73. ticks = 0
  74. pygame.display.flip()
  75. clock.tick(50)
  76. ticks += 1
  77. pygame.quit()
RAW Paste Data