在与某些东西发生碰撞后动画死神精灵

时间:2016-01-12 22:00:22

标签: python animation pygame

我已成功为我正在制作的平台游戏中的步行玩家精灵制作动画。当玩家与陷阱发生碰撞时,我希望它能够渲染他死亡的图像,而不是他走路的图像。有什么想法吗?

class Player(pygame.sprite.Sprite):
def __init__(self, x, y, width = 65, height = 35):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.hspeed = 2
    self.vspeed = 0

    self.images = []
    r0 = pygame.image.load("Images\Player\i1.png")
    r1 = pygame.image.load("Images\Player\i2.png")
    r2 = pygame.image.load("Images\Player\i3.png")
    r3 = pygame.image.load("Images\Player\i4.png")
    hurt = pygame.image.load("Images\Player\Hurt.png")
    self.images.append(r0)
    self.images.append(r1)
    self.images.append(r2)
    self.images.append(r3)

    self.gravity = 0.5
    self.index = 0
    self.image = self.images[self.index]
    self.rect = pygame.Rect(self.x,self.y,width,height)

    self.TimeNum = 0
    self.TimeTarget = 10

    self.OnGround = False
    self.collision = False

def update(self, BlockListGrass, TrapList):

    key = pygame.key.get_pressed()

    if key[pygame.K_RIGHT] or key[pygame.K_LEFT]:
        self.TimeNum += 1
        if self.TimeNum == self.TimeTarget:
            self.index += 1
            if self.index >= len(self.images):
                self.index = 0
            self.image = self.images[self.index]

            self.TimeNum = 0

    collision = pygame.sprite.spritecollide(self, BlockListGrass, False )
    for each_object in collision:
        if self.vspeed > 0:
            self.rect.bottom = each_object.rect.top
        if self.vspeed < 0:
            self.rect.top = each_object.rect.bottom

    TrapCollision = pygame.sprite.spritecollide(self,TrapList,False )
    for each_object in TrapCollision:
        self.collision = True
        if self.collision == True:
            #not sure what to do here

1 个答案:

答案 0 :(得分:0)

只需添加另一张图片,而精灵就是&#34;死了&#34;:

# In your Player class
dead = pygame.image.load("Images\Player\dead.png")
self.images.append(dead)

当您的精灵触发陷阱并死亡时,只需将图片更改为dead

if self.collision: # It is the same as yur original, just shorter
    self.image = dead