当前位置: 首页>>代码示例>>Python>>正文


Python Level.load_finished方法代码示例

本文整理汇总了Python中level.Level.load_finished方法的典型用法代码示例。如果您正苦于以下问题:Python Level.load_finished方法的具体用法?Python Level.load_finished怎么用?Python Level.load_finished使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在level.Level的用法示例。


在下文中一共展示了Level.load_finished方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_level

# 需要导入模块: from level import Level [as 别名]
# 或者: from level.Level import load_finished [as 别名]
def load_level(game, filename):
    filename = os.path.join('levels', filename)
    try:
        tmx_data = pytmx.load_pygame(filename, pixelalpha=True)
    except:
        print 'Level %s could not be read as a TMX file.' % (filename)
        raise

    try:
        name = tmx_data.name
    except AttributeError:
        print 'Level %s has no name: invalid level file.' % (filename)

    width = tmx_data.width
    height = tmx_data.height

    state_images = defaultdict(dict)
    animations = defaultdict(lambda: defaultdict(Animation))

    for tile_id, properties in tmx_data.tile_properties.items():
        image = tmx_data.getTileImageByGid(tile_id)
        try:
            name = properties['name']
        except KeyError:
            continue

        state = properties.get('state', 'default')
        frame = properties.get('frame', None)
        if frame:
            delay = properties.get('frame-delay', '4')
            try:
                delay = int(delay)
            except ValueError:
                print "Level %s has invalid frame delay of %s for frame %s/%s/%s" % (filename, delay, name, state, frame)

            try:
                frame = int(frame)
            except ValueError:
                print "Level %s has invalid frame: %s/%s/%s" % (filename, name, state, frame)

            animations[name][state].add_frame(frame, image, delay)
        else:
            state_images[name][state] = image

    for state in animations.values():
        for animation in state.values():
            animation.finalise()

    level = Level(game, name, width, height)
    for layer in tmx_data.tilelayers:
        for (x, y, tile) in layer:
            if tile == 0:
                continue
            name = tmx_data.tile_properties[tile]['name']
            state = tmx_data.tile_properties[tile].get('state', 'default')
            objtype = OBJECTS[name]
            obj = objtype(level=level, location=(x, y))
            obj.state = state
            obj.state_images = state_images[name]

            obj.animations = animations[name]
            if isinstance(obj, Player):
                level.player = obj

    for obj in tmx_data.getObjects():
        x = int(obj.x / TILE_W)
        y = int(obj.y / TILE_H)
        w = int(obj.width / TILE_W)
        h = int(obj.height / TILE_H)
        region = Region(obj.name, level, (x, y), (w, h))
        if hasattr(obj, 'message'):
            region.message = obj.message.decode('string-escape')
        if hasattr(obj, 'points'):
            region.set_vertices([(px/TILE_W, py/TILE_H) for (px, py) in obj.points])
        if hasattr(obj, 'dependencies'):
            for name in obj.dependencies.split(','):
                region.add_dependency(name)
        if hasattr(obj, 'anti_dependencies'):
            for name in obj.anti_dependencies.split(','):
                region.add_anti_dependency(name)
        region.visible = hasattr(obj, 'show')
        region.state_images = {'default' : None, 'visible' : state_images['hint']['default']}

    level.load_finished()

    return level
开发者ID:fish-face,项目名称:door-restorer,代码行数:88,代码来源:levelloader.py


注:本文中的level.Level.load_finished方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。