Python pygame设置颜色透明度。

huangapple go评论103阅读模式
英文:

python pygame set color transparency

问题

我想在屏幕上创建一个透明按钮和文本,我搜索了如何做这个的方法,第四个RGB参数和 set_alpha 可以使颜色变透明。

所以我使用了 self.button_color=(0,100,100,128) 来设置按钮的颜色,然后使用 self.text.set_alpha(128) 来改变文本的颜色。

但是当我运行脚本时什么都没有改变。

以下是代码:

#!/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()
英文:

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

得分: 4

阅读 pygame.font.Font.render 的文档:

根据所使用的背景类型和抗锯齿设置,此方法返回不同类型的 Surface。出于性能考虑,了解将要使用的图像类型是很重要的。如果背景是透明的,将设置一个 colorkey。抗锯齿图像将呈现为 24 位 RGB 图像。如果背景是透明的,将包含像素 alpha 值。

这意味着,如果 antialias 参数为 True,那么你必须设置一个透明的 background 颜色以生成透明的文本,例如:

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

阅读 pygame.Surface.fill 的文档:

颜色参数可以是 RGB 序列、RGBA 序列或映射的颜色索引。如果使用 RGBA,除非 Surface 使用了每像素 alpha(Surface 带有 SRCALPHA 标志),否则将忽略 Alpha(RGBA 的 A 部分)。

你必须创建一个带有 SRCALPHA 属性的 pygame.Surface 对象来绘制一个透明的矩形:

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

要实现你想要的效果,你必须使用特殊标志 BLEND_MAX 将文本与矩形叠加。draw_button 只需将按钮矩形(包含文本)叠加到屏幕上,例如:

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)
英文:

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:

Python pygame设置颜色透明度。

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)

huangapple
  • 本文由 发表于 2020年1月6日 22:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定