Пример #1
0
def initGame():
    # Read configuration
    global configuration
    random.seed(configuration['seed'])

    # Set images
    rawImages = dict()
    for tilekey, tiledict in configuration['maptiles'].items(): # PYTHON2.7 TODO: change back to .items(): # PYTHON2.7 TODO: change back to .iteritems():
        rawImages[tilekey]= tiledict['graphics']
    rawImages['agent']= configuration['agent']['graphics']

    # Get agent tile identifier
    aiBaseName = configuration['agent']['id']

    # This must be consistent with the agent base locations
    state = {'prev_pos': configuration['agent']['start']}

    # State handlers for key events
    state['inPause']=False
    state['step']=False

    debugMap= configuration['debugMap']

    # Read or generate a random map
    if configuration['type'] == 'random':
        map = maps.createMap(configuration, state, configuration['debug'])
    else:
        map, configuration = maps.readMap(configuration)

    # Display the initial screen
    screen_size = [configuration['map_size'][0] * tile_size, configuration['map_size'][1] * tile_size + text_size]
    screen = pygame.display.set_mode(screen_size)

    def scale(f, d): return pygame.transform.scale(pygame.image.load(d[f]).convert(),(tile_size - 5, tile_size - 5))
    images = {tile: {t: scale(t,rawImages[tile]) for t in rawImages[tile]} for tile in rawImages}

    # Declare starting state
    state['prev_pos'] = configuration['agent']['start']

    # Save map if option is declared
    if configuration['save']:
        with open(configuration['file'], 'w') as f:
            f.write(maps.printableMap(map, configuration, False))

    # Print the initial map
    maps.printMap(map, configuration, images, screen, state, configuration['debug'],"Running search")

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # --- In the IA201718 configuration we have planned in advance for the objective
    global aiPlan
    global aiMapText
    # Search for a solution to the problem
    aiPlan, problem, result, useViewer = searchSolution(map, configuration, state, aiBaseName, debugMap)

    # If a plan has been found, it is executed on the viewer
    if aiPlan:
        aiMapText = searchInfo(problem, result, useViewer)
        print ("----------------------- STARTING SEARCH ---------------------")
#        print("Retrieved a plan: {0}".format(aiPlan))
        state['searchOk']= True
        done= False
    else:
        aiMapText = "Search retrieved no plan for this problem"
        print("Search retrieved no plan for this problem")
        state['searchOk']=False

    return state, screen, images, map, configuration, clock
Пример #2
0
def init_mygame():
    global configuration
    random.seed(configuration['seed'])

    image_files = dict()
    for tilekey, tiledict in configuration['maptiles'].iteritems():
        image_files[tilekey] = tiledict['img']
    for tilekey, tiledict in configuration['agentTiles'].iteritems():
        image_files[tilekey] = tiledict

    aiBaseName = configuration['agentBaseTile']

    # This must be consistent with the agent base locations
    state = {'prev_pos': configuration['agentInit']}

    state['inPause'] = False
    state['step'] = False

    plan = []

    debugMap = configuration['debugMap']

    # FSM
    fsm_state = 'init'
    steps_in_state = 0
    direccion_guardia = 'north'

    # map
    if configuration['type'] == 'random':
        map = maps.create_map(configuration, state, configuration['debug'])
    else:
        map, configuration = maps.read_map(configuration)

    # display
    screen_size = [
        configuration['map_size'][0] * tile_size,
        configuration['map_size'][1] * tile_size + text_size
    ]
    screen = pygame.display.set_mode(screen_size)
    images = {
        f: pygame.transform.scale(
            pygame.image.load(image_files[f]).convert(),
            (tile_size - 5, tile_size - 5))
        for f in image_files
    }

    state['prev_pos'] = configuration['agentInit']
    if configuration['save']:
        with open(configuration['file'], 'w') as f:
            f.write(maps.printable_map(map, configuration, False))

    maps.print_map(map, configuration, images, screen, state, tile_size,
                   fsm_state, configuration['debug'], "Running search")

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # planner
    header = ""

    # --- In the IA201718 configuration we have planned in advance for the objective
    if ai201718:
        global aiPlan
        global aiMapText
        aiPlan, problem, result, use_viewer = searchSolution(
            map, configuration, state, aiBaseName, debugMap)

        if aiPlan:
            aiMapText = searchInfo(problem, result, use_viewer)
            print(
                "----------------------- STARTING SEARCH ---------------------"
            )
            print("Retrieved a plan: {0}".format(aiPlan))
            state['searchOk'] = True
            done = False
        else:
            aiMapText = "Search retrieved no plan for this problem"
            print("Search retrieved no plan for this problem")
            state['searchOk'] = False

    return state, plan, screen_size, screen, images, map, configuration, clock, header, fsm_state, steps_in_state, direccion_guardia