コード例 #1
0
ファイル: __init__.py プロジェクト: kkris/amazes
    def solve_maze(self, steps):

        self.runner = MazeRunner(steps)
        self.runner.connect('step', lambda *x: self.queue_draw())
        self.runner.run()
コード例 #2
0
ファイル: __init__.py プロジェクト: kkris/amazes
class Maze(gtk.DrawingArea):

    def __init__(self):

        gtk.DrawingArea.__init__(self)


        self.maze = None
        self.runner = None

        self.connect('draw', self.draw)

    def draw_maze(self, graph):

        self.maze = graph
        self.runner = None
        self.queue_draw()


    def solve_maze(self, steps):

        self.runner = MazeRunner(steps)
        self.runner.connect('step', lambda *x: self.queue_draw())
        self.runner.run()


    def draw(self, widget, ctx):

        if not self.maze:
            return

        ctx.set_line_width(LINE_WIDTH)
        ctx.set_operator(cairo.OPERATOR_OVER)
        ctx.set_font_size(10)

        width = self.get_allocation().width - 2*PADDING
        height = self.get_allocation().height - 2*PADDING

        node_width = width / float(len(self.maze.nodes[0]))
        node_height = height / float(len(self.maze.nodes))

        # paint outline
        ctx.set_source_rgb(0, 0, 0)
        ctx.rectangle(PADDING, PADDING, width, height)
        ctx.stroke()

        for node in flatten(self.maze.nodes):
            x = PADDING + node.x * node_width
            y = PADDING + node.y * node_height
            x1 = x + node_width
            y1 = y + node_height

            right = bottom = True
            for neighbour in node.connected_nodes:
                if node.x == neighbour.x and node.y < neighbour.y:
                    bottom = False
                if node.y == neighbour.y and node.x < neighbour.x:
                    right = False

            if bottom and node.y < len(self.maze.nodes) - 1:
                draw_line(ctx, x, y1, x1, y1)
            if right and node.x < len(self.maze.nodes[0]) - 1:
                draw_line(ctx, x1, y, x1, y1)

            if node.value is not None:
                value = str(node.value)
                ctx.move_to(x + node_width / 2, y + node_height / 2)
                ctx.show_text(value)

            if self.runner and node in self.runner.path:
                ctx.set_source_rgba(0.8, 0.0, 0.0, 0.2)
                ctx.rectangle(x, y, node_width, node_height)
                ctx.fill()


        # Draw runner
        if not self.runner: return

        x = PADDING + self.runner.x * node_width
        y = PADDING + self.runner.y * node_height

        ctx.set_source_rgba(0.9, 0.0, 0.0, 0.6)
        ctx.rectangle(x, y, node_width, node_height)
        ctx.fill()