我创建了一个用于导入图像的自定义模块,并能够对其进行调整并轻松设置其动画效果(基本上,您说的是具有动画的帧,并将图像分割成您说的数量)。
(我知道它们并不完美!)
这是模块“ 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()
可以解决吗?问任何问题,如果您需要我的更多信息。
居然修好了!
您的代码:
#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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句