[英]Pygame Surface layers: Transparent sprite layer issues
I am drawing a small colored square, using surface.fill(color, rect)
, on a sprite surface, which is then be displayed on top of the other surfaces in the following order: 我正在使用精灵表面上的surface.fill(color, rect)
绘制一个小的彩色正方形,然后按以下顺序显示在其他表面的顶部:
Currently I am having a problem where my sprites smear across the screen because I'm not wiping the screen each time. 目前,我有一个问题,我的精灵会在屏幕上涂抹,因为我没有每次都擦拭屏幕。 How can I eliminate this smearing effect, while keeping the sprite layer from covering the other layers? 我如何消除这种拖影效果,同时又使子图形层不能覆盖其他层?
Layers code - Initialized, but not updated each frame. 图层代码-已初始化,但未更新每个帧。
game_surface = pygame.Surface((self.canvas.get_size()))
game_surface = game_surface.convert()
game_surface.fill((0,0,255))
maze_surface = pygame.Surface((self.canvas.get_size()))
maze_surface = maze_surface.convert_alpha()
maze_surface.fill((0,0,255,0))
play_surface = pygame.Surface((self.canvas.get_size()))
play_surface = play_surface.convert_alpha()
play_surface.fill((0,0,0,0))
Presently only the play_surface
actually actually uses transparency, but eventually both the play_surface
and the maze_surface
will need to be transparent. 目前,只有play_surface
实际上实际上使用了透明性,但是最终play_surface
和maze_surface
都将需要透明。
Sprites layer update - Called every time the sprite is moved. Sprites图层更新-每次移动Sprite时调用。
def update(self, canvas):
# move sprite if keys pressed (not shown)
self.surface.fill((0,0,0,0)) # Newest screen fill attempt. Causes smearing effect
self.surface.fill(self.color, self.rect) # draw green square
canvas.blit(self.surface, (0,0))
Smear effect: Red = maze_layer
, Green = smeared sprite 拖影效果:红色= maze_layer
,绿色=涂抹的精灵 Alternate sprite fill - Altered version of the above 备用精灵填充-上面的替代版本
def update(self, canvas):
# move sprite if keys pressed (not shown)
self.surface.fill((0,0,0)) # Original screen fill. Covers up lower layers
self.surface.fill(self.color, self.rect) # draw green square
canvas.blit(self.surface, (0,0))
Non-transparency - Black = filled color (I want to do this without covering up the other layers) 非透明-黑色=填充颜色(我想这样做而不遮盖其他层)
How can I eliminate this smearing effect (pic 1), while keeping the sprite layer from covering up the other layers (pic 2)? 我如何消除这种拖影效果(图1),同时又使子图形层不能覆盖其他层(图2)? Any assistance is very much appreciated. 非常感谢您的协助。
You need to redraw the background covered by your sprites, update their positions, and then redraw the sprites in their new position. 您需要重新绘制精灵覆盖的背景,更新它们的位置,然后将精灵重新绘制到新位置。
You may be interested in exploring the pygame.sprite
module which has built-in support for this functionality. 您可能有兴趣探索pygame.sprite
模块,该模块内置了对此功能的支持。 You can create subclasses of pygame.sprite.Sprite
that: 您可以创建pygame.sprite.Sprite
子类:
update(self)
method (optional) 覆盖update(self)
方法(可选) self.image
and self.rect
attributes that are used to draw them to screen 具有用于将其绘制到屏幕的self.image
和self.rect
属性 pygame.sprite.Group
instances 添加到pygame.sprite.Group
实例 Pygame's sprite group classes (such as Group
, GroupSingle
, LayeredUpdates
, etc.) include methods like pygame.sprite.Group.clear(surface_dest, background)
(see the documentation ) to facilitate the task of re-drawing background images on your sprites before moving and re-drawing them elsewhere. Pygame的sprite组类(例如Group
, GroupSingle
, LayeredUpdates
等)包括pygame.sprite.Group.clear(surface_dest, background)
(请参阅文档 ),以方便在您之前在pygame.sprite.Group.clear(surface_dest, background)
上重新绘制背景图像的任务。将它们移动并重新绘制到其他位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.