放慢动画帧会减慢整个游戏帧的速度我该如何解决? Pygame

我做了一个精灵,我想放慢动画,因为这是我所做的

for some reason its slowing my whole game frame to 1o in the class I set the frame by fp = 10 VIDeo

class snow:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.fp = 10
        self.clock = pygame.time.Clock()
        self.games = [
        pygame.image.load("key1.png"),
        pygame.image.load("key2.png"),
        pygame.image.load("key3.png"),
        pygame.image.load("key4.png"),
        pygame.image.load("key5.png"),
        pygame.image.load("key6.png"),
        pygame.image.load("key7.png")]
        self.anim_index = 0
        self.rect = pygame.Rect(x,y,height,width)
        self.direction = "idk"
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        if self.direction == "idk":
            window.blit(self.games[self.anim_index],self.rect)
            self.clock.tick(self.fp)
            self.anim_index += 1
            if self.anim_index == len(self.games):
                self.anim_index = 0 

评论
淡淡的思绪
淡淡的思绪

You should use pygame.time.get_ticks() to get current time and calculate when you should change image

 self.delay = 100

 self.next_frame = pygame.time.get_ticks() + self.delay

然后使用

            if pygame.time.get_ticks() >= self.next_frame:
                self.next_frame = pygame.time.get_ticks() + self.delay

                self.anim_index += 1
                if self.anim_index == len(self.games):
                    self.anim_index = 0 

或多或少:

class Snow:

    def __init__(self, x, y, height, width, color):
        self.rect = pygame.Rect(x, y, width, height)
        self.color = color
        self.games = [
            pygame.image.load("key1.png"),
            pygame.image.load("key2.png"),
            pygame.image.load("key3.png"),
            pygame.image.load("key4.png"),
            pygame.image.load("key5.png"),
            pygame.image.load("key6.png"),
            pygame.image.load("key7.png")
        ]
        self.anim_index = 0
        self.direction = "idk"

        self.delay = 100
        self.next_frame = pygame.time.get_ticks() + self.delay

    def draw(self):

        self.rect.topleft = (self.x,self.y)
        if self.direction == "idk":
            window.blit(self.games[self.anim_index], self.rect)

            if pygame.time.get_ticks() >= self.next_frame:
                self.next_frame = pygame.time.get_ticks() + self.delay

                self.anim_index += 1
                if self.anim_index == len(self.games):
                    self.anim_index = 0 
点赞
评论