【问题标题】:What is the best thing to use to seperate images on sprite sheets?在精灵表上分离图像的最佳方法是什么?
【发布时间】:2014-09-03 20:09:54
【问题描述】:

寻找下载站点或任何可用于将精灵从表格中分离出来的东西。请让我知道您可能有什么想法

应该加上我一直在使用 spriters-resources 来获取我的精灵表。

【问题讨论】:

    标签: python animation pygame sprite sprite-sheet


    【解决方案1】:

    在 pygame 中,您可以加载一张包含许多精灵的图像,然后使用它来创建许多次表面,并像使用分离的图像一样使用它。因此,您不必将一张纸拆分为多个文件。

    subsurface()

    当然,您可以使用 pygame 函数将任何次表面保存为单独的文件。

    pygame.image.save()


    编辑:

    用于动画的一张图片。

    import pygame
    import pygame.locals
    
    pygame.init()
    screen = pygame.display.set_mode((300,300))
    
    # one image -> 7 subsurfaces
    
    sheet = pygame.image.load('explosed-sprite.png').convert_alpha()
    
    imgs = []
    
    for x in range(7):
        imgs.append( sheet.subsurface((20*x,0,20,20)) )
    
    current_img = 0
    
    # clock - FPS
    
    clock = pygame.time.Clock()
    
    # mainloop 
    
    running = True
    
    while running:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
    
        # next image modulo 7
        current_img = ( current_img + 1 ) % 7 
    
        screen.fill( (0,0,0) )
        screen.blit( imgs[current_img], (150-10,150-10) )
        pygame.display.flip()
    
        clock.tick(12)
    
    # save subsurfaces as separated images
    
    for x in range(7):
        name = 'frame_' + str(x) + '.png'
        pygame.image.save( imgs[x], 'frame_' + str(x) + '.png' ) 
        print name, 'saved'
    
    # the end
    pygame.quit()
    

    将此图像另存为explosed-sprite.png 保存在带有python 脚本的文件夹中。

    【讨论】:

    • 哇,好吧,我不知道你能做到这一点。使用它的示例代码会是什么样子?如果您只是想在屏幕上移动图像。
    • 参见示例。顺便说一句:如果你在一个类中有一个玩家/精灵/敌人(及其所有图像),那么使用动画会更容易 - 比你可以创建许多对象 - 就像许多爆炸一样。
    • 在示例中,我添加了将次表面保存为分离图像的代码。现在你可以创建自己的sprite splitter :)
    • 好的,所以如果我想在一张 1270 x 933 的表格上分离图像并且上面有 60 多个图像(大约 10 个不同的动画),我将如何告诉 pygame 我想要它的位置和图像采用?我需要知道什么信息?
    • 您需要x, y, width, height 用于工作表上的每个图像(x,y = 图像的左上角像素)。然后您可以使用img = sheet.subsurface((x,y,width,height)) 来获取工作表的那部分,您可以将其视为之前的分离图像。您可以在屏幕上进行 blit 或将其保存在文件中。
    猜你喜欢
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2016-04-01
    • 2013-05-23
    相关资源
    最近更新 更多