移动时如何修复图像精灵闪烁效果?

时间:2020-06-21 18:50:16

标签: python pygame

VIDEO,所以当我运行动画并使其移动以某种原因使图像闪烁时,我不知道为什么 如果我尝试self.bright // 3,我将得到索引超出范围错误

                # this makes the enemy move right and left
        def draw(self,window):
            self.move()
            if self.Walking + 1 >= 33:
                self.Walking = 0
            elif self.vel > 0:
                window.blit(self.bright[self.Walking % 3], (self.x,self.y))
                self.Walking += 1
            else:
                window.blit(self.bleft[self.Walking % 3], (self.x,self.y))
                self.Walking += 1


鸟班


 # ---------------------------------------------- # bird class
 
    class bird:
        def __init__(self,x,y,height,width,end):
            self.x = x
            self.y =y
            self.bright = [pygame.image.load("bird1.png"),
            pygame.image.load("bird2.png"),
            pygame.image.load("bird3.png"),
            pygame.image.load("bird4.png")
                              ]
            self.bleft = [pygame.image.load("b1.png"),
            pygame.image.load("b2.png"),
            pygame.image.load("b3.png"),
            pygame.image.load("b4.png")
                              ]
            self.bright = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.bright]
            self.bleft = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.bleft]
            self.height = height
            self.width = width
            self.anim_index = 0
            self.distance = 80
            self.speed = 8
            self.vel = 3
            self.Walking = 0
            self.path = [x,end]
            self.hitbox = (self.x + 17, self.y + 2, 31, 57)
            self.rect = pygame.Rect(x,y,height,width)
            COOLDOWN = 30
            # enemys health
            self.health = 10
            self.visible = True

                # this makes the enemy move right and left
        def draw(self,window):
            self.move()
            if self.Walking + 1 >= 33:
                self.Walking = 0
            elif self.vel > 0:
                window.blit(self.bright[self.Walking % 3], (self.x,self.y))
                self.Walking += 1
            else:
                window.blit(self.bleft[self.Walking % 3], (self.x,self.y))
                self.Walking += 1

    # this moves the enemy left and right
        def move(self):
            if self.visible:
                if self.vel > 0:
                   if self.x + self.vel < self.path[1]:
                       self.x += self.vel
                   else:
                       self.vel = self.vel * -1
                       self.Walking_index = 0
                else:
                   if self.x - self.vel >  self.path[0]:
                       self.x += self.vel
                   else:
                       self.vel = self.vel * -1
                       self.Walking_index = 0
                    # the hit box for the enemy the health
                pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 70, 10)) # NEW
                pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 70 - (5 * (10 - self.health)), 10))
                self.hitbox = (self.x + 47, self.y + 31, 50, 72)

     
    # THIS PART MAKES  the enemy not scroll with the player
        def scroll(self,sx, sy):
            self.x += sx
            self.y += sy
            self.path[0] += sx
            self.path[1] += sx

我不确定它是否与self.move()或我如何使图像发but,但我不确定它是否与self.move()或我如何使图像起毛。图片

1 个答案:

答案 0 :(得分:1)

问题在于,draw()不会绘制self.Walking + 1 >= 33函数。这会导致闪烁。

    # this makes the enemy move right and left
    def draw(self,window):
        self.move()
        if self.Walking + 1 >= 33:
            self.Walking = 0               # <-- HERE, no blit()
        elif self.vel > 0:
            window.blit(self.bright[self.Walking % 3], (self.x,self.y))
            self.Walking += 1
        else:
            window.blit(self.bleft[self.Walking % 3], (self.x,self.y))
            self.Walking += 1

可能只需将elif更改为if即可解决:

    def draw(self,window):
        self.move()
        if self.Walking + 1 >= 33:
            self.Walking = 0

        if self.vel > 0:        # right
            window.blit(self.bright[self.Walking % 3], (self.x,self.y))
            self.Walking += 1
        else:                   # left
            window.blit(self.bleft[self.Walking % 3], (self.x,self.y))
            self.Walking += 1

您为什么使用self.Walking + 1 >= 33?代码读起来就像您正在加载4图像一样,因此也应该是self.Walking % 4(给出0、1、2、3)。如果您使用动画图像列表的长度进行测试,则您的代码将比使用这样的常量更健壮和可读:

anim_list_length = len( self.bright )
if self.Walking >= anim_list_length:
    self.Walking = 0

...

     window.blit( self.bright[ self.Walking % anim_list_length ] )

因此,如果动画帧数发生变化,则无需更改代码。