Ejemplo n.º 1
0
def draw():
    ctx = Canvas(width=500, height=500, gtk_draw=True)
    bg = ctx.background()
    bg.fill_color = 0.2
    ctx.add(bg)

    ## Update each flock.
    global flocks
        
    for flock in flocks:
        flock.update(goal=60)
        
        ## Draw a grey arrow for each boid in a block.
        ## Radius and opacity depend on the boids z-position.
        for boid in flock:
            r = 10 + boid.z * 0.25
            alpha = 0.5 + boid.z*0.01
            s = shape()
            arrow = s.arrow(boid.x-r/2, boid.y-r/2, r)
            arrow.fill_color = (0.6, 0.6, 0.6, alpha)

            arrow.rotate(-boid.angle)
            ctx.add(arrow)

    ctx.draw()
    return ctx.gtk()
Ejemplo n.º 2
0
def draw():
    global colony

    ctx = Canvas(width=500, height=500, gtk_draw=True)
    bg = ctx.background()
    bg.fill_color = 0.2
    ctx.add(bg)

    ## Draw the hoarded food in the colony.
    shapes = shape()

    s = colony.food
    oval = shapes.oval(colony.x-s/2, colony.y-s/2, s, s)
    oval.fill_color = (0.3)

    ctx.add(oval)

    ## Draw each foodsource in green.
    ## Watch it shrink as the ants eat away its size parameter!
    for f in colony.foodsources:
        oval = shapes.oval(f.x-f.size/2, f.y-f.size/2, f.size, f.size)
        oval.fill_color = (0.6, 0.8, 0, 0.1)
        ctx.add(oval)
    
    for ant in colony:
        p = path()
        ## Draw the pheromone trail for each ant.
        ## Ants leave a trail of scent from the foodsource,
        ## enabling other ants to find the food as well!
        if len(ant.trail) > 0:
            p.moveto(ant.trail[0].x, ant.trail[0].y)
            for k in ant.trail: 
                p.lineto(k.x, k.y)
                
        p.stroke_color = (0.8, 0.8, 0.8, 1.0)
        p.stroke_width = (0.5)
        
        ctx.add(p)

        ## Change ant color when carrying food.
        
        ## The main ant behaviour:
        ## 1) follow an encountered trail,
        ## 2) harvest nearby food source,
        ## 3) bring food back to colony,
        ## 4) wander aimlessly
        ant.forage()
        oval = shapes.oval(ant.x, ant.y, 3, 3)
        if ant.has_food: 
            oval.fill_color = (0.6, 0.8, 0)
        else:
            oval.fill_color = (0.8, 0.8, 0.8, 0.5)

        ctx.add(oval)

    ctx.draw()
    return ctx.gtk()