一种从具有工作 alpha 和颜色的列表中 blit 多个按钮的方法?

A way to blit multiple buttons from a list with working alpha and color?

我想根据列表给出的数字来 blit 几个按钮选择,但是将它们放在 for 循环中会使 alpha() 和 fill() 函数停止工作。有没有办法解决这个问题,或者有更好的替代方法来对多个按钮进行编码?

起始代码:

import pygame, sys
pygame.init()

WIDTH, HEIGHT = 900, 600
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock() 
font = pygame.font.Font('freesansbold.ttf', 32) 

按钮和场景class:

class Button():
    def __init__(self, text, x, y):
        self.rect = pygame.Rect(x, y, 0, 0)
        self.updateText(text)
        self.clicked = False  

    def updateText(self, text):
        self.text = text   
        self.render = font.render(self.text, True, 'white')
        self.text_width = self.render.get_width()
        self.text_height = self.render.get_height()
        self.box = pygame.Surface((self.text_width, self.text_height)) 
        self.rect = self.render.get_rect(topleft = self.rect.topleft)

    def draw(self):
        action = False
        screen.blit(self.box, (self.rect.x, self.rect.y))
        screen.blit(self.render, (self.rect.x, self.rect.y))   

        pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(pos):
            self.box.set_alpha(100) 
            self.box.fill((255, 255, 255))

            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

            if pygame.mouse.get_pressed()[0] == 0:
                action = False
                self.clicked = False
        else:
            self.box.set_alpha(0) 

        return action


class Scene():
    def __init__(self):
        pass

    def on_start(self):
        self.count = 0
        self.blitcount = 0
        self.optionList = []

        for button in range(5): 
            self.optionList.append(Button("button", WIDTH/3*2, 60 *(button + 1)))
            self.count += 1

        self.altButton = Button("Button without for loop", 100, 100)
        self.buttons = None

    def update(self, events):
        screen.fill('gray')

        for i in range(4):

            self.buttons = self.optionList[self.blitcount]
            self.buttons.updateText(str(i))        

            if self.buttons.draw():
                 print(i)

            self.blitcount += 1

        if self.altButton.draw():
            print("Alt")

        self.blitcount = 0

        return self

其余代码:

game = Scene()
game.on_start()

while True:
    clock.tick(30)

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit() 
            sys.exit() 

    game.update(events)

    pygame.display.update()

您必须更改顺序。在绘制方框之前,您需要设置方框的 Alpha 通道 。请注意,框是使用当前设置的 alpha 通道绘制的。
(另请参阅您上一个问题的答案

class Button():
    # [...]

    def draw(self):
        action = False
        
        pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(pos):
            self.box.set_alpha(100) 
            self.box.fill((255, 255, 255))

            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

            if pygame.mouse.get_pressed()[0] == 0:
                action = False
                self.clicked = False
        else:
            self.box.set_alpha(0) 

        screen.blit(self.box, (self.rect.x, self.rect.y))
        screen.blit(self.render, (self.rect.x, self.rect.y))  

        return action