【问题标题】:How do I implement option buttons and change the button color in PyGame?如何在 PyGame 中实现选项按钮并更改按钮颜色?
【发布时间】:2021-03-11 12:44:30
【问题描述】:

请建议我如何在按下按钮时更改按钮的颜色,当我按下第二个按钮时,第一个按钮的颜色将更改为默认颜色。

例如,当我单击 STRAIGHT 按钮后,按钮将变为绿色,当我单击 LEFT 按钮时,LEFT 按钮将变为绿色,而 STRAIGHT 按钮将变为默认颜色,即白色。在此先感谢:)

代码:

def draw_button(self):

    global clicked
    action = False

    # get mouse position
    pos = pygame.mouse.get_pos()

    # create pygame Rect object for the button
    button_rect = Rect(self.x, self.y, self.width, self.height)

    # check mouseover and clicked conditions
    if button_rect.collidepoint(pos):
        if pygame.mouse.get_pressed()[0] == 1:
            clicked = True
            pygame.draw.rect(screen, self.click_col, button_rect)
        elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
            clicked = False
            action = True
           
        else:
            pygame.draw.rect(screen, self.hover_col, button_rect)
    else:
        pygame.draw.rect(screen, self.button_col, button_rect)

【问题讨论】:

标签: python pygame


【解决方案1】:

绘制按钮时,需要根据全局变量clicked设置颜色:

def draw_button(self):

    global clicked
    
    # get mouse position
    pos = pygame.mouse.get_pos()

    # create pygame Rect object for the button
    button_rect = Rect(self.x, self.y, self.width, self.height)

    # check mouseover and clicked conditions
    hover = button_rect.collidepoint(pos)
    if hover and pygame.mouse.get_pressed()[0] == 1:
        clicked = not clicked

    color = self.button_col
    if clicked:
        color = self.click_col
    elif hover:
        color = self.hover_col

    pygame.draw.rect(screen, color, button_rect)

无论如何,这不会让你满意,因为pygame.mouse.get_pressed() 返回一个布尔值列表,代表所有鼠标按钮的状态(TrueFalse)。只要按下按钮,按钮的状态就是True
您必须使用MOUSEBUTTONDOWN 事件。 MOUSEBUTTONDOWN 事件在您单击鼠标按钮时发生一次,MOUSEBUTTONUP 事件在释放鼠标按钮时发生一次。 pygame.event.Event() 对象有两个属性提供有关鼠标事件的信息。 pos 是一个存储被点击位置的元组。 button 存储被点击的按钮。


如果您有多个必须相互交互的按钮,则单个clicked 状态是不够的。每个按钮都需要一个单独的“单击”状态。如果1个按钮的点击状态变为True,则其他按键的状态必须设置为False。我建议为此实现一个RadioButton 类。

另请参阅 MouseSprite

最小的例子: repl.it/@Rabbid76/PyGame-RadioButton

import pygame

class RadioButton(pygame.sprite.Sprite):
    def __init__(self, x, y, w, h, font, text):
        super().__init__() 
        text_surf = font.render(text, True, (0, 0, 0))
        self.button_image = pygame.Surface((w, h))
        self.button_image.fill((96, 96, 96))
        self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
        self.hover_image = pygame.Surface((w, h))
        self.hover_image.fill((96, 96, 96))
        self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
        pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
        self.clicked_image = pygame.Surface((w, h))
        self.clicked_image.fill((96, 196, 96))
        self.clicked_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
        self.image = self.button_image
        self.rect = pygame.Rect(x, y, w, h)
        self.clicked = False
        self.buttons = None

    def setRadioButtons(self, buttons):
        self.buttons = buttons

    def update(self, event_list):
        hover = self.rect.collidepoint(pygame.mouse.get_pos())
        for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN:
                if hover and event.button == 1:
                    for rb in self.buttons:
                        rb.clicked = False
                    self.clicked = True
        
        self.image = self.button_image
        if self.clicked:
            self.image = self.clicked_image
        elif hover:
            self.image = self.hover_image


pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)

radioButtons = [
    RadioButton(50, 40, 200, 60, font50, "option 1"),
    RadioButton(50, 120, 200, 60, font50, "option 2"),
    RadioButton(50, 200, 200, 60, font50, "option 3")
]
for rb in radioButtons:
    rb.setRadioButtons(radioButtons)
radioButtons[0].clicked = True

group = pygame.sprite.Group(radioButtons)

run = True
while run:
    clock.tick(60)
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False 

    group.update(event_list)

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

【讨论】:

    【解决方案2】:
    your_button_rect = pygame.Rect(x,y,width,height)
    color = (0,0,0)
    pos = pygame.mouse.get_pos()
    def change_button_color():
        if your_button_rect.colliderect(pos):
            if event.type == pygame.MOUSEBUTTONDOWN: #checks if mouse button is pressed down while the cursor is on the rectangle
                color = (your_desired_Color_RGBvalue)
    
    while True:
        pygame.draw.rect(screen,color,your_button_rect)
        change_button_color()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2019-10-13
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多