Warm tip: This article is reproduced from stackoverflow.com, please click
python python-3.x pygame

Making a Semi-Transparent Pause Background screen in Pygame

发布于 2020-03-29 12:47:24

I was wondering if anyone out there would be able to help me with my problem. Currently I want to display a pause screen everytime a certain key is pressed, which it does however the resulting screen is always either fully transparent or non-transparent and was wondering if there was any way that I would be able to adjust the following code in order to make that dream a reality.

Here's where the Pause Screen is called:

if event.key == pygame.K_p:

                    notPaused = False
                    #print("Obtained")
                    pause = Pause(self.pauseScreen)
                    while notPaused == False:
                        #print("Received")

                        notPaused = pause.processEvents()
                        print(str(notPaused))
                        pause.displayFrame(self.pauseScreen)

                        clock = pygame.time.Clock()

                        clock.tick(60)

And here's how the pause screen displays itself:

screen.fill(constants.BLACK)
        font = pygame.font.SysFont("serif", 25)

        for counter in range(1,5):

            text = font.render(self.options[counter-1], True, constants.WHITE)
            center_x = 150
            center_y = (counter * 120) - (text.get_height() // 2) + (self.pointer.image.get_height() // 2)
            screen.blit(text, [center_x, center_y])

        self.active_sprite_list.draw(screen)

        pygame.display.flip()

And for anyone wondering I sometimes try to sub out BLACK for ABLACK using the RGBA values of: (0,0,0,125)

Finally this is where the screen is initialized for the pause screen:

self.size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT]
self.pauseScreen = pygame.Surface(self.size,pygame.SRCALPHA,32)

Any and all help is appreciated.

Questioner
Kitso
Viewed
51
Hoog 2020-01-31 22:34

You should build up your pause screen as a separate surface. This command: screen.fill(constants.BLACK) all but ensures you will never get 50% transparency (since everything that was on your screen before has been painted over with black).

Build a new surface for your pause screen, fill it black, add your text; then use something like my_pause_surface.set_alpha(128) to make that whole surface 50% transparent, then blit it on top of the rest of your game.

Here is some useful information in another question: Draw a transparent rectangle in pygame