Esempio n. 1
0
    def update(self):
        if not self._node.obstacle:
            self._width = self._orig_width
            self._height = self._orig_height
            self._color = self._orig_color
            self._alpha = self._orig_alpha

            if self._app_ctx.show_grid:
                self._alpha = 255
        else:
            self._alpha = 255
            self._color = get_color('green')
            self._width = 9
            self._height = 9

        if self._resize:
            self._draw_surface(self._width, self._height)
            self._resize = False

        self.image.set_alpha(self._alpha)
        self.image.fill(self._color)
Esempio n. 2
0
def main():
    """
    Implements the mainloop
    """

    solvers = {
        sim.ShortestPathAlgorithm.TYPE: sim.ShortestPathAlgorithm()
    }

    ctx = ApplicationContext(solvers)

    # screen sizes
    ctx.screen_width = 1300
    ctx.screen_height = 700
    ctx.border_offset = 50

    ctx.anim_panel_x = ctx.border_offset
    ctx.anim_panel_y = ctx.border_offset
    ctx.offset_screen_width = ctx.screen_width - 2 * ctx.border_offset
    ctx.offset_screen_height = ctx.screen_height - 2 * ctx.border_offset
    ctx.anim_panel_width = ctx.offset_screen_width / 5 * 3
    ctx.anim_panel_height = ctx.offset_screen_height
    ctx.gui_panel_x = ctx.anim_panel_x + ctx.anim_panel_width + ctx.border_offset
    ctx.gui_panel_y = ctx.border_offset
    ctx.gui_panel_width = ctx.screen_width - 3 * ctx.border_offset - ctx.anim_panel_width
    ctx.gui_panel_height = ctx.offset_screen_height

    # state variables
    ctx.show_only_shortest = False
    ctx.done = False
    ctx.paint = False
    ctx.paint_erase = False
    ctx.state = 'stop'

    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([ctx.screen_width, ctx.screen_height], HWSURFACE | DOUBLEBUF | RESIZABLE)

    ctx.ant_color_dialog = ColorDialog('#000000')

    # CREATE THE ANT COLONY
    ctx.colony = sim.AntColony()

    application = create_application(ctx, ctx.screen_width, ctx.screen_height,
                                     ctx.gui_panel_x, ctx.gui_panel_y,
                                     ctx.gui_panel_width, ctx.gui_panel_height)

    application.listen_for('num_ants', on_num_ants_change)
    application.listen_for('show_grid_lines', on_show_grid_lines_change)
    application.listen_for('show_grid', on_show_grid_change)
    application.listen_for('restart', restart_button_pressed)
    application.listen_for('reset', reset_button_pressed)
    application.listen_for('start', start_button_pressed)
    application.listen_for('stop', stop_button_pressed)

    solution_chart_thread = SolutionChartThread(ctx)
    solution_chart_thread.start()

    while ctx.done is False:
        for event in pygame.event.get():  # User did something
            application.app.event(event)

            if event.type == pygame.QUIT:  # If user clicked close
                ctx.done = True  # Flag that we are done so we exit this loop
            elif event.type is KEYDOWN and event.key == K_ESCAPE:
                ctx.done = True
            elif event.type == VIDEORESIZE:
                screen = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE)
                pygame.display.flip()
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
                if ctx.running:
                    ctx.paint = True
                    draw_obstacle(event, ctx)
            elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
                if ctx.running:
                    ctx.paint = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT:
                if ctx.running:
                    ctx.paint_erase = True
                    draw_obstacle(event, ctx)
            elif event.type == pygame.MOUSEBUTTONUP and event.button == RIGHT:
                if ctx.running:
                    ctx.paint_erase = False
            elif event.type == pygame.MOUSEMOTION:
                draw_obstacle(event, ctx)

        screen.fill(get_color('white'))

        if ctx.runner and ctx.running:
            if not ctx.paused:
                ctx.runner.next_step()

            # Clear the screen
            #
            if not ctx.show_only_shortest:
                for edge in ctx.runner.updated_edges:
                    lines = ctx.edge_lines[edge]
                    plevel = edge.pheromone_level(
                        ctx.colony.pheromone_kind('default'))
                    if plevel:
                        level = 255 - (plevel * 10000)
                        if level < MIN_BLUE:
                            level = MIN_BLUE
                        if level > 255:
                            level = 255

                        pygame.draw.lines(screen, (0, 0, level), False,
                                          lines, 10)

            if ctx.show_grid_lines:
                for edge in ctx.graph.edges:
                    lines = ctx.edge_lines[edge]
                    pygame.draw.lines(screen, (0, 0, 0), False, lines, 1)

            label = default_font().render('fps %d' % clock.get_fps(), 5, get_color('gray'))
            screen.blit(label, (15, 15))

            label = default_font().render('Round %d' % ctx.runner.rounds, 5, get_color('black'))
            screen.blit(label, (100, 15))

            if ctx.runner.solutions:
                num_solutions = len(ctx.runner.solutions)

                label = default_font().render('(#%d)' % num_solutions,
                                              5, get_color('black'))
                screen.blit(label, (200, 15))

            # ant_sprites.color = dialog.rgb
            ctx.ant_sprites.update()
            ctx.wp_sprites.update()

            # Draw all the spites
            ctx.wp_sprites.draw(screen)

            if ctx.runner.best_solution:
                draw_solution_line(screen, ctx.runner.best_solution,
                                   color=(255, 69, 0), thickness=8)

                label = default_font().render('Best %.2f' % ctx.runner.best_solution[1],
                                              5, get_color('black'))
                screen.blit(label, (300, 15))

            if ctx.runner.local_best_solution:
                draw_solution_line(screen, ctx.runner.local_best_solution,
                                   color=(150, 69, 0), thickness=3)

                label = default_font().render('Local best %.2f' % ctx.runner.local_best_solution[1],
                                              5, get_color('black'))
                screen.blit(label, (500, 15))

            if not ctx.show_only_shortest:
                ctx.ant_sprites.draw(screen)

        application.app.paint()

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()

        clock.tick()

    pygame.quit()
Esempio n. 3
0
def node_factory(name):
    if name == 'waypoint':
        return Waypoint
    else:
        raise ValueError('Node type %s does not exist' % name)


def edge_factory(name):
    if name == 'waypoint':
        return WaypointEdge
    else:
        raise ValueError('Edge type %s does not exist' % name)


node_config = {
    'nest': (get_color('green'), 255, 10, 10),
    'food': (get_color('red'), 255, 10, 10),
    'waypoint': (get_color('black'), 0, 2, 2)
}


def create_ant_sprite(ant, app_ctx):
    return AntSprite(app_ctx, ant, (89, 54, 99), 4, 4,
                     color_dialog=app_ctx.ant_color_dialog)


def start_button_pressed(e, app, app_ctx):
    if not app_ctx.running:
        reset_button_pressed(e, app, app_ctx)
    app_ctx.paused = False