创建一个透明的非活动按钮 - pygame

时间:2016-01-15 12:03:11

标签: python pygame

我正在尝试为游戏菜单创建一个按钮,在所述按钮的顶部使用了一个图像,我希望它是透明的,因为背景应该通过图像的背面看到(png with透明背景)。反正我是否在pygame中创建了一个透明无效状态的按钮?

编辑:例如透明色的颜色代码

1 个答案:

答案 0 :(得分:0)

我发现在处于非活动状态时删除按钮(矩形)的draw行会解决此问题。

原始代码:

def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
    pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
    if click[0] == 1 and action != None:
        print("reached")
        if action == "quit":
            pygame.quit()
            quit()
        elif action == "instructions":
            print("instructions")
        elif action == "credits":
            print("credits")
        elif  action == "warrior":
            print("warrior")
        elif action == "archer":
            print("archer")
        elif action == "mage":
            print("mage")
else:
    pygame.draw.rect(gameDisplay, white, (x,y,width,height)) 

正确的代码:

def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
    pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
    if click[0] == 1 and action != None:
        print("reached")
        if action == "quit":
            pygame.quit()
            quit()
        elif action == "instructions":
            print("instructions")
        elif action == "credits":
            print("credits")
        elif  action == "warrior":
            print("warrior")
        elif action == "archer":
            print("archer")
        elif action == "mage":
            print("mage")
else:
    pass

请注意,else最后甚至不需要,可以删除。我只是把它留给了我改变代码的地方。