Python 在Pygame中单击按钮后,如何更改按钮的颜色?

Python 在Pygame中单击按钮后,如何更改按钮的颜色?,python,pygame,pygame-surface,Python,Pygame,Pygame Surface,我一直在做这个项目,我创建了一个包含函数和事件的选择屏幕,这样当鼠标悬停在按钮上时,它就会变成橙色。但这是一个选择屏幕,一旦点击一个按钮,我希望它保持高亮显示,以显示该项目已被选中,但我似乎不知道如何做到这一点。这是我的密码: def button2(msg,x,y,w,h,ic,ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x+w >

我一直在做这个项目,我创建了一个包含函数和事件的选择屏幕,这样当鼠标悬停在按钮上时,它就会变成橙色。但这是一个选择屏幕,一旦点击一个按钮,我希望它保持高亮显示,以显示该项目已被选中,但我似乎不知道如何做到这一点。这是我的密码:

def button2(msg,x,y,w,h,ic,ac, action=None):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x,y,w,h))

        if click[0] == 1 and action != None:
            if action == "START":
                game_loop()
            elif action == "BACK":
                game_intro()
                quit()

            elif action == "Playstation 4":
                print("")
    else:
        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

#Selection Screen
def game_select_items_menu():

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    gameSelectItems = False
    while not gameSelectItems:

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

        blank_image(x, y)

        button2("Xbox One", 374, 60, 168, 48, white, orange,"Xbox One") #XBOX ONE - BUTTON (TOP ROW)

        button2("Playstation 4", 200, 60, 168, 48, white, orange,"Playstation 4")#PLAYSTATION 4 - BUTTON (TOP ROW)

        button("Kettle", 30, 60, 163, 48, white, orange)#Kettle - BUTTON (TOP ROW)

        button("Lewi Jeans", 374, 160, 168, 48, white, orange)#LEWI JEANS - BUTTON(SECOND ROW)

        button("MacBook", 200, 160, 168, 48, white, orange)#MACBOOK - BUTTON (SECOND ROW)

        button("Samsung TV", 30, 160, 163, 48, white, orange)#SAMSUNG TV - BUTTON (SECOND ROW)

        button("Nike Air Max", 374, 250, 168, 48, white, orange)#NIKE AIR MAX - BUTTON (THIRD ROW)

        button("Tablet", 200, 250, 168, 48, white, orange)#TABLET - BUTTON (THIRD ROW)

        button("Perfume", 30, 250, 163, 48, white, orange)#PERFUME - BUTTON (THIRD ROW)

        #button("", 30, 340, 300, 150, white, orange)#Print Box

       #Bottom buttons(Start,Back)

        button2("START", 374, 370, 163, 48, green, green_bright, "START")#START - BUTTON (BOTTOM)

        button2("BACK", 374, 430, 163, 48, green, green_bright, "BACK")#BACK - BUTTON (BOTTOM)

        pygame.display.update()

        clock.tick(80) #Setting the fps
更改这些行以更改文本的坐标(它们靠近末尾)


试试看。

你想让按钮在被点击后保持橙色吗?@Jed是的correct@JeD非常感谢你,它确实有效。我知道它很简单,但我就是想不出它的语法。如果你不介意的话,我还需要其他方面的帮助,就这样。非常感谢你,它确实有效。我知道它很简单,但我就是想不出它的语法。如果你不介意的话,我还需要其他方面的帮助。@JustinLeslie Tell me^^^因此,对于我的大学项目,我自己创建了一个包含各种不同项目的选择屏幕。我想让我的选择屏幕做的是,一旦按下按钮(项目),我想让它显示所按下项目的重量、价格,我想让它将每个项目加起来,并显示在我的GUI上的白色框中。我在互联网上找不到任何东西来做这件事,所以如果这真的可能的话,我真的需要一些帮助。(我本来想把这个贴到别的地方)@jedIf我看到了,对吧?你已经显示了所选物品的名称了,对吗?你需要从某处得到价格和重量。添加另一个列表,就像clickedButtons=[]一样,并使其成为全局列表。称它为priceAndWeight=[0,0]或其他什么。当一个按钮被点击时,将价格添加到第一个条目,将重量添加到第二个条目。是的,项目名称在按钮上,但我想发生的是当按下该按钮时。例如,我希望Xbox One按钮在我创建的一个白色框中的GUI底部打印(“重量=120kg,价格为310英镑”),它应该在代码列表的底部附近。我还想让它加起来,这样Xbox one的价格是310英镑,Playstation 4的价格是210英镑。所有这些加起来总共是520英镑,希望你能得到我想要的东西。@jed
global clickedButtons,priceAndWeight
clickedButtons=[]
priceAndWeight=[250, 150]

def button2(msg,x,y,w,h,ic,ac, action=None):
    global clickedButtons
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if click[0] == 1 and action != None:
        if not (msg in clickedButtons):
            # Save that this button has been clicked
            clickedButtons.append(msg)
            # Add price and weight (You need to get price and weight frome somewhere)
            global priceAndWeight
            priceAndWeight[0] = priceAndWeight[0] + price
            priceAndWeight[1] = priceAndWeight[1] + weight
        elif action == "Playstation 4":
            print("")
    else:
        #Check if this button has been clicked
        if (msg in clickedButtons):
            pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        else:
            pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)
textRect.centerx = x
textRect.centery = y
global clickedButtons,priceAndWeight
clickedButtons=[]
priceAndWeight=[250, 150]

def button2(msg,x,y,w,h,ic,ac, action=None):
    global clickedButtons
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if click[0] == 1 and action != None:
        if not (msg in clickedButtons):
            # Save that this button has been clicked
            clickedButtons.append(msg)
            # Add price and weight (You need to get price and weight frome somewhere)
            global priceAndWeight
            priceAndWeight[0] = priceAndWeight[0] + price
            priceAndWeight[1] = priceAndWeight[1] + weight
        elif action == "Playstation 4":
            print("")
    else:
        #Check if this button has been clicked
        if (msg in clickedButtons):
            pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        else:
            pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
    smallText = pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)