Python很多图像

发布于 2025-01-29 05:21:33 字数 1206 浏览 5 评论 0原文

我想在pygame中有一个动画的背景,由于我没有找到任何Bette Soultions,因此我决定在Sprites的帮助下做到这一点。据我所知,pygame不支持视频文件,我将视频分为帧并制作了这样的代码:

class BackgroundAnimator(pygame.sprite.Sprite):

    def __init__(self,pos_x,pos_y):
        super().__init__()
        self.sprites = []
        self.is_animating = False
        self.sprites.append(pygame.transform.scale(pygame.image.load(os.path.join("Textures", "backgroundframe1.jpg")),(WIDTH,HEIGHT)))

这里我必须像200次一样复制此行

        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]
    
    def animate(self):
        self.is_animating=True

    def update(self):
        if self.is_animating == True:
            self.current_sprite += 0.14
            if self.current_sprite >= len(self.sprites):
                self.current_sprite = 0
            self.image=self.sprites[int(self.current_sprite)]

moving_backgroundsprites = pygame.sprite.Group()
backgroundlocation = BackgroundAnimator(0,0)

在 解决方案比这更好,但我不知道从哪里开始。还是如何加载所有图像,而无需粘贴200次相同的行,然后更改文件的名称(例如背景框架1,然后是BackgroundFrame2等等)?

I want to have an animated backround in pygame, and since I didn't find any bette soulutions, I have decided to make it with the help of sprites. As far as I know, pygame doesn't support video files, I split the video into frames and made a code like this:

class BackgroundAnimator(pygame.sprite.Sprite):

    def __init__(self,pos_x,pos_y):
        super().__init__()
        self.sprites = []
        self.is_animating = False
        self.sprites.append(pygame.transform.scale(pygame.image.load(os.path.join("Textures", "backgroundframe1.jpg")),(WIDTH,HEIGHT)))

here I would have to copy this line like 200 times so each frame would be in the sprite

        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]
    
    def animate(self):
        self.is_animating=True

    def update(self):
        if self.is_animating == True:
            self.current_sprite += 0.14
            if self.current_sprite >= len(self.sprites):
                self.current_sprite = 0
            self.image=self.sprites[int(self.current_sprite)]

moving_backgroundsprites = pygame.sprite.Group()
backgroundlocation = BackgroundAnimator(0,0)

There is probably a way better solution than this, but I have no idea where to begin. Or how can i load all images without having to paste the same line 200 times, and change the name of the file (so for example backgroundframe1 and then backgroundframe2 and the 3 and so on)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-02-05 05:21:33

只需加入名称和数字即可。该数字可以用 str函数:

for i in range(1, 100):
    filename = "backgroundframe" + str(i) + ".jpg"

或者使用

for i in range(1, 100):
    filename = f"backgroundframe{i}.jpg"

例如:

class BackgroundAnimator(pygame.sprite.Sprite):
    def __init__(self,pos_x,pos_y, filename):
        super().__init__()
        self.sprites = []
        self.is_animating = False
        path = os.path.join("Textures", filename)
        self.sprites.append(pygame.transform.scale(pygame.image.load(path),(WIDTH,HEIGHT)))
no_of_frames = 100 # depends on the number of files
moving_backgroundsprites = pygame.sprite.Group()
for i in range(1, no_of_frames):
    filename = f"backgroundframe{i}.jpg"
    backgroundlocation = BackgroundAnimator(0, 0, filename)
    moving_backgroundsprites.add(backgroundlocation)

Just concatenate the name and the number. The number can be converted to a string with the str function:

for i in range(1, 100):
    filename = "backgroundframe" + str(i) + ".jpg"

Alternatively use f-strings:

for i in range(1, 100):
    filename = f"backgroundframe{i}.jpg"

e.g.:

class BackgroundAnimator(pygame.sprite.Sprite):
    def __init__(self,pos_x,pos_y, filename):
        super().__init__()
        self.sprites = []
        self.is_animating = False
        path = os.path.join("Textures", filename)
        self.sprites.append(pygame.transform.scale(pygame.image.load(path),(WIDTH,HEIGHT)))
no_of_frames = 100 # depends on the number of files
moving_backgroundsprites = pygame.sprite.Group()
for i in range(1, no_of_frames):
    filename = f"backgroundframe{i}.jpg"
    backgroundlocation = BackgroundAnimator(0, 0, filename)
    moving_backgroundsprites.add(backgroundlocation)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文