繁体   English   中英

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

[英]How do I add more than 2 sprites into an animation in 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它会出现并停留在屏幕上,但不会消失。 如果可以的话,请帮助我看看问题出在哪里。 谢谢! (我希望闪电开始并一直贯穿动画,然后消失。然后再次开始并重复)。

首先创建图像列表,然后可以通过以下方式使用它:

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张图像才能将其视为流畅的动画。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM