Karasick

Untitled

May 11th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. class BlockModel:
  2.     def __init__(self, x1, y1, x2, y2, color):
  3.         self._x1 = x1
  4.         self._y1 = y1
  5.         self._x2 = x2
  6.         self._y2 = y2
  7.         self._color = color
  8.  
  9.  
  10.     def move(self, dx, dy):
  11.         self._x1 += dx
  12.         self._x2 += dx
  13.         self._y1 += dy
  14.         self._y2 += dy
  15.  
  16.  
  17.     def get_shift_after_click(self):
  18.         """Функция, которая возвращает, как должен сдвинуться блок после клика по нему"""
  19.         return (1, -1) # Пусть, к примеру, двигается на клетку вправо и вверх
  20.  
  21.  
  22. class View:
  23.     _canvas = ...
  24.     _cell_size = 70
  25.     def __init__(self):
  26.         pass
  27.  
  28.  
  29.  
  30. class BlockView(View):
  31.     def __init__(self, block:BlockModel):
  32.         self._block = block
  33.         self._canvas_id = None
  34.  
  35.  
  36.     def show(self):
  37.         x1 = self._block._x1 * self._cell_size
  38.         x2 = self._block._x2 * self._cell_size
  39.         y1 = self._block._y1 * self._cell_size
  40.         y2 = self._block._y2 * self._cell_size
  41.         color = self._block._color
  42.  
  43.         self._canvas_id = _canvas.create_rectangle(
  44.             x1, x2, y1, y2, fill=color,
  45.             activefill="white", outline='#CD853F', width='2')
  46.  
  47.  
  48.     def hide(self):
  49.         self._canvas.delete(self._canvas_id)
  50.         self._canvas_id = None
  51.  
  52.  
  53.  
  54. class BlockPresenter:
  55.     def __init__(self, model:BlockModel, view:BlockView):
  56.         self.model = model
  57.         self.view = view
  58.         self.view.show()
  59.         self._create_button_bind()
  60.  
  61.  
  62.     def _create_button_bind(self):
  63.         canvas = self.view._canvas
  64.         id = self.view._canvas_id
  65.             # вместо отслеживания всех кликов и проверки на пересечение
  66.             # поставим отслеживание клика для каждого блока отдельно
  67.         canvas.tag_bind(id, "<Button-1>", self._mouse_click)
  68.  
  69.  
  70.     def _mouse_click(self, event):
  71.         (dx, dy) = self.model.get_shift_after_click()
  72.         self.move(dx, dy)
  73.  
  74.  
  75.     def move(self, dx, dy):
  76.         self.view.hide()
  77.         self.model.move(dx, dy)
  78.         self.view.show()
Add Comment
Please, Sign In to add comment