如何在pygame中为动画添加2个以上的精灵?

时间:2014-07-11 02:46:47

标签: python animation pygame

#Importing the pygame functions
import pygame 
import sys
import os
from pygame.locals import *

#Allows for the editing of a window
pygame.init() 
#Sets screen size
window = pygame.display.set_mode((800,600),0,32) 
#Names the window
pygame.display.set_caption("TEST") 
#Types of colors (red,green,blue)
black = (0,0,0) 
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
red = (255,0,0)
purple = (255,0,255)
lightblue = (0,255,255)
white = (255,255,255)
pink = (255,125,125)

clock = pygame.time.Clock()

L1="bolt_strike_0001.PNG"
L1=pygame.image.load(L1).convert_alpha()
L2="bolt_strike_0002.PNG"
L2=pygame.image.load(L2).convert_alpha()
L3="bolt_strike_0003.PNG"
L3=pygame.image.load(L3).convert_alpha()
L4="bolt_strike_0004.PNG"
L4=pygame.image.load(L4).convert_alpha()

lightingCurrentImage = 1


#Loop
gameLoop = True 
while gameLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameLoop=False #Allows the user to exit the loop/game
    window.fill(black) #used to fill the creen with the certian color variables
    if (lightingCurrentImage==1): 
        window.blit(L1, (0,0))
    if (lightingCurrentImage==2):
        window.blit(L2, (0,0))
    if (lightingCurrentImage==3):
        window.blit(L3, (0,0))
    if (lightingCurrentImage==4):
        window.blit(L4, (0,0))
    if (lightingCurrentImage==2):
        lightingCurrentImage=1
    if (lightingCurrentImage==3):
        lightingCurrentImage=2
    if (lightingCurrentImage==4):
        lightingCurrentImage=3

    else:

        lightingCurrentImage+=3;

    pygame.display.flip() #must flip the image o the color is visable

    clock.tick(5)

pygame.quit() #quit the pygame interface
exit(0)

我在pygame中将10个闪电动画图像拼接在一起时遇到了问题。我现在所拥有的东西,但它不是我想要的样子。当我运行它时会发生什么,闪电一旦创建动画序列就消失了,再也不会重新启动序列。如果我将lightingCurrentImage+=3设置为lightingCurrentImage+=2它会出现并停留在屏幕上,但不会消失。如果可以的话,请帮我看看问题所在。谢谢! (我希望闪电开始并一直通过动画然后消失。然后重新开始并重复)。

1 个答案:

答案 0 :(得分:2)

首先创建图像列表,然后您可以这样使用它:

bold_imgs = []

bold_imgs.append( pygame.image.load("bolt_strike_0001.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0002.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0003.PNG").convert_alpha() )
bold_imgs.append( pygame.image.load("bolt_strike_0004.PNG").convert_alpha() )

lightingCurrentImage = 0

while True:

     # here ... your code with events

    window.fill(black)

    window.blit( bold_imgs[ lightingCurrentImage ], (0,0))

    lightingCurrentImage += 1

    if lightingCurrentImage = len( bold_imgs ):
        lightingCurrentImage = 0

    pygame.display.flip() 

    clock.tick(5)

您可以使用tick(25)来获得更快但更流畅的动画。

人眼每秒至少需要25张图像才能将其视为平滑动画。