I have files (*.png) with animation. Each file – frame. So 10 files – 10 frames. Full animation repeat 4 times. So I need render images 20 times.
DiceAnim1=pygame.image.load('Gfx/Dices/dice0042.png').convert_alpha()
DiceAnim2=pygame.image.load('Gfx/Dices/dice0043.png').convert_alpha()
DiceAnim3=pygame.image.load('Gfx/Dices/dice0044.png').convert_alpha()
DiceAnim4=pygame.image.load('Gfx/Dices/dice0045.png').convert_alpha()
DiceAnim5=pygame.image.load('Gfx/Dices/dice0046.png').convert_alpha()
DiceAnim6=pygame.image.load('Gfx/Dices/dice0047.png').convert_alpha()
DiceAnim7=pygame.image.load('Gfx/Dices/dice0048.png').convert_alpha()
DiceAnim8=pygame.image.load('Gfx/Dices/dice0049.png').convert_alpha()
DiceAnim9=pygame.image.load('Gfx/Dices/dice0050.png').convert_alpha()
DiceAnim10=pygame.image.load('Gfx/Dices/dice0051.png').convert_alpha()
Can anyone suggest function with minimal code length.
Initiation in main programm loop:
if event.type == pygame.MOUSEBUTTONDOWN:
if gameState==7:
if 165<pointer_coord[0]<424 and 249<pointer_coord[1]<607 :
gameState=9
rolling=1
This is mean is user click in area (x=165, y=249, length=424-165, height=607-249), then game enter to state 9. State 9 – was created for playing animation.
if gameState==9:
RollingAnimation(166,253)
RollingAnimation(210,278)
RollingAnimation(166,303)
RollingAnimationControl()
This function RollingAnimation is:
def RollingAnimation(tx,ty):
global diceTikTac
screen.blit(DiceAnimShadow,[tx,ty])
if diceTikTac==1: screen.blit(DiceAnim1,[tx,ty])
if diceTikTac==2: screen.blit(DiceAnim2,[tx,ty])
if diceTikTac==3: screen.blit(DiceAnim3,[tx,ty])
if diceTikTac==4: screen.blit(DiceAnim4,[tx,ty])
if diceTikTac==5: screen.blit(DiceAnim5,[tx,ty])
if diceTikTac==6: screen.blit(DiceAnim6,[tx,ty])
if diceTikTac==7: screen.blit(DiceAnim7,[tx,ty])
if diceTikTac==8: screen.blit(DiceAnim8,[tx,ty])
if diceTikTac==9: screen.blit(DiceAnim9,[tx,ty])
if diceTikTac==10: screen.blit(DiceAnim10,[tx,ty])
To control animation I use a control function RollingAnimationControl:
def RollingAnimationControl():
global radiobuttonPos,diceTikTac,round1,gameState
diceTikTac=diceTikTac+1
if diceTikTac==11:
round1=round1+1
diceTikTac=1
if round1>4: # how much need loops animation.
gameState=11
diceTikTac=1
rolling=0
round1=0
Question is: How I can minimized this function. Can you imagine how would looks this function if animtion was use 100 files with frames.
For loading the animations, you can use a for loop to go through the things you need to load, because of their convinient naming (good job on that):
Now that the frames are all in a list, it makes it much easier to blit them.
In this way, you can blit and load your animation without any long or repetative