有什么办法可以“清除”吗?一个表面?

发布于 2024-08-09 07:29:31 字数 32 浏览 5 评论 0原文

有什么方法可以清除表面上的任何被位块损坏的东西吗?

Is there any way to clear a surface from anything that has been blitted to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

原来分手还会想你 2024-08-16 07:29:31

如果你想要的是使 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)

*抱歉,我的英语不好,还在学习中

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

狼亦尘 2024-08-16 07:29:31

当我不关心性能时,我使用:

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

它将在整个表面上绘制指定的颜色(在本例中为黑色)。这就是你所说的“清晰”吗?

哦,当然,您需要在执行此操作后“翻转”表面,使其在屏幕上可见。

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.

生生不灭 2024-08-16 07:29:31

我不知道你正在使用什么API,所以这里有一个模糊的答案:

在几乎所有情况下,“清除”表面只是将与表面大小相同的彩色四边形传输到其上。使用的颜色是您想要的透明颜色。

如果您知道如何进行 blit,只需将一个相同大小的白色四边形 blit 到表面上即可。

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.

本宫微胖 2024-08-16 07:29:31

您无法撤消写在另一图形顶部的一个图形,就像您无法撤消在同一板上另一粉笔插图顶部绘制的粉笔插图一样。

在图形中通常所做的事情就像你在黑板上所做的那样——清除全部内容,下次只重画你想要保留的部分。

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.

一绘本一梦想 2024-08-16 07:29:31

我也遇到了这个问题

要创建表面:

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

要清除表面:

mask.fill
mask.set_alpha(255)

绘制线条/图像等

然后使用 Alpha Blend 标志将该表面传输到主表面上

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

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))
蔚蓝源自深海 2024-08-16 07:29:31

无论如何, .fill((255,255,255,0)) 似乎对我有用。

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

痴者 2024-08-16 07:29:31

我在自己制作的游戏中使用了以下内容:

self.image = 0 #to empty it
self.image = pygame.image.load("image")

I used the following in a game I made:

self.image = 0 #to empty it
self.image = pygame.image.load("image")
神魇的王 2024-08-16 07:29:31

对于 pygame,您可以使用 Surface.fill

For pygame you can use Surface.fill

许一世地老天荒 2024-08-16 07:29:31

如果要在位图传输之前清除表面,可以首先用不太可能随机的颜色填充该表面,用该颜色设置 alpha(颜色键),然后将具有透明背景的原始精灵位图传输回表面。

idle_image = pg.image.load("sprite.png") #sprite with transparent background
temp_color = pg.Color(132, 23, 213, 255) #random unlikely replacement color

def clear_surface(surface):
    surface.fill(st.temp_color)
    surface.set_colorkey(st.temp_color)
    surface.blit(idle_image, (0,0))

If you want to clear a surface before blitting, you can first fill the surface with a unlikely random color, set the alpha (colorkey) with that color, and then blit the original sprite with the transparent background back onto the surface.

idle_image = pg.image.load("sprite.png") #sprite with transparent background
temp_color = pg.Color(132, 23, 213, 255) #random unlikely replacement color

def clear_surface(surface):
    surface.fill(st.temp_color)
    surface.set_colorkey(st.temp_color)
    surface.blit(idle_image, (0,0))
灵芸 2024-08-16 07:29:31

您可以通过将表面设为透明来清理表面。
下面是我如何使用空颜色 RGB 通过将表面设置为透明版本来使文本闪烁。

from __future__ import absolute_import, division, print_function
from itertools import cycle
import pygame

VISITOR_TTF_FILENAME = 'pixelFont.ttf'
BLINK_EVENT = pygame.USEREVENT + 0
empty = (255,255,255,0)


def main():
    pygame.init()
    try:
        screen = pygame.display.set_mode((800, 600))
        screen.fill((255,255,255))
        screen_rect = screen.get_rect()

        clock = pygame.time.Clock()

        font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
        on_text_surface = font.render(
            'Press Any Key To Start', True, pygame.Color('green3')
        )
        blink_rect = on_text_surface.get_rect()
        blink_rect.center = screen_rect.center
        off_text_surface = pygame.Surface(blink_rect.size)
        off_text_surface.fill(empty)
        
        blink_surfaces = cycle([on_text_surface, off_text_surface])
        blink_surface = next(blink_surfaces)
        pygame.time.set_timer(BLINK_EVENT, 1000)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == BLINK_EVENT:
                    blink_surface = next(blink_surfaces)

            screen.blit(blink_surface, blink_rect)
            pygame.display.update()
            clock.tick(60)
    finally:
        pygame.quit()


if __name__ == '__main__':
    main()

You can clear a surface by making it transparent.
Below is how I used the empty color RGB to make text blink by setting the surface to switch to a transparent version.

from __future__ import absolute_import, division, print_function
from itertools import cycle
import pygame

VISITOR_TTF_FILENAME = 'pixelFont.ttf'
BLINK_EVENT = pygame.USEREVENT + 0
empty = (255,255,255,0)


def main():
    pygame.init()
    try:
        screen = pygame.display.set_mode((800, 600))
        screen.fill((255,255,255))
        screen_rect = screen.get_rect()

        clock = pygame.time.Clock()

        font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
        on_text_surface = font.render(
            'Press Any Key To Start', True, pygame.Color('green3')
        )
        blink_rect = on_text_surface.get_rect()
        blink_rect.center = screen_rect.center
        off_text_surface = pygame.Surface(blink_rect.size)
        off_text_surface.fill(empty)
        
        blink_surfaces = cycle([on_text_surface, off_text_surface])
        blink_surface = next(blink_surfaces)
        pygame.time.set_timer(BLINK_EVENT, 1000)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == BLINK_EVENT:
                    blink_surface = next(blink_surfaces)

            screen.blit(blink_surface, blink_rect)
            pygame.display.update()
            clock.tick(60)
    finally:
        pygame.quit()


if __name__ == '__main__':
    main()
疏忽 2024-08-16 07:29:31

当我想摆脱

screen.fill("Gray")

每次更新时都会使用的东西时!所以它把它隐藏在灰屏后面。

灰色不是唯一可用的颜色,因此这里列出了您可以使用的所有可用颜色可以用于 pygame

希望它有帮助!

When I want to get rid of something I use

screen.fill("Gray")

every time I update! So it hides it behind the gray screen.

Gray is not the only color available, so here is a list of all of the available colors you can use for pygame

Hope it helped!

夜巴黎 2024-08-16 07:29:31

用 fill 填充表面,然后用 set_alpha 使其透明

surface.fill
surface.set_alpha(255)

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

surface.fill
surface.set_alpha(255)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文