如何在pygame中制作动画 [英] how to make an animation in pygame

查看:189
本文介绍了如何在pygame中制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作我的球员被击中时的动画,让他看起来好像要倒在地上.

i am trying to make an animation of when my player gets shot to make it seem like he is falling to the ground.

我尝试了下面的代码,但似乎不起作用.它减慢了帧的速度并且只显示我的动画的最后一张图像 在 pygame 中是否有更简单的动画方式?

i have tried the code below but that doesn't seem to work. it slows down the frames and only shows the last image of my animation is there a simpler way of animation in pygame?

    if player2_hit_sequence == True:
        stage1 = True
        if stage1 == True:
            game_display.blit(dying1_p2, (player2X, player2Y))
            time.sleep(0.2)
            stage1 = False
            stage2 = True
        if stage2 == True:
            game_display.blit(dying2_p2, (player2X, player2Y))
            time.sleep(0.2)
            stage2 = False
            stage3 = True
        if stage3 == True:
            game_display.blit(dying3_p2, (player2X, player2Y))
            time.sleep(0.2)

是否有一个功能可以制作一系列图像或类似的东西?

is there a function to make a sequence of images or something like that?

推荐答案

好的,对于动画,您需要一堆图像和一个计时器.

Ok so for animation you need a bunch of images, and a timer.

我将展示一些基于 pygame 精灵的代码片段.也许这并不完全符合问题,但它似乎是比手动绘制/blitting 图像更好的解决方案.

I'm presenting some code snippets based around a pygame sprite. Maybe this doesn't exactly fit the question, but it seems like a better solution then painting/blitting images manually.

所以首先代码从一个精灵类开始:

So first the code starts with a sprite class:

class AlienSprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.base_image = pygame.image.load('alien.png').convert_alpha()
        self.image = self.base_image
        self.rect = self.image.get_rect()
        self.rect.center = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
        # Load warp animation
        self.warp_at_time = 0
        self.warp_images = []
        for filename in [ "warp1.png", "warp2.png", "warp3.png" ]:
            self.warp_images.append( pygame.image.load(filename).convert_alpha() )

所以这个想法是 Alien Sprite 有一个正常"的图像,但是当它扭曲"(传送)时会播放动画.实现的方式是拥有一个动画图像列表.当动画开始时,精灵的 imagebase_image 更改为 warp_images[] 的第一个.随着时间的流逝,精灵图像更改为下一帧,然后是下一帧,最后恢复到基础图像.通过将所有这些嵌入到精灵 update() 函数中,精灵的正常更新机制处理外星精灵的当前状态",正常或扭曲".一旦扭曲"状态被触发,它就会运行,而无需 pygame 主循环的任何额外参与.

So the idea is the Alien Sprite has a "normal" image, but then when it "warps" (teleports) an animation plays. The way this is implemented is to have a list of animation images. When the animation starts, the sprite's image is changed from base_image to the first of the warp_images[]. As time elapses, the sprites image is changed to the next frame, and then the next, before finally reverting back to the base image. By embedding all this into the sprite update() function, the normal updating mechanism for sprites handles the current "state" of the alien sprite, normal or "warp". Once the "warp"-state is triggered, it runs without any extra involvement of the pygame main loop.

def update(self):
    # Get the current time in milliseconds (normally I keep this in a global)
    NOW_MS = int(time.time() * 1000.0)
    # Did the alien warp? (and at what time)
    if (self.warp_at_time > 0):
        # 3 Frames of warp animation, show each for 200m
        ms_since_warp_start = NOW_MS - self.warp_at_time
        if ( ms_since_warp > 600 ):
            # Warp complete
            self.warp_at_time = 0
            self.image = self.base_image  # return to original bitmap
            # Move to random location
            self.rect.center = ( random.randrange( 0, WINDOW_WIDTH ), random.randrange( 0,  WINDOW_HEIGHT ) )
        else:
            image_number = ms_since_warp // 200  # select the frame for this 200ms period
            self.image = self.warp_images[image_number] # show that image

def startWarp(self):
    # Get the current time in milliseconds (normally I keep this in a global)
    NOW_MS = int(time.time() * 1000.0)
    # if not warping already ...
    if (self.warp_at_time == 0):
        self.warp_at_time = NOW_MS

所以首先要注意的是,update() 使用时钟来知道自动画开始以来经过的毫秒数.为了跟踪时间,我通常在游戏循环中设置全局 NOW_MS.

So the first thing to notice, is that the update() uses the clock to know the number of elapsed milliseconds since the animation started. To keep track of the time I typically set a global NOW_MS in the game loop.

在精灵中,我们有 3 帧动画,每帧之间有 200 毫秒.要启动精灵动画,只需调用 startWarp() 显然只是启动计时器.

In the sprite, we have 3 frames of animation, with 200 milliseconds between each frame. To start a sprite animating, simply call startWarp() which obviously just kicks-off the timer.

SPRITES = pygame.sprite.Group()
alien_sprite = AlienSprite()
SPRITES.add(alien_sprite)

...

# Game Loop
done = False
while not done:
    SPRITES.update()

    # redraw window
    screen.fill(BLACK)
    SPRITES.draw(screen)
    pygame.display.update()
    pygame.display.flip()

    if (<some condition>):
        alien_sprite.startWarp()  # do it

显然所有这些帧时间和what-not 应该是 sprite 类的成员变量,但我没有这样做以保持示例简单.

Obviously all those frame timings & what-not should be member variables of the sprite class, but I didn't do that to keep the example simple.

这篇关于如何在pygame中制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆