def update_buttons(self): g2d.clear_canvas() g2d.set_color((0, 0, 0)) cols, rows = self._game.cols(), self._game.rows() for y in range(1, rows): g2d.draw_line((0, y * H), (cols * W, y * H)) for x in range(1, cols): g2d.draw_line((x * W, 0), (x * W, rows * H)) for y in range(rows): for x in range(cols): value = self._game.value_at(x, y) if value == "": g2d.set_color((204, 204, 204)) elif value == "W": g2d.set_color((255, 255, 255)) elif value == "B": g2d.set_color((0, 0, 0)) g2d.fill_rect((x * W + 1, y * H + 1, W - 1, H - 1)) g2d.update_canvas() if self._game.finished(): g2d.alert(self._game.message()) g2d.close_canvas()
def move_pen(start: (float, float), length: float, angle: float) -> (float, float): x, y = start x1 = x + math.cos(angle) * length y1 = y + math.sin(angle) * length g2d.draw_line((int(x), int(y)), (int(x1), int(y1))) return (x1, y1)
def htree(rect: (int, int, int, int), level: int): x, y, w, h = rect if level == 0 or w < 3 or h < 3: return if level % 2 == 0: rect1 = x, y, w / 2, h rect2 = x + w / 2, y, w / 2, h else: rect1 = x, y, w, h / 2 rect2 = x, y + h / 2, w, h / 2 g2d.draw_line(center(rect1), center(rect2)) htree(rect1, level - 1) htree(rect2, level - 1)
def update_buttons(self): g2d.clear_canvas() g2d.set_color((0, 0, 0)) cols, rows = self._game.cols(), self._game.rows() for y in range(1, rows): g2d.draw_line((0, y * H), (cols * W, y * H)) for x in range(1, cols): g2d.draw_line((x * W, 0), (x * W, rows * H)) for y in range(rows): for x in range(cols): value = self._game.value_at(x, y) center = x * W + W // 2, y * H + H // 2 g2d.draw_text_centered(value, center, H // 2) g2d.update_canvas() if self._game.finished(): g2d.alert(self._game.message()) g2d.close_canvas()
def h_tree(x, y, w, h, level): # per avere una limitazione nella stampa if level == 0: return # caso base if w < 10 or h < 10: return g2d.draw_line((x + 1 * w / 4, y + 1 * h / 4), (x + 1 * w / 4, y + 3 * h / 4)) #linea verticale g2d.draw_line((x + 3 * w / 4, y + 1 * h / 4), (x + 3 * w / 4, y + 3 * h / 4)) #linea orizzontale g2d.draw_line((x + 1 * w / 4, y + 2 * h / 4), (x + 3 * w / 4, y + 2 * h / 4)) #linea verticale # ricorsione per i 4 quadranti ottenuti dalla divisione dell'area h_tree(x, y, w / 2, h / 2, level - 1) h_tree(x + w / 2, y, w / 2, h / 2, level - 1) h_tree(x + w / 2, y + h / 2, w / 2, h / 2, level - 1) h_tree(x, y + h / 2, w / 2, h / 2, level - 1)