【问题标题】:Displaying a high resolution .png image with pygame使用 pygame 显示高分辨率 .png 图像
【发布时间】:2018-01-06 13:13:18
【问题描述】:

我目前正在尝试使用 pygame 开发电脑游戏 - 我想显示游戏的背景图像(2D 跳跃并运行)。

但是,当显示全屏背景图像时,我注意到帧速率显着下降,超过一定的窗口大小:

Windowsize | Time to blit the image | (~ FPS)

1280x720:  0.029s (~32 FPS)
1366x768:  0.031s (~29 FPS)
1600x900:  0.042s (~22 FPS)
1920x1080: 0.062s (~15 FPS)

是否有其他更有效的方式来对图像进行位图处理?我不想将游戏限制在 1366x768 或更低的小窗口大小......

我尝试使用 KB 大小较小的图像,并尝试使用 1/4 大小的 4 张图像来覆盖屏幕,但时间保持不变。

游戏在 pygame 屏幕上运行,所以我不能使用任何功能,例如Tkinter(因为他们需要一个 Tkinter 表面)。

目前我正在使用pygame的以下功能:

# Once at the beginning
screen = pygame.display.set_mode((screenWidth_X, screenHeight_Y))

# Every frame
screen.blit(background_Image, (0,0))

任何帮助/建议将不胜感激!

【问题讨论】:

  • 加载图片后,是否调用了convert方法(background_Image = background_Image.convert())?那应该有所作为。 Here's the documentation
  • 不,但我明天会试试看!感谢您的提示!
  • Blitting 未转换的表面确实会影响性能。始终使用convertconvert_alpha 方法。

标签: python image pygame


【解决方案1】:

根据Ted Klein Bergman 的评论,使用.convert().convert_alpha 是一个巨大的性能提升。

# From originally:
1920x1080: 0.062s (~15 FPS)
# To:
1920x1080: 0.002s (~140 FPS) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多