python 有没有办法“清除”表面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1634509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to "clear" a surface?
提问by Eric
Is there any way to clear a surface from anything that has been blitted to it?
有什么方法可以清除表面上任何被 blitted 的东西?
回答by Mizipzor
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.
哦,当然,您需要在执行此操作后“翻转”表面以使其在屏幕上可见。
回答by Wolfang Torres
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
如果您想要使 pygame Surface 对象“干净”,即擦除所有图像,在每个游戏循环中将新图像传输到它,并保持对等像素 alpha 而不创建新表面,您可以填充它,但不是纯色,而是使用透明色
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
*对不起,我的英语不好,还在学习中
回答by Thomas Collingwood
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
然后使用 Alpha Blend 标志将此表面 blit 到您的主表面上
screen.blit(mask,(0,0),special_flags=(pygame.BLEND_RGBA_ADD))
回答by Pod
I don't know what API you're using, so here's a vague answer:
我不知道你在使用什么 API,所以这里有一个模糊的答案:
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.
如果您知道如何进行 blit,只需将相同大小的白色四边形 blit 到表面上。
回答by Kylotan
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.
通常在图形中完成的是您对黑板所做的事情 - 清除所有内容,下次只重绘您想要保留的内容。
回答by thegunmaster
.fill((255,255,255,0))
appears to work for me anyway.
.fill((255,255,255,0))
无论如何似乎对我有用。
回答by DrevanTonder
I used the following in a game I made:
我在我制作的游戏中使用了以下内容:
self.image = 0 #to empty it
self.image = pygame.image.load("image")
回答by John La Rooy
For pygame you can use Surface.fill
对于 pygame,您可以使用Surface.fill
回答by Charles Ma
Fill the surface with fill, then make it transparent with set_alpha
用 fill 填充表面,然后用 set_alpha 使其透明
surface.fill
surface.set_alpha(255)