Пример #1
0
class Scene(graphics.Scene):
    def __init__(self):
        graphics.Scene.__init__(self)

        # we should redo the boxes when window gets resized
        box_size = 10
        self.proximities = LQProximityStore(Vector2(0, 0), Vector2(600, 400),
                                            box_size)

        self.waypoints = []
        self.waypoints = [
            QueueingWaypoint(100, 100, 70),
            BucketWaypoint(500, 100, 10),
            GrowWaypoint(500, 500, 10),
            QueueingWaypoint(300, 500, 70),
            BucketWaypoint(100, 500, 10),
            GrowWaypoint(100, 300, 3),
        ]

        for waypoint in self.waypoints:
            self.add_child(waypoint)

        # link them together
        for curr, next in zip(self.waypoints, self.waypoints[1:]):
            curr.next = next
            next.previous = curr

        self.waypoints[0].previous = self.waypoints[-1]
        self.waypoints[-1].next = self.waypoints[0]

        self.boids = [Boid(Vector2(100, 100), 2.0) for i in range(15)]

        for i, boid in enumerate(self.boids):
            boid.target(self.waypoints[0])
            self.add_child(boid)

        self.mouse_node = None

        # some debug variables
        self.debug_radius = False
        self.debug_awareness = False

        self.connect("on-enter-frame", self.on_enter_frame)

    def on_enter_frame(self, scene, context):
        c_graphics = graphics.Graphics(context)
        c_graphics.set_line_style(width=0.8)

        for waypoint in self.waypoints:
            waypoint.update(context)

        for boid in self.boids:
            # the growing antennae circle
            if self.debug_radius:
                c_graphics.set_color("#aaa", 0.3)
                context.arc(boid.location.x, boid.location.y, boid.radio,
                            -math.pi, math.pi)
                context.fill()

            # obstacle awareness circle
            if self.debug_awareness:
                c_graphics.set_color("#aaa", 0.5)
                context.arc(boid.location.x, boid.location.y, boid.awareness,
                            -math.pi, math.pi)
                context.fill()

        for boid in self.boids:
            neighbours = self.proximities.find_neighbours(boid, 40)

            boid.run(neighbours)

            self.proximities.update_position(boid)

            # debug trail (if enabled)
            c_graphics.set_color("#0f0")
            for position1, position2 in zip(boid.positions,
                                            boid.positions[1:]):
                context.move_to(position1.x, position1.y)
                context.line_to(position2.x, position2.y)
            context.stroke()

            # line between boid and it's target
            """
            c_graphics.set_color("#999")
            context.move_to(boid.location.x, boid.location.y)
            context.line_to(boid.target_waypoint.location.x,
                                 boid.target_waypoint.location.y)
            context.stroke()
            """

        self.redraw()
Пример #2
0
class Canvas(graphics.Scene):
    def __init__(self):
        graphics.Scene.__init__(self)
        self.segments = []

        # we should redo the boxes when window gets resized
        self.proximity_radius = 10
        self.proximities = LQProximityStore(Vector2(0, 0), Vector2(600, 400), self.proximity_radius)
        self.flock = []
        self.frame = 0

        self.connect("on-click", self.on_mouse_click)
        self.connect("on-enter-frame", self.on_enter_frame)

    def on_enter_frame(self, scene, context):
        c_graphics = graphics.Graphics(context)

        if len(self.flock) < 80:
            for i in range(2):
                self.flock.append(Boid(Vector2(self.width / 2, self.height / 2), 2.0, 0.05))

        # main loop (i should rename this to something more obvious)
        c_graphics.set_line_style(width=0.8)
        c_graphics.set_color("#666")

        for boid in self.flock:
            neighbours = []
            if self.frame % 2 == 0:  # recalculate direction every second frame
                neighbours = self.proximities.find_neighbours(boid, 40)

            boid.run(neighbours)
            self.wrap(boid)
            self.proximities.update_position(boid)

            self.draw_boid(context, boid)

        self.frame += 1

        context.stroke()

        self.redraw()

    def wrap(self, boid):
        "wraps boid around the edges (teleportation)"
        if boid.location.x < -boid.radius:
            boid.location.x = self.width + boid.radius

        if boid.location.y < -boid.radius:
            boid.location.y = self.height + boid.radius

        if boid.location.x > self.width + boid.radius:
            boid.location.x = -boid.radius

        if boid.location.y > self.height + boid.radius:
            boid.location.y = -boid.radius

    def draw_boid(self, context, boid):
        context.save()
        context.translate(boid.location.x, boid.location.y)

        theta = boid.velocity.heading() + math.pi / 2
        context.rotate(theta)

        context.move_to(0, -boid.radius * 2)
        context.line_to(-boid.radius, boid.radius * 2)
        context.line_to(boid.radius, boid.radius * 2)
        context.line_to(0, -boid.radius * 2)

        context.restore()

    def on_mouse_click(self, widget, event, target):
        self.flock.append(Boid(Vector2(event.x, event.y), 2.0, 0.05))
Пример #3
0
class Canvas(graphics.Scene):
    def __init__(self):
        graphics.Scene.__init__(self)
        self.segments = []

        # we should redo the boxes when window gets resized
        self.proximity_radius = 10
        self.proximities = LQProximityStore(Vector2(0, 0), Vector2(600, 400),
                                            self.proximity_radius)
        self.flock = []
        self.frame = 0

        self.connect("on-click", self.on_mouse_click)
        self.connect("on-enter-frame", self.on_enter_frame)

    def on_enter_frame(self, scene, context):
        c_graphics = graphics.Graphics(context)

        if len(self.flock) < 80:
            for i in range(2):
                self.flock.append(
                    Boid(Vector2(self.width / 2, self.height / 2), 2.0, 0.05))

        # main loop (i should rename this to something more obvious)
        c_graphics.set_line_style(width=0.8)
        c_graphics.set_color("#666")

        for boid in self.flock:
            neighbours = []
            if self.frame % 2 == 0:  #recalculate direction every second frame
                neighbours = self.proximities.find_neighbours(boid, 40)

            boid.run(neighbours)
            self.wrap(boid)
            self.proximities.update_position(boid)

            self.draw_boid(context, boid)

        self.frame += 1

        context.stroke()

        self.redraw()

    def wrap(self, boid):
        "wraps boid around the edges (teleportation)"
        if boid.location.x < -boid.radius:
            boid.location.x = self.width + boid.radius

        if boid.location.y < -boid.radius:
            boid.location.y = self.height + boid.radius

        if boid.location.x > self.width + boid.radius:
            boid.location.x = -boid.radius

        if boid.location.y > self.height + boid.radius:
            boid.location.y = -boid.radius

    def draw_boid(self, context, boid):
        context.save()
        context.translate(boid.location.x, boid.location.y)

        theta = boid.velocity.heading() + math.pi / 2
        context.rotate(theta)

        context.move_to(0, -boid.radius * 2)
        context.line_to(-boid.radius, boid.radius * 2)
        context.line_to(boid.radius, boid.radius * 2)
        context.line_to(0, -boid.radius * 2)

        context.restore()

    def on_mouse_click(self, widget, event, target):
        self.flock.append(Boid(Vector2(event.x, event.y), 2.0, 0.05))