我有带动画的文件 (*.png)。每个文件- 框架。所以 10 个文件 - 10 帧。完整动画重复 4 次。所以我需要渲染图像 20 次。
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()
任何人都可以建议具有最小代码长度的功能。在主程序循环中启动:
if event.type == pygame.MOUSEBUTTONDOWN:
if gameState==7:
if 165<pointer_coord[0]<424 and 249<pointer_coord[1]<607 :
gameState=9
rolling=1
这意味着用户点击区域(x = 165,y = 249,长度= 424-165,高度= 607-249),然后游戏进入状态9。状态9 - 为播放动画而创建。
if gameState==9:
RollingAnimation(166,253)
RollingAnimation(210,278)
RollingAnimation(166,303)
RollingAnimationControl()
这个函数 RollingAnimation 是:
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])
为了控制动画,我使用了一个控制函数 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
问题是:我怎样才能最小化这个功能。如果动画使用 100 个带帧的文件,你能想象这个函数的外观吗?