如何在 pygame 中将精灵制作为 gif? [英] How do I make a sprite as a gif in pygame?

查看:152
本文介绍了如何在 pygame 中将精灵制作为 gif?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个游戏的想法,你在控制直升机的同时躲避射弹.我想知道您是否可以使精灵显示为 gif,或者类似于每几分之一秒切换的两个图像的线条.我知道如何使精灵显示为一个图像:

self.surf = pygame.image.load("example.png").convert()

但我想知道这是否有影响:

self.surf = pygame.image.load("example.gif").convert()

不幸的是,它只显示了 gif 中的第一张图片.这是gif:

好的,所以我查看了答案并尝试在我的代码中实现它们,但后来太混乱了,我试图做一些更简单的事情.这是我想出的:

 如果播放 == 1:self.surf = pygame.image.load("Image2.png").convert()pygame.display.update()玩 = 2时间.睡眠(.2)如果播放 == 2:self.surf = pygame.image.load("Image1.png").convert()pygame.display.update()播放 = 1时间.睡眠(.2)

但是,所做的只是将播放器精灵显示为图像 1.有什么我可以添加的东西吗?

解决方案

导入pygame从 PIL 导入图像def pilImageToSurface(pilImage):模式、大小、数据 = pilImage.mode、pilImage.size、pilImage.tobytes()返回 pygame.image.fromstring(data, size, mode).convert_alpha()定义加载GIF(文件名):pilImage = Image.open(文件名)帧 = []如果 pilImage.format == 'GIF' 和 pilImage.is_animated:对于 ImageSequence.Iterator(pilImage) 中的帧:pygameImage = pilImageToSurface(frame.convert('RGBA'))frame.append(pygameImage)别的:frame.append(pilImageToSurface(pilImage))返回帧pygame.init()窗口 = pygame.display.set_mode((500, 500))时钟 = pygame.time.Clock()gifFrameList = loadGIF(my_gif.gif")当前帧 = 0运行 = 真运行时:时钟滴答(20)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window.fill(0)rect = gifFrameList[currentFrame].get_rect(center = (250, 250))window.blit(gifFrameList[currentFrame], rect)currentFrame = (currentFrame + 1) % len(gifFrameList)pygame.display.flip()

您可以使用 pygame.Surface 对象列表来生成 Spritesheet.

So I have this idea for a game where you dodge projectiles while controlling a helicopter. I am wondering if you can make a sprite appear as a gif, or something along the lines of two images switching every fraction of a second. I know how to make a sprite appear as one image:

self.surf = pygame.image.load("example.png").convert()

But I was wondering if this had an effect:

self.surf = pygame.image.load("example.gif").convert()

Unfortunately, it only displayed the first image in the gif. Here is the gif:

Edit: Ok so I looked at the answers and tried to implement them in my code, but then it was all too confusing and I had tried to do something a bit more simple. This is what I came up with:

    if play == 1:
        self.surf = pygame.image.load("Image2.png").convert()
        pygame.display.update()
        play = 2
        time.sleep(.2)
    if play == 2:
        self.surf = pygame.image.load("Image1.png").convert()
        pygame.display.update()
        play = 1
        time.sleep(.2)

But, all that did was display the player sprite as image 1. Is there anything I can add to make this work?

解决方案

PyGame respectively the pygame.image module can only handle non-animated GIFs.
But on the PyGame homepage is introduced the GIFImage library:

This library adds GIF animation playback to pygame.


Another option is to use the Pillow library (pip install Pillow).

Write a function, that can convert a PIL image to a pygame.Surface:
(see also PIL and pygame.image)

def pilImageToSurface(pilImage):
    mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
    return pygame.image.fromstring(data, size, mode).convert_alpha()

Use the PIL library to load a GIF frame by frame:
(see also Extracting The Frames Of An Animated GIF Using Pillow

def loadGIF(filename):
    pilImage = Image.open(filename)
    frames = []
    if pilImage.format == 'GIF' and pilImage.is_animated:
        for frame in ImageSequence.Iterator(pilImage):
            pygameImage = pilImageToSurface(frame.convert('RGBA'))
            frames.append(pygameImage)
    else:
        frames.append(pilImageToSurface(pilImage))
    return frames

See also Load animated GIF and a simple animated gif viewer example:

import pygame
from PIL import Image

def pilImageToSurface(pilImage):
    mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
    return pygame.image.fromstring(data, size, mode).convert_alpha()

def loadGIF(filename):
    pilImage = Image.open(filename)
    frames = []
    if pilImage.format == 'GIF' and pilImage.is_animated:
        for frame in ImageSequence.Iterator(pilImage):
            pygameImage = pilImageToSurface(frame.convert('RGBA'))
            frames.append(pygameImage)
    else:
        frames.append(pilImageToSurface(pilImage))
    return frames
 
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

gifFrameList = loadGIF("my_gif.gif")
currentFrame = 0

run = True
while run:
    clock.tick(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)

    rect = gifFrameList[currentFrame].get_rect(center = (250, 250))
    window.blit(gifFrameList[currentFrame], rect)
    currentFrame = (currentFrame + 1) % len(gifFrameList)
    
    pygame.display.flip()

You can use the list of pygame.Surface objects to generate a Spritesheet.

这篇关于如何在 pygame 中将精灵制作为 gif?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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