【问题标题】:Frame animation from images. Pygame (python3)从图像帧动画。 Pygame (python3)
【发布时间】:2012-12-27 02:34:00
【问题描述】:

我有带有动画的文件 (*.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 个带帧的文件,你能想象这个函数会是什么样子吗?

【问题讨论】:

  • 我无法理解您编写的代码。你能把整个东西放在没有分裂、乱序或缩写等的地方吗?谢谢,一旦你这样做了,我会试着找出你的问题。
  • 我有 10 个文件,想要制作动画。就是这样。
  • 我尝试修改我的问题。
  • 如果有人知道更好的图片加载方法-请指教。

标签: image animation python-3.x frame pygame


【解决方案1】:

为了加载动画,你可以使用 for 循环来遍历你需要加载的东西,因为它们的命名很方便(很好):

frames=[]
for i in range(42, 52):
    frames.append(pygame.image.load('Gfx/Dices/dice00'+str(i)+'.png').convert_alpha())

现在所有帧都在一个列表中,这使得对它们进行 blit 变得更加容易。

for frame in frame:
    screen.blit(frame, ???)

通过这种方式,您可以在没有任何冗长或重复的情况下对动画进行 blit 和加载

【讨论】:

    【解决方案2】:

    一种方法是使用精灵表来制作动画。

    动画加载:

    def load_anim(start=42, stop=52):
        # loads array of many frames
        files = [ "Gfx/Dices/dice00{}.png".format(i) for i in range(start, stop) ]
        frames = []
    
        for file in files:
            surf = pygame.image.load(file).convert_alpha()
            frames.append(surf)
        return frames
    

    我不确定你是如何制作动画的。如果是基于时间的,还是什么?

    点击

    if event.type == pygame.MOUSEBUTTONDOWN:
        if gameState==7:
            # I'm assuming this is the rect of a sprite.
            # Otherwise you can still create the Rect
            if dice_sprite.get_rect.collidepoint(event.pos)
                gameState = 9
                rolling = 1
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多