如何在pygame中修复我的精灵动画?

时间:2019-03-24 13:18:41

标签: python pygame sprite

我正在为学校项目用python 3(使用pygame)编写游戏,但是我在做一些事情: 我试图让我的精灵在移动时具有动画效果,但它只显示静态图像: 当我按向左键时,子画面的图像更改为静态图像,而不是由3 png组成的动画。 这是代码的一部分:

clock = pygame.time.Clock()

walkRight = [pygame.image.load('ressources/images/R1.png'), pygame.image.load('ressources/images/R2.png'), pygame.image.load('ressources/images/R3.png')]
walkLeft = [pygame.image.load('ressources/images/L1.png'), pygame.image.load('ressources/images/L2.png'), pygame.image.load('ressources/images/L3.png')]
walkUp = [pygame.image.load('ressources/images/U1.png'), pygame.image.load('ressources/images/U2.png'), pygame.image.load('ressources/images/U3.png')]
walkDown = [pygame.image.load('ressources/images/D1.png'), pygame.image.load('ressources/images/D2.png'), pygame.image.load('ressources/images/D3.png')]


#this is the player sprite

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = (walkDown[4//3])
        self.rect = self.image.get_rect()
        self.rect.centerx = width / 2
        self.rect.bottom = height / 2
        self.speedx = 0
        self.speedy = 0
        self.velocity = 2


    def update(self):
        self.speedx = 0
        self.speedy = 0
        left = False
        right = False
        up = False
        down = False
        run = False  
        walkCount = 4
        runCount = 4   
        clock.tick(FPS)
        keys = pygame.key.get_pressed()

#I believe this is the part that has something wrong!
        if walkCount + 1 >= 9:
            walkCount = 0
        if (down == True and run == False):
            walkCount += 1
            self.image = (walkDown[walkCount//3])     
        elif (up == True and run == False):
            walkCount += 1
            self.image = (walkUp[walkCount//3])
        elif (right == True and run == False):
            walkCount += 1
            self.image = (walkRight[walkCount//3])
        elif (left == True and run == False):
            walkCount += 1
            self.image = (walkLeft[walkCount//3])

1 个答案:

答案 0 :(得分:0)

walkCount是方法update中的局部变量。每次调用4时,它都会由update初始化。因此,walkCount的值每次在函数开始时都是相同的。这导致图像似乎是静态的。
walkCount必须是类Player的属性,而不是局部变量:

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = (walkDown[4//3])
        self.rect = self.image.get_rect()
        self.rect.centerx = width / 2
        self.rect.bottom = height / 2
        self.speedx = 0
        self.speedy = 0
        self.velocity = 2

        self.walkCount = 4 # <---- add attribute

    def update(self):

        # [...]

        # walkCount = 4  <------ delete local variable

        keys = pygame.key.get_pressed()
        down = keys[pygame.K_DOWN]
        up = keys[pygame.K_UP]
        right = keys[pygame.K_RIGHT]
        left = keys[pygame.K_LEFT]
        run = False

        if self.walkCount + 1 >= 9:
            self.walkCount = 0
        if (down == True and run == False):
            self.walkCount += 1
            self.image = (walkDown[self.walkCount//3])     
        elif (up == True and run == False):
            self.walkCount += 1
            self.image = (walkUp[self.walkCount//3])
        elif (right == True and run == False):
            self.walkCount += 1
            self.image = (walkRight[self.walkCount//3])
        elif (left == True and run == False):
            self.walkCount += 1
            self.image = (walkLeft[self.walkCount//3])