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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常您会这样做:
但是,您可以只更新屏幕的脏部分。请参阅
pygame.display.update()
:最好将其与
RenderUpdates
,这是一个
Sprite.Group
:Normally you will do:
But, you can update just dirty portions of the screen. See
pygame.display.update()
:It's best to use it in combination with
RenderUpdates
, which is aSprite.Group
:只需有一个黑色矩形,然后将其覆盖在前一帧上精灵所在位置的上方,就可以将其删除。请记住在块传输精灵之前执行此操作,否则新精灵将部分变黑。
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.
记下您的屏幕名称。 (对于我的情况,它是屏幕。)并且...
将其放在位图传输之前和绘图之后。
(这会形成黑色背景。)
希望这有帮助!
Take the name of your screen. (For my case it's screen.) And do...
Put that before the bliting and after the drawing.
(It would make a black background.)
Hope this helps!
如果您想避免重绘整个屏幕,那么只需修复被精灵“损坏”的区域即可。已经建议在精灵所在的位置绘制一个黑色矩形。如果你的背景始终是全黑的,那就有效。但是,如果有背景图像,则可以仅将背景图像的损坏部分复制回屏幕。
如果背景更加动态,那么另一种解决方案是在绘制精灵之前复制将用精灵覆盖的屏幕部分,并将其存储在某个地方。然后绘制精灵,并根据需要翻转屏幕。在移动精灵之前,将保存的背景部分复制回屏幕,然后再次重复这一切。
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.