我的自定义图像导入模块(图像动画)出现Python / pygame错误?

时间:2015-03-08 21:52:41

标签: python image animation module pygame

我已经创建了一个用于导入图像的自定义模块,并且能够调整它的大小并轻松地为它们设置动画(基本上你说它有动画的帧,并且它将图像分割成你说的数量)。

(我知道他们并不完美!)

这是模块“load.py”

#IMPORT MODULES
try:
    import pygame; pygame.init()
    import os
except: pass

#------------------------------------------------------------------------------

#DEFAULT VALUES    
Image_Frames = 1
Image_Size = (0,0)

#------------------------------------------------------------------------------

def image(name,frames=Image_Frames,size=Image_Size):

    #Load Image
    try:
        img = pygame.image.load(name).convert_alpha()
    except: return None

    #Single / Multi Frame
    if frames <= 1:
        if not 0 in size: img = pygame.transform.scale(img,size)
        return img
    else:

        #Set Variables
        frame_y = 0
        frames_list = []
        frame_height = int(img.get_size()[1] / frames)

        #Loop Frames
        for frame in range(frames):

            #Create a Surface and blit part of the image (Single Frame)
            frame_surface = pygame.Surface((img.get_size()[0], frame_height),pygame.SRCALPHA)
            frame_surface.blit(img,(0,0),(0,frame_y,img.get_size()[0],frame_height))

            #If both values of size are not 0 scale it to size
            if not 0 in size: img = pygame.transform.scale(frame_surface,size)

            #Add it to list of Frames and prepare for next
            frames_list.append(frame_surface)
            frame_y += frame_height

        #Return Frames
        return frames_list

主要代码(不完美,只是为了看图像):

import pygame; pygame.init()
Screen = pygame.display.set_mode((800,800))

import load

images = load.image("test.png",2,(400,300))

while True:
    Screen.fill((255,0,255))

    Screen.blit(images[0],(0,0))
    Screen.blit(images[1],(0,400))

    pygame.display.update()

这是我使用的图像(抱歉,我无法显示图像...... :() https://www.dropbox.com/s/u7nghxntor2vbt1/test.png?dl=0

这就是问题...... 假设发生的是图像被分成2个, 并且每张图像的比例扩大到400,300。

但是当我运行代码时会发生这种情况: https://www.dropbox.com/s/idrsygynftb1oua/Sin%20t%C3%ADtulo.png?dl=0

第一个不缩放,第二个变得奇怪...... 但这只发生在超过1'帧的时候。

如果将其更改为仅执行一个图像,则可以正常工作(main.py的代码示例)

import pygame; pygame.init()
Screen = pygame.display.set_mode((800,800))

import load

images = load.image("test.png",1,(400,300))

while True:
    Screen.fill((255,0,255))

    Screen.blit(images,(0,0))

    pygame.display.update()

任何修复?如果你需要更多关于我的信息,请问你什么。

1 个答案:

答案 0 :(得分:0)

实际修好!

您的代码:

#If both values of size are not 0 scale it to size
if not 0 in size: 
    >> img << = pygame.transform.scale(frame_surface,size)

应该是:

#If both values of size are not 0 scale it to size
if not 0 in size:
    >> frame_surface << = pygame.transform.scale(frame_surface,size)