コード例 #1
0
ファイル: line.py プロジェクト: wonglkd/cam-1b-graphics
def draw(u, v, shading_func=lambda v: 1):
    """ Placeholder, to replace with draw_midpoint once that is debugged. """
    u = map(int, u[:2])
    v = map(int, v[:2])
    x_0, y_0 = map(int, u)
    x_1, y_1 = map(int, v)
    d_x = abs(x_1 - x_0)
    d_y = abs(y_1 - y_0)
    s_x = 1 if x_0 < x_1 else -1
    s_y = 1 if y_0 < y_1 else -1
    err = d_x - d_y
    try:
        while True:
            screen.draw_pixel(x_0, y_0, shading_func((x_0, y_0)))
            if x_0 == x_1 and y_0 == y_1:
                break
            e2 = 2 * err
            if e2 > -d_y:
                err -= d_y
                x_0 += s_x
            if e2 < d_x:
                err += d_x
                y_0 += s_y
    except IndexError:
        print "Failed to draw line ", u, v
        raise RuntimeError
コード例 #2
0
def draw_midpoint(centre, radius):
    x_0, y_0 = centre
    x, y = radius, 0
    radius_error = 1 - x
    while x >= y:
        types = [(x, y), (-x, y), (-y, x), (-x, -y)]
        for a, b in types:
            screen.draw_pixel(a + x_0, b + y_0)
            screen.draw_pixel(b + x_0, a + y_0)

        y += 1
        if radius_error < 0:
            radius_error += 2 * y + 1
        else:
            x -= 1
            radius_error += 2 * (y - x) + 1
コード例 #3
0
def draw_barycentric(vertices, shading_func):
    vertices = [map(int, v) for v in vertices]
    v1, v2, v3 = vertices
    vs1 = v2[0] - v1[0], v2[1] - v1[1]
    vs2 = v3[0] - v1[0], v3[1] - v1[1]
    # calculate bounding box
    max_x = int(max(v[0] for v in vertices))
    min_x = int(min(v[0] for v in vertices))
    max_y = int(max(v[1] for v in vertices))
    min_y = int(min(v[1] for v in vertices))

    # iterate over each point inside bounding box
    for i in range(min_x, max_x + 1):
        for j in range(min_y, max_y + 1):
            v = (i - v1[0], j - v1[1])
            n1 = float(np.cross(v, vs2)) / np.cross(vs1, vs2)
            n2 = float(np.cross(vs1, v)) / np.cross(vs1, vs2)

            if n1 >= 0 and n2 >= 0 and n1 + n2 <= 1:
                screen.draw_pixel(i, j, shading_func((i, j)))
コード例 #4
0
ファイル: screens.py プロジェクト: Elioby/Trail-Game
def draw_travelling_screen():
    if previous_screen is None:
        open_screen(screen_list["starting"])

    screen.set_cursor_visibility(False)

    show_next_city_notification = previous_screen is not None and previous_screen[
        "name"] == "city"

    car_body_image = ascii_helper.load_image("resources/car_body.ascii")
    car_wheel_image_1 = ascii_helper.load_image("resources/car_wheel_1.ascii")
    car_wheel_image_2 = ascii_helper.load_image("resources/car_wheel_2.ascii")

    stats_x_start = int(screen.get_width() / 10)
    stats_y_start = screen.get_height() - (
        (len(survivors.survivor_list) + 1) * 2) - 1

    car_x = int((screen.get_width() / 2) - (car_body_image["width"] / 2))
    car_y = stats_y_start - car_body_image["height"] - 5

    iterations = 0
    wheel = 0
    road = 0

    while True:
        # Draw travelling progress bar
        progress_bar_box_width = int(screen.get_width() / 1.5)
        progress_bar_box_x = int((screen.get_width() / 2) -
                                 (progress_bar_box_width / 2))

        progress_bar_width = progress_bar_box_width - 6

        screen.draw_bordered_rect(progress_bar_box_x, -1,
                                  progress_bar_box_width, 5)

        for x in range(progress_bar_box_x + 3,
                       progress_bar_box_x + progress_bar_box_width - 3):
            screen.draw_pixel(x, 1, "-")

        progress_bar_current_x = progress_bar_box_x + 3 + (
            (survivors.distance_travelled / get_end_distance()) *
            progress_bar_width)

        end_distance = get_end_distance()

        for city in cities.city_list.values():
            screen.draw_pixel(
                progress_bar_box_x + 3 + int(
                    (city["distance_from_start"] / end_distance) *
                    (progress_bar_width - 1)), 1, "|")

        screen.draw_pixel(int(progress_bar_current_x), 2, "^")

        # Draw survivors and car stats
        stats_y = stats_y_start

        health_x = 0

        name_length = len("Fuel")

        if name_length > health_x:
            health_x = name_length

        screen.draw_text(stats_x_start, stats_y + 1, "Fuel")

        stats_y += 2

        for survivor in survivors.survivor_list:
            survivor_name = survivor["name"]
            name_length = len(survivor_name)

            if name_length > health_x:
                health_x = name_length

            screen.draw_text(stats_x_start, stats_y + 1, survivor_name)

            stats_y += 2

        stats_y = stats_y_start + 1

        total_bars = 14

        fuel_amount = 0

        if "Fuel" in survivors.group_inventory:
            fuel_amount = survivors.group_inventory["Fuel"]["amount"]

        screen.draw_progress_bar(stats_x_start + health_x + 2, stats_y,
                                 total_bars,
                                 fuel_amount / max(fuel_amount, 60))

        stats_y += 2

        for survivor in survivors.survivor_list:
            if survivor["alive"]:
                screen.draw_progress_bar(
                    stats_x_start + health_x + 2, stats_y, total_bars,
                    survivor["health"] / survivor["max_health"])

                if survivor["zombified"]:
                    screen.draw_text(stats_x_start + health_x + total_bars + 5,
                                     stats_y, "(ZOMBIE)")
                elif survivor["bitten"]:
                    screen.draw_text(stats_x_start + health_x + total_bars + 5,
                                     stats_y, "(BITTEN)")
            else:
                padding = int((total_bars - 4) / 2)
                screen.draw_text(
                    stats_x_start + health_x + 2, stats_y,
                    "[" + (padding * " ") + "DEAD" + (padding * " ") + "]")

            stats_y += 2

        # Draw stats
        next_city = get_next_city(survivors.distance_travelled)

        amount_of_food = 0

        if "Food" in survivors.group_inventory:
            amount_of_food = survivors.group_inventory["Food"]["amount"]

        stat_lines = [
            "Time: " + format_time(survivors.current_datetime),
            "Date: " + format_date(survivors.current_datetime),
            "Next City: " + next_city["name"],
            "Food: " + str(int(amount_of_food))
        ]

        longest_line = 0

        for stat_line in stat_lines:
            stat_line_length = len(stat_line)
            if stat_line_length > longest_line:
                longest_line = stat_line_length

        stat_x = int(screen.get_width() - longest_line -
                     (screen.get_width() / 10) + 2)
        stat_y = stats_y_start + 1

        for stat_line in stat_lines:
            screen.draw_text(stat_x, stat_y, stat_line)

            stat_y += 2

        # Draw the car
        screen.draw_ascii_image(car_x, car_y, car_body_image)

        if wheel <= 0.25:
            screen.draw_ascii_image(car_x + 14, car_y + 7, car_wheel_image_2)
            screen.draw_ascii_image(car_x + 53, car_y + 7, car_wheel_image_2)
        else:
            screen.draw_ascii_image(car_x + 14, car_y + 7, car_wheel_image_1)
            screen.draw_ascii_image(car_x + 53, car_y + 7, car_wheel_image_1)

        for x in range(screen.get_width()):
            pixel_char = "="

            if road < 1:
                if x % 2 == 0:
                    pixel_char = "-"
            else:
                if x % 2 != 0:
                    pixel_char = "-"

            screen.draw_pixel(x, car_y + car_body_image["height"] + 2,
                              pixel_char)

        screen.flush()

        if show_next_city_notification:
            next_city = get_next_city(survivors.distance_travelled)
            screen.print_notification(next_city["name"] + " is " + str(
                int(next_city["distance_from_start"] -
                    survivors.distance_travelled)) + " miles away.")
            show_next_city_notification = False

        wheel += 0.25
        road += 1

        if wheel > 1:
            iterations += 1

            if iterations > 2:
                return

        if road > 1:
            road = 0

        time.sleep(0.5 - (survivors.car_speed / 100.0))

        screen.set_cursor_visibility(False)
コード例 #5
0
def draw_wireframe(vertices):
    for v in vertices:
        screen.draw_pixel(*v[:2])
    for v, u in zip(vertices, vertices[1:] + [vertices[0]]):
        line.draw(v[:2], u[:2])