2015-03-08 67 views
0

我已经创建了一个导入图像的自定义模块,并且能够调整它的大小并轻松地为它们设置动画(基本上你说它具有动画的框架,并且它将图像分割为您说的数量)。Python/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

一个图像210

这就是问题... 假设发生的事情是图像被分成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() 

任何解决?如果您需要我的更多信息,请询问任何事情。

回答

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)