从几张图片动画精灵 [英] Animated sprite from few images

查看:161
本文介绍了从几张图片动画精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找有关使用pygame的Python中的一些图片制作简单精灵动画一些很好的教程。我还没有找到我要找的。

我的问题很简单:如何让动画精灵从一些图像(一个例子:制作爆炸的几张图片,尺寸20x20px是为一个动画却)

什么好的想法?


解决方案

您可以尝试修改你的精灵,使其换出它的形象为更新里面的一个不同。这样,当精灵被渲染,它会看起来动画。

修改

下面是一个简单的例子,我画了起来:

 进口pygame的
进口SYSDEF load_image(名称):
    图像= pygame.image.load(名)
    返回图像类TestSprite(pygame.sprite.Sprite):
    高清__init __(个体经营):
        超(TestSprite,个体经营).__的init __()
        self.images = []
        self.images.append(load_image('image1.png'))
        self.images.append(load_image('image2.png'))
        #假设两个图像是64×64像素        =手动调用self.index 0
        self.image = self.images [手动调用self.index]
        self.rect = pygame.Rect(5,5,64,64)    高清更新(个体经营):
        ''内通过的self.images元素的这种方法和迭代
        显示下一个每一跳。对于较慢的动画,你可能想
        考虑使用某种类型的计时器,以便它更新慢。'''
        +手动调用self.index = 1
        手动调用self.index是否GT&= LEN(self.images):
            =手动调用self.index 0
        self.image = self.images [手动调用self.index]高清的main():
    pygame.init()
    屏幕= pygame.display.set_mode((250,250))    my_sprite = TestSprite()
    my_group = pygame.sprite.Group(my_sprite)    而真正的:
        事件= pygame.event.poll()
        如果event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)        #调用my_group.update'函数调用所有的更新功能
        #其成员的精灵。调用my_group.draw功能采用了形象
        其成员精灵#和矩形属性来绘制精灵。
        my_group.update()
        my_group.draw(屏)
        pygame.display.flip()如果__name__ =='__main__':
    主要()

它假定您已经叫 image1.png image2.png 同一文件夹内$两个图像C $ c为

I've been searching for some good tutorial about making simple sprite animation from few images in Python using Pygame. I still haven't found what I'm looking for.

My question is simple: how to make an animated sprite from few images (for an example: making few images of explosion with dimensions 20x20px to be as one but animated)

Any good ideas?

解决方案

You could try modifying your sprite so that it swaps out its image for a different one inside update. That way, when the sprite is rendered, it'll look animated.

Edit:

Here's a quick example I drew up:

import pygame
import sys

def load_image(name):
    image = pygame.image.load(name)
    return image

class TestSprite(pygame.sprite.Sprite):
    def __init__(self):
        super(TestSprite, self).__init__()
        self.images = []
        self.images.append(load_image('image1.png'))
        self.images.append(load_image('image2.png'))
        # assuming both images are 64x64 pixels

        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(5, 5, 64, 64)

    def update(self):
        '''This method iterates through the elements inside self.images and 
        displays the next one each tick. For a slower animation, you may want to 
        consider using a timer of some sort so it updates slower.'''
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]

def main():
    pygame.init()
    screen = pygame.display.set_mode((250, 250))

    my_sprite = TestSprite()
    my_group = pygame.sprite.Group(my_sprite)

    while True:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)

        # Calling the 'my_group.update' function calls the 'update' function of all 
        # its member sprites. Calling the 'my_group.draw' function uses the 'image'
        # and 'rect' attributes of its member sprites to draw the sprite.
        my_group.update()
        my_group.draw(screen)
        pygame.display.flip()

if __name__ == '__main__':
    main()

It assumes that you have two images called image1.png and image2.png inside the same folder the code is in.

这篇关于从几张图片动画精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆