【问题标题】:How Do I Slow Down Sprite FPS in Pygame?如何在 Pygame 中减慢 Sprite FPS?
【发布时间】:2020-05-24 14:19:15
【问题描述】:

所以我在这里有我的精灵,我想知道如何在不减慢游戏速度的情况下减慢速度? video of my sprite 正如您在视频中看到的那样,它移动得很快,我想放慢速度,我该怎么做

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.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.anim_index += 1
            if self.anim_index == len(self.games):
                self.anim_index = 0

【问题讨论】:

    标签: python html pygame


    【解决方案1】:

    如果你想让动画变慢,那么你必须将动画索引除以某个数字。添加一个属性 (anim_frames),该属性定义动画图像的帧数并将anim_index 除以anim_frames。商是当前图像索引。如果超过最大索引,则重新启动动画 (anim_index = 0)。因此anim_frames 控制动画的速度。例如:

    class snow:
        def __init__(self,x,y,height,width,color):
            # [...]
    
            self.anim_index = 0
            self.anim_frames = 10
    
            # [...]
    
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            if self.direction == "idk":
    
                img_index = self.anim_index // self.anim_frames
                if img_index >= len(self.games):
                    img_index = 0
                    self.anim_index = 0
                self.anim_index += 1   
    
                window.blit(self.games[img_index], self.rect)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多