【问题标题】:How do I change the color of a button once it is clicked in Pygame?在 Pygame 中单击按钮后如何更改按钮的颜色?
【发布时间】:2020-12-02 04:20:04
【问题描述】:

我一直在做这个项目,我创建了一个带有函数和事件的选择屏幕,这样当鼠标悬停在按钮上时它会变成橙色。但它是一个选择屏幕,一旦单击按钮,我希望它保持突出显示以显示该项目已被选中,但我似乎无法弄清楚如何做到这一点。这是我的代码:

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 是的,这是正确的
  • @JeD 非常感谢它确实有效。我知道这很简单,但我就是想不出它的语法。如果您不介意的话,我还需要其他方面的帮助,那就这样吧。

标签: python pygame pygame-surface


【解决方案1】:
global clickedButtons,sumOfCosts,textDisp
clickedButtons=[]
sumOfCosts=[0,0]
textDisp = None

def addCostsAndDisplay(msg,cost,weight):
    global sumOfCosts
    sumOfCosts[0]+=cost
    sumOfCosts[1]+=weight

    basicfont = pygame.font.SysFont(None, 48)
    text = basicfont.render('adding '+str(msg)+' adds up to '+str(sumOfCosts[0])+' and weighs '+str(sumOfCosts[1]), True, (255, 0, 0), (255, 255, 255))

    return text

def button2(msg,x,y,w,h,ic,ac,cost=1,weight=1, action=None):
    global clickedButtons,textDisp
    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 not (msg in clickedButtons):
                #Save that this button has been clicked
                clickedButtons.append(msg)
                textDisp=addCostsAndDisplay(msg,cost,weight)
            if action == "START":
                game_loop()
            elif action == "BACK":
                game_intro()
                quit()

            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)

#Selection Screen
def game_select_items_menu():
    global textDisp
    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)
        if (not textDisp is None):

            screen=pygame.display.get_surface()
            textRect=textDisp.get_rect()
            textRect.centerx = x
            textRect.centery = y
            screen.blit(textDisp,textRect)

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

        button2("Playstation 4", 200, 60, 168, 48, white, orange,20,20"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,0,0  "START")#START - BUTTON (BOTTOM)

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

        pygame.display.update()

        clock.tick(80) #Setting the fps

更改这些行以更改文本的坐标(它们接近结尾)

textRect.centerx = x
textRect.centery = y

试试看。

【讨论】:

  • 非常感谢它确实有效。我知道这很简单,但我就是想不出它的语法。如果您不介意,我还需要其他方面的帮助,那就这样吧。
  • @JustinLeslie 告诉我^^
  • 所以对于我的大学项目,我自己创建了一个包含各种不同项目的选择屏幕。我希望我的选择屏幕做的是,一旦按下按钮(项目),我希望它显示按下的项目的重量、价格,我希望它把它们中的每一个加起来,并在我的 GUI 上的一个白框中显示.我无法在互联网上找到任何可以做到这一点的东西,所以如果这真的可能的话,我真的需要一些帮助。 (我打算在其他地方发布这个)@jed
  • 如果我没看错,您已经显示了所选项目的名称,对吧?您需要从某个地方获取价格和重量。添加另一个列表,就像 clickedButtons=[] 并使其成为全局。称它为 priceAndWeight=[0,0] 之类的。单击按钮时,将价格添加到第一个条目,将重量添加到第二个条目。
  • 是的,项目名称在按钮上,但我想要发生的是当按下该按钮时。例如,我希望它在我创建的白框中的 GUI 底部打印(“重量 = 120 公斤,价格 310 英镑”)的 Xbox One 按钮,这应该靠近代码中列表的底部。我还希望它加起来,所以 Xbox 是“310 英镑”,而 Playstation 4 是 210 英镑。所有这些加起来是 520 英镑,希望你能在这里得到我想要尝试和实现的目标。@jed
【解决方案2】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2021-03-18
    • 2018-08-01
    相关资源
    最近更新 更多