0

so I am trying to make a class to make buttons and handle clicking them on the go everything was okay until... I had 2 alternating buttons when one is clicked it disappears and the other show up and
the problem is if 2 buttons are in the same position they click at the same time even though the if statement is False

I tired different ways, the last thing I tried is putting buttons in a list and refereeing to it as an argument, but still once I click "Create Deck" it clicks "Save Deck" also only if they are at the same position

the handle buttons Class:

class  Handle_buttons:
        
    Done = False
    Button = None
    
    def __init__(self,Button_H, Button_W, by, bx):
        self.Button= pygame.Rect(bx, by, Button_W, Button_H)

        
    def create_button(self, events, Button_H, Button_W, by, bx, Place, Color, button_text, font, txtColor,Button_Rect, action= None):
        pygame.draw.rect(Place, Color, self.Button)
        text.txt_getter.draw_txt(button_text, font, txtColor, Place, self.Button.center[0]-text.txt_getter.get_txtW(button_text,font)//2, self.Button.center[1]-text.txt_getter.get_txtH(button_text,font)//2)
        posx,posy = pygame.mouse.get_pos()
        text.txt_getter.draw_txt(str(self.Button), font2, BLACK, cards.DECK_WIN, 400,400)
        for event in events:
            if event.type==pygame.MOUSEBUTTONDOWN and event.button==1 and self.Button.collidepoint(posx, posy) and action!= None:
                action()
                
            elif event.type==pygame.MOUSEBUTTONDOWN and event.button==1 and self.Button.collidepoint(posx, posy):
                posx, posy= pygame.mouse.get_pos()
                self.Done = True
                return True

so I create them:

Create_Deck = Handle_buttons(50, 120, 500, 30)
Save_Deck = Handle_buttons(50, 120, 500, 30)

in my loop:

  while True:
        clock.tick(FPS)
        cards.DECK_WIN.fill(Light_Yellow)
        events = pygame.event.get()

        #if not creating show "Create Deck" and when handle buttons return True(clicked) creating becomes True 

        if not card1.creating:
            if Create_Deck.create_button(events, 50, 200, 30, 500, cards.DECK_WIN, GOLD, "Create Deck", font2, BLACK, 1):
                card1.deck_name= "deck"+str(card1.Decks_Num)
                card1.deck_name+= ".txt"
                cards.Deck_File= open(card1.deck_name, 'w')
                print("Created")
                #self.Decks_Num+=1
                card1.creating = True
                #check == True

        if card1.creating:
            if Save_Deck.create_button(events, 50, 200, 30, 700, cards.DECK_WIN, GOLD, "Save Deck0", font2, BLACK, 2):
                card1.creating = False
            DList_Border = pygame.Rect(SCREEN_WIDTH-238, 0, 230, SCREEN_HIGHT)
            pygame.draw.rect(cards.DECK_WIN, BLACK, DList_Border)
            DList_Rect = pygame.Rect(SCREEN_WIDTH-230, 0, 230, SCREEN_HIGHT)
            pygame.draw.rect(cards.DECK_WIN, GRAY, DList_Rect)
            text.txt_getter.draw_txt(str(card1.creating),font1, BLACK, cards.DECK_WIN, 500,500)
    
                    

0 Answers0