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

2024-05-08 01:49:38 发布

您现在位置:Python中文网/ 问答频道 /正文

#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,它会出现并停留在屏幕上,但不会消失。如果可以的话,请帮我看看有什么问题。谢谢!(我希望闪电开始,一直穿过动画,然后消失。然后重新开始并重复)。你知道吗


Tags: theimageimportalphal1convertifpng
1条回答
网友
1楼 · 发布于 2024-05-08 01:49:38

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

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个图像才能将其视为平滑动画。你知道吗

相关问题 更多 >