Pygame 如何修复“尾随像素"? [英] Pygame how to fix 'trailing pixels'?

查看:17
本文介绍了Pygame 如何修复“尾随像素"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

通常你会这样做:

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

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

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

pygame.display.update()
更新软件显示的屏幕部分
<代码>更新(矩形=无)->无
update(rectangle_list) ->无

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

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.

最好结合RenderUpdates,这是一个Sprite.Group:

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

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.

这篇关于Pygame 如何修复“尾随像素"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆