Pygame 如何修复“尾随像素”?

发布于 2024-11-17 09:49:30 字数 183 浏览 4 评论 0原文

在此处输入图像描述

在图像中,红色轨迹是当我在精灵周围添加边界矩形时 pygame 创建的轨迹。精灵也可以做到这一点,最简单的解决方案是在每次重绘后将表面清除为黑色。然而,尝试在整个主表面上这样做并不是一个好主意。我该如何解决这个问题?

enter image description here

In the image the red trail is a trail that pygame is creating when I have a bounding rectangle added around sprites. The sprite also does it and the simplest solution was to just clear the surface to black after each redraw. However attempting to do so on the entire main surface is not such a good idea. How can I fix this?

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

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

发布评论

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

评论(4

关于从前 2024-11-24 09:49:30

通常您会这样做:

def draw():
    # fill screen with solid color.
    # draw, and flip/update screen.

但是,您可以只更新屏幕的脏部分。请参阅 pygame.display.update()

pygame.display.update()
更新软件显示的部分屏幕
更新(矩形=无)->无
更新(矩形列表)->无

该函数就像用于软件显示的 pygame.display.flip() 的优化版本。它只允许一部分
要更新的屏幕的一部分,而不是整个区域。如果没有争论
通过它会更新整个 Surface 区域,例如
pygame.display.flip()。

您可以向函数传递单个矩形或一系列矩形。一次传递多个矩形比传递多个矩形更有效
使用单个或部分列表多次调用更新
矩形。如果传递一系列矩形,则可以安全地包含
列表中没有值,将被跳过。

最好将其与 RenderUpdates,这是一个 Sprite.Group

pygame.sprite.RenderUpdates
跟踪脏更新的组子类。
RenderUpdates(*sprites) ->渲染更新
该类派生自 pygame.sprite.Group()。它有一个扩展的draw()方法来跟踪屏幕的变化区域。

Normally you will do:

def draw():
    # fill screen with solid color.
    # draw, and flip/update screen.

But, you can update just dirty portions of the screen. See pygame.display.update():

pygame.display.update()
Update portions of the screen for software displays
update(rectangle=None) -> None
update(rectangle_list) -> None

This function is like an optimized version of pygame.display.flip() for software displays. It allows only a portion
of the screen to updated, instead of the entire area. If no argument
is passed it updates the entire Surface area like
pygame.display.flip().

You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than
to call update multiple times with single or a partial list of
rectangles. If passing a sequence of rectangles it is safe to include
None values in the list, which will be skipped.

It's best to use it in combination with RenderUpdates, which is a Sprite.Group:

pygame.sprite.RenderUpdates
Group sub-class that tracks dirty updates.
RenderUpdates(*sprites) -> RenderUpdates
This class is derived from pygame.sprite.Group(). It has an extended draw() method that tracks the changed areas of the screen.

伏妖词 2024-11-24 09:49:30

只需有一个黑色矩形,然后将其覆盖在前一帧上精灵所在位置的上方,就可以将其删除。请记住在块传输精灵之前执行此操作,否则新精灵将部分变黑。

Just have a black rectangle and blit that overtop of where your sprite was on the previous frame and that should get rid of it. Just remember to do this before you blit your sprite or your new sprite will be partly blacked out.

奢华的一滴泪 2024-11-24 09:49:30

记下您的屏幕名称。 (对于我的情况,它是屏幕。)并且...

screen.fill((0, 0, 0))

将其放在位图传输之前和绘图之后。
(这会形成黑色背景。)
希望这有帮助!

Take the name of your screen. (For my case it's screen.) And do...

screen.fill((0, 0, 0))

Put that before the bliting and after the drawing.
(It would make a black background.)
Hope this helps!

你丑哭了我 2024-11-24 09:49:30

如果您想避免重绘整个屏幕,那么只需修复被精灵“损坏”的区域即可。已经建议在精灵所在的位置绘制一个黑色矩形。如果你的背景始终是全黑的,那就有效。但是,如果有背景图像,则可以仅将背景图像的损坏部分复制回屏幕。

如果背景更加动态,那么另一种解决方案是在绘制精灵之前复制将用精灵覆盖的屏幕部分,并将其存储在某个地方。然后绘制精灵,并根据需要翻转屏幕。在移动精灵之前,将保存的背景部分复制回屏幕,然后再次重复这一切。

If you want to avoid redrawing the whole screen, then just fix the region that was "damaged" by your sprite. Already suggested is to draw a black rectangle over where your sprite was. That works if your background is always completely black. However, if there was a background image, then you can copy just the damaged part of the background image back to the screen.

If the background is more dynamic, then another solution is to copy the part of the screen that you will cover with a sprite before drawing that sprite, and store it somewhere. Then draw the sprite, and flip the screen if necessary. Right before moving the sprite, copy the saved part of the background back to the screen, and then repeat all this over again.

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