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


Python Sprite.image方法代码示例

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


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

示例1: collapse_layer

# 需要导入模块: from pygame.sprite import Sprite [as 别名]
# 或者: from pygame.sprite.Sprite import image [as 别名]
def collapse_layer(old_layer, new_layer, num_tiles=(2, 2)):
    """Collapse a single layer by joining num_tiles into one tile. A new layer
    is returned.
    
    The old_layer argument is the layer to process.
    
    The new_layer argument is the layer to build.
    
    The num_tiles argument is a tuple representing the number of tiles in the X
    and Y axes to join.
    
    If a map area is sparse (fewer tiles than num_tiles[0] * num_tiles[1]) the
    tiles will be kept as they are.
    
    If tiles with different characteristics are joined, the results can be
    unexpected. These characteristics include some flags, depth, colorkey. This
    can be avoided by pre-processing the map to convert all images so they have
    compatible characteristics.
    """
    from pygame.sprite import Sprite
    from gummworld2 import Vec2d
    
    # New layer dimensions.
    num_tiles = Vec2d(num_tiles)
    tw, th = (old_layer.tile_width, old_layer.tile_height) * num_tiles
    mw, mh = (old_layer.width, old_layer.height) // num_tiles
    if mw * num_tiles.x != old_layer.pixel_width:
        mw += 1
    if mh * num_tiles.y != old_layer.pixel_height:
        mh += 1
    # Poke the right values into new_layer.
    cell_size = max(tw, th) * 2
    new_layer.objects = spatialhash.SpatialHash(cell_size)
    new_layer.width = mw
    new_layer.height = mh
    new_layer.tile_width = tw
    new_layer.tile_height = th
    # Grab groups of map sprites, joining them into a single larger image.
    query_rect = pygame.Rect(0, 0, tw - 1, th - 1)
    for y in range(0, mh * th, th):
        for x in range(0, mw * tw, tw):
            query_rect.topleft = x, y
            sprites = old_layer.objects.intersect_entities(query_rect)
            if len(sprites) != num_tiles.x * num_tiles.y:
                for s in sprites:
                    new_layer.add(s)
                continue
            # If sprite images have different characteristics, they cannot be
            # reliably collapsed. In which case, keep them as-is.
#            incompatible = False
            image = sprites[0].image
            flags = image.get_flags() ^ pygame.SRCALPHA
            colorkey = image.get_colorkey()
            depth = image.get_bitsize()
# This is probably too restrictive. However, some combinations of tiles may
# give funky results.
#            all_details = (flags,colorkey,depth)
#            for s in sprites[1:]:
#                if all_details != (
#                        s.image.get_flags(),
#                        s.image.get_colorkey(),
#                        s.image.get_bitsize(),
#                ):
#                    incompatible = True
#            if incompatible:
#                print('collapse_layer: incompatible image characteristics')
#                for s in sprites:
#                    new_layer.add(s)
#                continue
            # Make a new sprite.
            new_sprite = Sprite()
            new_sprite.rect = sprites[0].rect.unionall([s.rect for s in sprites[1:]])
            new_sprite.rect.topleft = x, y
            new_sprite.image = pygame.surface.Surface(new_sprite.rect.size, flags, depth)
            if colorkey:
                new_sprite.image.set_colorkey(colorkey)
            
            # Blit (x,y) tile and neighboring tiles to right and lower...
            left = reduce(min, [s.rect.x for s in sprites])
            top = reduce(min, [s.rect.y for s in sprites])
            for sprite in sprites:
                p = sprite.rect.x - left, sprite.rect.y - top
                new_sprite.image.blit(sprite.image.convert(depth, flags), p)
            new_layer.add(new_sprite)
    return new_layer
开发者ID:gentooza,项目名称:Freedom-Fighters-of-Might-Magic,代码行数:87,代码来源:basicmap.py

示例2: Group

# 需要导入模块: from pygame.sprite import Sprite [as 别名]
# 或者: from pygame.sprite.Sprite import image [as 别名]
screen.blit(background, (0,0))

sprites = Group()

def personat(x,y):
    return noiseat(x*8,y*8) <= 0.95

for x in range(0, background.get_width(), 8):
    for y in range(0, background.get_height(), 8):
        present = personat(x/8, y/8)
        if shownoise and not present:
            continue
        
        sprite = Sprite()
        sprite.image = Surface((4,4), flags=SRCALPHA)
        if present:
            sprite.exists = True
            male = sex(x/8,y/8)
            draw.circle(sprite.image, (196,196,255) if male else (255,196,196),
                            (2,2), 2, 1 if shownoise else 0)
        else:
            sprite.exists = False
        if not shownoise:
            draw.circle(sprite.image, (128,128,128), (2,2), 2, 1)
        sprite.rect = sprite.image.get_rect().move(x, y)
        sprites.add(sprite)

limit = Clock()

done = False
开发者ID:tps12,项目名称:Generation-Generation,代码行数:32,代码来源:main.py

示例3: _load_tiled_tmx_map

# 需要导入模块: from pygame.sprite import Sprite [as 别名]
# 或者: from pygame.sprite.Sprite import image [as 别名]
def _load_tiled_tmx_map(tmx_map, gummworld_map, load_invisible=True):
    """Load an orthogonal TMX map file that was created by the Tiled Map Editor.
    
    If load_invisible is False, layers where visible!=0 will be empty. Their
    tiles will not be loaded.
    
    Thanks to DR0ID for his nice tiledtmxloader module:
        http://www.pygame.org/project-map+loader+for+%27tiled%27-1158-2951.html
    
    And the creators of Tiled Map Editor:
        http://www.mapeditor.org/
    """
    
    # Taken pretty much verbatim from the (old) tiledtmxloader module.
    
    from pygame.sprite import Sprite
    
    resource = ResourceLoaderPygame()
    resource.load(tmx_map)
    tile_size = (tmx_map.tilewidth, tmx_map.tileheight)
    map_size = (tmx_map.width, tmx_map.height)
    
    for layeri,layer in enumerate(tmx_map.layers):
        gummworld_layer = TiledLayer(gummworld_map, layer, layeri)
        gummworld_map.layers.append(gummworld_layer)
        if not layer.visible and not load_invisible:
            continue
        if layer.is_object_group:
            for obj in layer.objects:
                sprite = Sprite()
                sprite.image = obj.image
                sprite.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                sprite.type = obj.type
                sprite.image_source = obj.image_source
                sprite.name = obj.name
                sprite.properties = obj.properties
                gummworld_layer.add(sprite)
        else:
            for ypos in xrange(0, layer.height):
                for xpos in xrange(0, layer.width):
                    x = (xpos + layer.x) * layer.tilewidth
                    y = (ypos + layer.y) * layer.tileheight
                    img_idx = layer.content2D[xpos][ypos]
                    if img_idx == 0:
                        continue
                    try:
                        offx,offy,tile_img = resource.indexed_tiles[img_idx]
                        screen_img = tile_img
                    except KeyError:
                        print 'KeyError',img_idx,(xpos,ypos)
                        continue
                    sprite = Sprite()
                    ## Note: alpha conversion can actually kill performance.
                    ## Do it only if there's a benefit.
#                    if convert_alpha:
#                        if screen_img.get_alpha():
#                            screen_img = screen_img.convert_alpha()
#                        else:
#                            screen_img = screen_img.convert()
#                            if layer.opacity > -1:
#                                screen_img.set_alpha(None)
#                                alpha_value = int(255. * float(layer.opacity))
#                                screen_img.set_alpha(alpha_value)
#                                screen_img = screen_img.convert_alpha()
                    sprite.image = screen_img
                    sprite.rect = screen_img.get_rect(topleft=(x + offx, y + offy))
                    sprite.name = xpos,ypos
                    gummworld_layer.add(sprite)
开发者ID:SirSwegens,项目名称:Universe_The-Fallout,代码行数:70,代码来源:tiledmap.py


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