Is there any way to "clear" a surface?
Asked Answered
C

12

8

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

Conde answered 28/10, 2009 at 0:27 Comment(5)
Which toolkit are you using? Tk, Qt, Gtk, WxWidgets?Emblements
TkInter I guess, or I don't know really? Im using pygame for all my graphics.Conde
Yes, there certainly is a way.Queenqueena
If it is pygame: why not just fill the surface with a solid color (it's bound to have a fill method)....Forgiving
@Eric: If you're using pygame, then please UPDATE the question to actually say that you're using pygame.Igloo
J
9

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

Jaclin answered 18/6, 2015 at 12:19 Comment(0)
C
5

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.

Curvy answered 4/12, 2009 at 10:30 Comment(0)
H
2

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.

Hydrochloride answered 28/10, 2009 at 0:37 Comment(0)
P
2

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.

Pindaric answered 28/10, 2009 at 13:51 Comment(0)
P
2

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))
Petronel answered 10/7, 2011 at 14:11 Comment(0)
S
2

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

Soapberry answered 9/11, 2014 at 21:36 Comment(0)
C
1

I used the following in a game I made:

self.image = 0 #to empty it
self.image = pygame.image.load("image")
Chadd answered 14/10, 2015 at 8:15 Comment(0)
S
0

For pygame you can use Surface.fill

Superpose answered 28/10, 2009 at 0:40 Comment(2)
Yes I could use it, but here's the tricky part: My surface is being blitted upon another surface, and is using the SRCALPHA flag to support transparency. If I fill the whole surface with a solid color, my whole game board will turn into that color, since this surface is being blitted upon everything.... And I don't know how to solve this problemConde
@Eric, you should add those requirements to your questionSuperpose
S
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))
Scampi answered 18/12, 2020 at 7:14 Comment(0)
E
0

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()
Emotionalism answered 5/10, 2021 at 20:42 Comment(0)
E
0

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!

Eigenfunction answered 8/1, 2023 at 6:39 Comment(0)
H
-1

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

surface.fill
surface.set_alpha(255)
Hatley answered 28/10, 2009 at 2:34 Comment(1)
ah i see what you're trying to do now. No you can't undo a blit on a surface. Follow Kylotan's advice.Hatley

© 2022 - 2024 — McMap. All rights reserved.