मैं विकल्प बटन कैसे लागू करूं और PyGame में बटन का रंग बदलूं?

Nov 29 2020

Pls सुझाव दें कि जब मैंने इसे दबाया तो मैं बटन का रंग कैसे बदल सकता हूं, और जब मैंने दूसरा बटन दबाया तो पहले बटन का रंग डिफ़ॉल्ट रंग में बदल जाएगा।

उदाहरण के लिए, STRAIGHT बटन पर क्लिक करने के बाद, बटन हरे रंग का हो जाएगा और जब मैं बाएँ बटन पर क्लिक करूँगा तो 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)

जवाब

1 Rabbid76 Nov 29 2020 at 19:01

जब आप बटन खींचते हैं, तो आपको वैश्विक चर पर निर्भर रंग सेट करना होगा 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()बूलियन मूल्यों की एक सूची देता है जो सभी माउस बटन के राज्य ( Trueया False) का प्रतिनिधित्व करते हैं। एक बटन की स्थिति Trueतब तक होती है जब तक बटन दबाया जाता है।
आपको MOUSEBUTTONDOWNईवेंट का उपयोग करना होगा । MOUSEBUTTONDOWNघटना एक बार जब आप माउस बटन पर क्लिक होता है और MOUSEBUTTONUPघटना एक बार जब माउस बटन जारी की है होता है। pygame.event.Event()वस्तु दो विशेषताओं है कि माउस घटना के बारे में जानकारी प्रदान करती हैं। posएक टपल है जो उस स्थिति को संग्रहीत करता है जिसे क्लिक किया गया था। buttonउस बटन को संग्रहीत करता है जिसे क्लिक किया गया था।


यदि आपके पास कई बटन हैं जिन्हें आपको एक-दूसरे के साथ बातचीत करना है, तो एक एकल clickedस्थिति पर्याप्त नहीं है। आपको प्रत्येक बटन के लिए एक अलग "क्लिक" स्थिति की आवश्यकता है। यदि 1 बटन की क्लिक की गई स्थिति बन जाती है True, तो अन्य कुंजियों के राज्यों को सेट करना होगा False। मैं इसके लिए एक 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()
GameDev Nov 29 2020 at 18:15
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()