Is there any way to “clear” a surface?

两盒软妹~` 提交于 2019-12-05 06:10:42

When I dont care about performance, I use:

mysurface.fill((0,0,0))

Which will draw the specified color (black in this case) across the entire surface. Is that what you meant by "clear"?

Oh, and you need, of course, to "flip" the surface after doing this for it to be visible on the screen.

I don't know what API you're using, so here's a vague answer:

In virtually all cases "clearing" a surface simply blits a coloured quad of the same size as the surface onto it. The colour used is whatever you want your clear colour to be.

If you know how do blit, just blit a white quad of the same size onto the surface.

You can't undo one graphic written over the top of another graphic any more than you can undo one chalk illustration drawn over the top of another chalk illustration on the same board.

What is typically done in graphics is what you'd do with the chalkboard - clear the whole lot, and next time only redraw what you want to keep.

I had this problem too

To create the surface:

mask=pygame.Surface((180,100),pygame.SRCALPHA)

To clear the surface:

mask.fill
mask.set_alpha(255)

Draw your lines/images etc

Then blit this surface onto your main surface using the Alpha Blend flag

screen.blit(mask,(0,0),special_flags=(pygame.BLEND_RGBA_ADD))

If what you want is to make a pygame Surface object "clean", that is erase all images blited to it, to blit new images to it every game loop and keep the peer pixel alpha without creating a new surface, you can fill it, but instead of a solid color, use a transparent color

from pygame import Color, Surface
empty = Color(0,0,0,0) #The last 0 indicates 0 alpha, a transparent color
field = Surface((width, height), flags=SRCALPHA)

#Inside the game loop
field.fill(empty)

*Sorry is my english is bad, still learning

thegunmaster

.fill((255,255,255,0)) appears to work for me anyway.

For pygame you can use Surface.fill

Fill the surface with fill, then make it transparent with set_alpha

surface.fill
surface.set_alpha(255)

I used the following in a game I made:

self.image = 0 #to empty it
self.image = pygame.image.load("image")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!