简体   繁体   中英

Image Sprite Flickers When Moving How To Fix?

VIDEO so when I run the animation and make it move the image flickers for some reason I dont know why if I try to self.bright //3 I will get a index out of range error

                # 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


the bird class


 # ---------------------------------------------- # 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

I am not sure if it has anything to do with the self.move() or how I am blitting the image but II am not sure if it has anything to do with the self.move() or how I am blitting the image

The issue is that the draw() function does not draw when self.Walking + 1 >= 33 . This would cause flicker.

    # 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

Probably just changing the elif to if would fix it:

    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

Why are you using self.Walking + 1 >= 33 ? The code reads like you're loading 4 images, so that should be self.Walking % 4 too (which gives 0, 1, 2, 3). Rather than using constants like this, your code would be more robust and readable if it used the length of the animations image-list for tests:

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

...

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

So if the number of animation frames change, no code changes are needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM