コード例 #1
0
def render_maze(task_id):

    for w in tk.winfo_children():
        w.destroy()

    m, n = rob.get_field_size()

    w = CELL_SIZE * n + 2 * X_OFFSET
    h = CELL_SIZE * m + 2 * Y_OFFSET

    sw = tk.winfo_screenwidth()
    sh = tk.winfo_screenheight()
    x = (sw - w) // 2
    y = (sh - h) // 2

    tk.title("pyrob :: " + task_id)
    tk.geometry('{}x{}+{}+{}'.format(w, h, x, y))

    global canvas
    canvas = Canvas(tk, width=w, height=h)
    canvas.pack()

    lines = []
    cells = []
    parking_points = []
    for i in range(m):
        for j in range(n):

            x = X_OFFSET + j * CELL_SIZE
            y = Y_OFFSET + i * CELL_SIZE

            cs = (x, y)
            ce = (x + CELL_SIZE - 1, y + CELL_SIZE - 1)
            cells.append((i, j, cs, ce))

            if rob.is_parking_cell(i, j):
                parking_points.append(
                    (x + PARKING_POINT_OFFSET, y + PARKING_POINT_OFFSET))

            w = rob.is_blocked(i, j, rob.WALL_LEFT)
            wt = WALL_THICKNESS if w else GRID_THICKNESS
            wc = WALL_COLOR if w else GRID_COLOR
            ws = (x, y)
            we = (x + wt - 1, y + CELL_SIZE - 1)
            lines.append((ws, we, wc))

            w = rob.is_blocked(i, j, rob.WALL_TOP)
            wt = WALL_THICKNESS if w else GRID_THICKNESS
            wc = WALL_COLOR if w else GRID_COLOR
            ws = (x, y)
            we = (x + CELL_SIZE - 1, y + wt - 1)
            lines.append((ws, we, wc))

            w = rob.is_blocked(i, j, rob.WALL_RIGHT)
            wt = WALL_THICKNESS if w else GRID_THICKNESS
            wc = WALL_COLOR if w else GRID_COLOR
            ws = (x + CELL_SIZE - wt, y)
            we = (x + CELL_SIZE - 1, y + CELL_SIZE - 1)
            lines.append((ws, we, wc))

            w = rob.is_blocked(i, j, rob.WALL_BOTTOM)
            wt = WALL_THICKNESS if w else GRID_THICKNESS
            wc = WALL_COLOR if w else GRID_COLOR
            ws = (x, y + CELL_SIZE - wt)
            we = (x + CELL_SIZE - 1, y + CELL_SIZE - 1)
            lines.append((ws, we, wc))

    lines.append(((X_OFFSET - WALL_THICKNESS, Y_OFFSET - WALL_THICKNESS),
                  (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS,
                   Y_OFFSET + WALL_THICKNESS), WALL_COLOR))
    lines.append(((X_OFFSET - WALL_THICKNESS, Y_OFFSET + m * CELL_SIZE),
                  (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS,
                   Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR))
    lines.append(
        ((X_OFFSET - WALL_THICKNESS, Y_OFFSET - WALL_THICKNESS),
         (X_OFFSET, Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR))
    lines.append(((X_OFFSET + n * CELL_SIZE, Y_OFFSET - WALL_THICKNESS),
                  (X_OFFSET + n * CELL_SIZE + WALL_THICKNESS,
                   Y_OFFSET + m * CELL_SIZE + WALL_THICKNESS), WALL_COLOR))

    def rect(start, end, *args, **kwargs):
        canvas.create_rectangle(start[0], start[1], end[0] + 1, end[1] + 1,
                                *args, **kwargs)

    def make_label(
        x,
        y,
        text,
    ):
        f = Frame(canvas, height=CELL_SIZE, width=CELL_SIZE)
        f.pack_propagate(0)
        f.place(x=x, y=y)
        label = Label(f, font=("Helvetica", 14), text=text)
        label.pack(fill=tkinter.BOTH, expand=1)
        return label

    for i in range(m):
        make_label(X_OFFSET - CELL_SIZE - WALL_THICKNESS,
                   Y_OFFSET + CELL_SIZE * i, str(i + 1))

    for j in range(n):
        make_label(X_OFFSET + CELL_SIZE * j,
                   Y_OFFSET - CELL_SIZE - WALL_THICKNESS, str(j + 1))

    for i, j, cs, ce in cells:
        color = CELL_COLOR_MAP[rob.get_cell_type(i, j)]
        rect(cs, ce, fill=color, width=0, tags='{}_{}'.format(i, j))

    for ws, we, wc in lines:
        rect(ws, we, fill=wc, width=0)

    for (x, y) in parking_points:
        canvas.create_oval(x,
                           y,
                           x + 2 * PARKING_POINT_RADIUS,
                           y + 2 * PARKING_POINT_RADIUS,
                           width=0,
                           fill=PARKING_POINT_COLOR)

    canvas.create_oval(0,
                       0,
                       2 * ROBOT_RADIUS,
                       2 * ROBOT_RADIUS,
                       tags='robot',
                       width=ROBOT_THICKNESS,
                       outline=ROBOT_COLOR)
コード例 #2
0
 def check_solution(self):
     return rob.is_parking_cell(*rob.get_pos())