如何使用类,功能和for循环为pygame屏幕进行动画屏幕?
我正在将自己置于SO社区的怜悯之中,现在已经看了以下时间了。我正在尝试为我的标题屏幕创建动画背景,该背景通过名为background_images的列表中的八个元素循环。通过调用rocket_settings类中的函数bg_start_animation,我希望使用列表的第一个元素填充自background。在屏幕上,将屏幕刷新在display.flip中,然后由列表的其他七个元素重复该过程以创建动画效果。
我使用以下方式存储了背景图像的最新迭代:
animated_bg = rocket_settings.bg_start_animation()
并闪烁到屏幕:
screen.blit(animated_bg, (0, 0))
请注意:Rocket_settings调用我在代码主体下方复制的类。
while True:
pygame.time.delay(5)
gf.check_events(rocket_settings, screen, character)
character.update()
gf.update_screen(rocket_settings, screen, character)
# Redraw the screen during each pass through the loop
character.update()
#screen.blit(rocket_settings.background, (0, 0))
animated_bg = rocket_settings.bg_start_animation()
screen.blit(animated_bg, (0, 0))
screen.blit(rocket_settings.title, (300, 180))
character.blitme()
#print(character.rect.bottom, ", ", character.rect.y) - to identify the coords
#of the desired spawn point by experimenting in runtime.
# Make the most recently drawn screen visible
pygame.display.flip()
火箭设置的相关部分:
def bg_start_animation(self):
# Screen animation loop
background_images = ["images/wick_title_screen_1.bmp",
"images/wick_title_screen_2.bmp", "images/wick_title_screen_3.bmp",
"images/wick_title_screen_4.bmp", "images/wick_title_screen_5.bmp",
"images/wick_title_screen_6.bmp", "images/wick_title_screen_7.bmp",
"images/wick_title_screen_8.bmp"]
for background in background_images:
self.background = pygame.image.load(background)
return self.background
我必须忽略某些东西。预先感谢您,感谢您的帮助。
I'm throwing myself at the mercy of the SO community, having looked at the below now for considerable time. I'm attempting to create an animated background for my title screen which loops through eight elements in a list called background_images. By calling the function bg_start_animation in the rocket_settings class, I am expecting to have self.background be populated with the first element of the list, for this to be passed to the main body of the code using return self.background, for this be blit to the screen, have the screen refreshed at display.flip, and then have the process repeated by the other seven elements of the list to create the animation effect.
I store the latest iteration of background image using:
animated_bg = rocket_settings.bg_start_animation()
and blit to the screen:
screen.blit(animated_bg, (0, 0))
please note: rocket_settings calls the Class for which I have copied below the main body of the code.
while True:
pygame.time.delay(5)
gf.check_events(rocket_settings, screen, character)
character.update()
gf.update_screen(rocket_settings, screen, character)
# Redraw the screen during each pass through the loop
character.update()
#screen.blit(rocket_settings.background, (0, 0))
animated_bg = rocket_settings.bg_start_animation()
screen.blit(animated_bg, (0, 0))
screen.blit(rocket_settings.title, (300, 180))
character.blitme()
#print(character.rect.bottom, ", ", character.rect.y) - to identify the coords
#of the desired spawn point by experimenting in runtime.
# Make the most recently drawn screen visible
pygame.display.flip()
relevant part of rocket settings:
def bg_start_animation(self):
# Screen animation loop
background_images = ["images/wick_title_screen_1.bmp",
"images/wick_title_screen_2.bmp", "images/wick_title_screen_3.bmp",
"images/wick_title_screen_4.bmp", "images/wick_title_screen_5.bmp",
"images/wick_title_screen_6.bmp", "images/wick_title_screen_7.bmp",
"images/wick_title_screen_8.bmp"]
for background in background_images:
self.background = pygame.image.load(background)
return self.background
I must be overlooking something. Thanks in advance, I appreciate the assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参见来自少数图像的动画精灵。您可以在精灵和背景上使用该方法。甚至可以从GIF加载图像。
在应用程序循环之前创建图像列表,然后在应用程序循环中绘制一个图像。使用计数器(
background_index
)通过图像迭代:See Animated sprite from few images. You can use the approach on both a sprite and the background. It is even possible to load the images from a GIF.
Create a list of images before the application loop and draw one image in the application loop. Use a counter (
background_index
) to iterate through the images: