예제 #1
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()
예제 #2
0
def draw():
    global m
    ctx = Canvas(width=500, height=500, gtk_draw=True)
    wn = 500.0 / (grid_size - 1)
    hn = 500.0 / (grid_size - 1)
    
    #bg = ctx.radial_gradient()

    a.solve()
    shapes = shape()
    length = int(sqrt(len(a.vectors()[0])))
    U = a.vectors()[0]
    V = a.vectors()[1]
    colors = a.rgb()

    s = shape()
    p = path()
    for i in xrange(length):
        for j in xrange(length):
            idx = (j * length) + i
            start_x = (i * wn)
            start_y = (j * hn)
            #r = s.rectangle(start_x, start_y, wn, hn)
            #color = colors[idx]
            #r.stroke_color = tuple(color)
            #r.stroke_width = 0.25
            #ctx.add(r)

            p.moveto(start_x, start_y)
            p.rellineto(U[idx]*1000.0, V[idx]*1000.0)
            
    p.stroke_color = (0.0, 0.0, 0.0)
    p.stroke_width = 0.25
    ctx.add(p)
    
    if (m%100) == 0:
        a.add_force(random.randint(0, grid_size)/1000.0, 
                    random.randint(0, grid_size)/1000.0,
                    random.randint(0, grid_size)/1000.0,
                    random.randint(0, grid_size)/1000.0,

                    #random.random(),
                    #random.random()
                    )

    m += 1
    #ctx.draw()
    return ctx