Python很多图像
我想在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需加入名称和数字即可。该数字可以用
str
函数:
或者使用:
例如:
Just concatenate the name and the number. The number can be converted to a string with the
str
function:Alternatively use f-strings:
e.g.: