Pygame滚动背景滞后

发布于 2025-01-02 21:13:15 字数 204 浏览 0 评论 0原文

在 pygame 中,我希望有一个滚动背景,以便玩家可以在大范围内移动,我已经很容易地做到了这一点,但游戏运行速度非常慢。

我的一些代码: http://pastebin.com/1EzDV7mc

我在做什么效率低下? 我怎样才能让它运行得更快?

In pygame I wish to have a scrolling background so that the player can move around a large area, I have done this rather easily, but the game runs very slowly.

some of my code: http://pastebin.com/1EzDV7mc

what am I doing inefficiently?
how can I make it run faster?

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

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

发布评论

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

评论(2

り繁华旳梦境 2025-01-09 21:13:15

请参阅 PyGame 新手指南

特别是:

  1. 使用 surface.convert()< /p>

  2. 理想情况下使用脏矩形动画,仅更新屏幕上已更改的部分,而不是整个视图。也就是说,我之前曾使用 PyGame 进行全屏更新(对于整个屏幕不断滚动的游戏来说,这是你需要做的),只要你没有太多,在现代计算机上它并没有那么糟糕 理想情况下使用脏矩形动画

该指南中没有提到:

  1. 您的目标是使用clock.tick(60)实现60fps,目标较低,30fps就可以了,不会对CPU造成太大影响

  2. 如果你需要进行全屏滚动更新,那么要么 a) 不要使用很多对象,要么 b) 停止使用 PyGame 并切换到 OpenGL

  3. 为什么要使用中点位图传输图像,而不是他们的左上角?错误?

  4. 更常见的做法是将尺寸属性存储为单独的宽度和高度属性,而不是存储为必须索引的列表。这使您的代码更加清晰。

See A Newbie Guide to PyGame

In particular:

  1. Use surface.convert()

  2. Ideally use dirty rect animation, only updating the parts of the screen that have changed, not the entire view. That said I have previously used whole screen updates with PyGame (which is what you need to do for a game where the whole screen is constantly scrolling), and it's not that bad on a modern computer as long as you don't have too many objects.

Not mentioned in that guide:

  1. you are aiming for 60fps with clock.tick(60), aim lower, 30fps is fine and won't hose the CPU as much

  2. If you need to do full screen updates for scrolling, then either a) don't use many objects or b) stop using PyGame and switch to OpenGL

  3. why are you blitting images using their mid points, rather than their top left point? A mistake?

  4. It is more usual to store size attributes as seperate width and height attribute, not as a list you have to index. This makes your code much clearer.

走走停停 2025-01-09 21:13:15

除了纯一郎所说的之外,我确实找到了一个网站,提供了他所说的信息以及一些信息。

https://www.pygame.org/docs/tut/newbieguide.html

我并不是说你是新手,即使我从这个网站学到了很多东西,而且我已经使用 pygame 6 年了。

此外,surface.convert() 仅适用于非透明图像。使用surface.convert_alpha(),它适用于所有东西。我通常创建一个函数,这样我就不必每次要加载图像时都输入整个内容。请随意使用以下 2 行代码:

def loadify(imgname):
    return pygame.image.load(imgname).convert_alpha()

这将我的游戏的 fps 从 18 fps 增加到 30 fps。

祝你好运!

Adding to what junichiro said, I did find a website giving the information he said plus some.

https://www.pygame.org/docs/tut/newbieguide.html

I'm not calling you a newbie, even I learned quite a bit from this website, and I've been using pygame for 6 years.

Also, surface.convert() only works with non-transparent images. Use surface.convert_alpha(), which works with everything. I usually create a function so I don't have to type the entire thing every time I want to load an image. Feel free to use the following 2 lines of code:

def loadify(imgname):
    return pygame.image.load(imgname).convert_alpha()

This increased the fps of my game from 18 to 30 fps.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文