4

I want to create an tranparent button and text on the screen, i search for the way to do this, the fourth RGB parameter and set_alpha can transparent the color

So i use self.button_color=(0,100,100,128) to set the button and self.text.set_alpha(128) to change the color on text

But nothing change when i run the scripts

Here's the code:

#!/usr/bin/python
import sys,os
import pygame


class Setting():
    def __init__(self,width,height):
        self.w=width
        self.h=height
        self.flag=pygame.RESIZABLE
        self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
        self.screen_rect=self.screen.get_rect()
        pygame.display.set_caption("Test")

class Button():
    def __init__(self,setting,text):            
        self.width,self.height = 400,100

        self.button_color=(0,100,100,128)

        self.text_color=(255,0,0)
        self.text = pygame.font.Font(None,100).render(text,True,self.text_color)
        self.text.set_alpha(128)
        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = setting.screen_rect.center
        self.text_rect = self.text.get_rect()
        self.text_rect.center = self.rect.center
    def draw_button(self,setting):
        setting.screen.fill(self.button_color,self.rect)
        setting.screen.blit(self.text,self.text_rect)


def game():
    pygame.init()
    setting=Setting(1200,800)
    button=Button(setting,'PLAY')
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        setting.screen.fill((0,0,0))
        button.draw_button(setting)
        pygame.display.flip()
game()

1 Answer 1

4

Read the documentation of pygame.font.Font.render:

[...] Depending on the type of background and antialiasing used, this returns different types of Surfaces. For performance reasons, it is good to know what type of image will be used. [...] If the background is transparent a colorkey will be set. Antialiased images are rendered to 24-bit RGB images. If the background is transparent a pixel alpha will be included.

That means, if the antialias argument is True, then you have to set a transparent background color to generate a transparent text. e.g:

self.button_color=(0,100,100,128) # transparent alpha=128
self.text_color=(255,0,0)
self.text = pygame.font.Font(None,100).render(text,True,self.text_color, self.button_color)

Read the documentation of pygame.Surface.fill:

[...] The color argument can be either a RGB sequence, a RGBA sequence or a mapped color index. If using RGBA, the Alpha (A part of RGBA) is ignored unless the surface uses per pixel alpha (Surface has the SRCALPHA flag).

You have to create a pygame.Surface object with attribute SCRALPHA to draw a transparent rectangle:

rectsurf = pygame.Surface(self.rect.size,pygame.SRCALPHA)
rectsurf.fill(self.button_color)
setting.screen.blit(rectsurf,self.rect.topleft)

To achieve what you want, you have to blit the text on the rectangle, by using the special flag BLEND_MAX. draw_button just have to blit, the button rectangle, which contains the text, on the screen. e.g:

class Button():
    def __init__(self,setting,text):
        self.width,self.height = 400,100

        self.button_color=(0,100,100,128)
        self.text_color=(255,0,0,128)
        self.text = pygame.font.Font(None,100).render(text,True,self.text_color, self.button_color)

        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.text_rect = self.text.get_rect()
        self.text_rect.center = self.rect.center

        self.btnsurf = pygame.Surface(self.rect.size,pygame.SRCALPHA)
        self.btnsurf.fill(self.button_color)
        self.btnsurf.blit(self.text, self.text_rect, special_flags=pygame.BLEND_MAX)

        self.rect.center = setting.screen_rect.center

    def draw_button(self,setting):
        setting.screen.blit(self.btnsurf,self.rect)
Sign up to request clarification or add additional context in comments.

7 Comments

i found whatever i set self.text.set_alpha(128) or self.text.set_alpha(255), it blit the same color text , is that the text color controlled by the alpha of self.button_color=(0,100,100,128)???
@M_Sea Sorry my bad. self.text.set_alpha(128) is superfluous, because the surface has per pixel alpha. I that case set_alpha has no meaning.
if i want to set the different transparent for button and text, how can i did it?
@M_Sea That doesn't seem to be possible, with ease. I have no idea, currently. Anyway, that is an extension to your question.
Thanks a lot, i found if i set False to True as .render(text,False,self.text_color then it can directly use self.text.set_alpha(200)to set the transparent, only one thing not good is the text not beautiful
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.