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

Python 移动时图像精灵闪烁如何修复?,python,pygame,Python,Pygame,因此,当我运行动画并使其移动时,图像会因某种原因闪烁,我不知道为什么 如果我尝试self.bright//3,我将得到一个索引超出范围的错误 # this makes the enemy move right and left def draw(self,window): self.move() if self.Walking + 1 >= 33: self.Wa

因此,当我运行动画并使其移动时,图像会因某种原因闪烁,我不知道为什么 如果我尝试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


#第三十四天——鸟类类
类鸟:
定义初始(自、x、y、高度、宽度、终点):
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(对于self.bright中的图像,(image.get\u width()//5,image.get\u height()//5)]
self.bleft=[pygame.transform.scale(对于self.bleft中的图像,(image.get\u width()//5,image.get\u height()//5)]
自我高度=高度
self.width=宽度
self.anim_索引=0
自我距离=80
自身速度=8
self.vel=3
自行行走=0
self.path=[x,end]
self.hitbox=(self.x+17,self.y+2,31,57)
self.rect=pygame.rect(x,y,高度,宽度)
冷却时间=30
#敌人的健康
自我健康=10
self.visible=True
#这使敌人左右移动
def绘图(自,窗口):
self.move()
如果自行行走+1>=33:
自行行走=0
elif self.vel>0:
blit(self.bright[self.Walking%3],(self.x,self.y))
自行行走+=1
其他:
blit(self.bleft[self.Walking%3],(self.x,self.y))
自行行走+=1
#这使敌人左右移动
def移动(自我):
如果自可见:
如果self.vel>0:
如果self.x+self.velself.path[0]:
self.x+=self.vel
其他:
self.vel=self.vel*-1
self.Walking_索引=0
#敌人的杀手锏是健康
pygame.draw.rect(窗口,(255,0,0),(self.hitbox[0],self.hitbox[1]-20,70,10))#新
pygame.draw.rect(窗口,(0255,0),(self.hitbox[0],self.hitbox[1]-20,70-(5*(10-self.health)),10))
self.hitbox=(self.x+47,self.y+31,50,72)
#此部分使敌人不能与玩家一起滚动
def滚动(自、sx、sy):
self.x+=sx
self.y+=sy
self.path[0]+=sx
self.path[1]+=sx

我不确定它是否与self.move()有关,或者我是如何快速移动图像的,但我不确定它是否与self.move()有关,或者我是如何快速移动图像的。问题是,
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
你为什么使用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 ] )
因此,如果动画帧数发生变化,则不需要更改代码

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

...

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