栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

来自少量图像的动画精灵

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

来自少量图像的动画精灵

您可以尝试修改sprite,以便将其图像换成另一个内部的图像

update
。这样,当渲染精灵时,它将看起来很生气。

编辑:

这是我草拟的一个简单示例:

import pygameimport sysdef load_image(name):    image = pygame.image.load(name)    return imageclass 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()

假定您有两个被调用的图像,

image1.png
并且
image2.png
在代码所在的同一文件夹中。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641579.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号