Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BlockModel:
- def __init__(self, x1, y1, x2, y2, color):
- self._x1 = x1
- self._y1 = y1
- self._x2 = x2
- self._y2 = y2
- self._color = color
- def move(self, dx, dy):
- self._x1 += dx
- self._x2 += dx
- self._y1 += dy
- self._y2 += dy
- def get_shift_after_click(self):
- """Функция, которая возвращает, как должен сдвинуться блок после клика по нему"""
- return (1, -1) # Пусть, к примеру, двигается на клетку вправо и вверх
- class View:
- _canvas = ...
- _cell_size = 70
- def __init__(self):
- pass
- class BlockView(View):
- def __init__(self, block:BlockModel):
- self._block = block
- self._canvas_id = None
- def show(self):
- x1 = self._block._x1 * self._cell_size
- x2 = self._block._x2 * self._cell_size
- y1 = self._block._y1 * self._cell_size
- y2 = self._block._y2 * self._cell_size
- color = self._block._color
- self._canvas_id = _canvas.create_rectangle(
- x1, x2, y1, y2, fill=color,
- activefill="white", outline='#CD853F', width='2')
- def hide(self):
- self._canvas.delete(self._canvas_id)
- self._canvas_id = None
- class BlockPresenter:
- def __init__(self, model:BlockModel, view:BlockView):
- self.model = model
- self.view = view
- self.view.show()
- self._create_button_bind()
- def _create_button_bind(self):
- canvas = self.view._canvas
- id = self.view._canvas_id
- # вместо отслеживания всех кликов и проверки на пересечение
- # поставим отслеживание клика для каждого блока отдельно
- canvas.tag_bind(id, "<Button-1>", self._mouse_click)
- def _mouse_click(self, event):
- (dx, dy) = self.model.get_shift_after_click()
- self.move(dx, dy)
- def move(self, dx, dy):
- self.view.hide()
- self.model.move(dx, dy)
- self.view.show()
Add Comment
Please, Sign In to add comment