-1

Something happening here as it does not print HOVERING when the button is over the self.rect? The pos prints fine, but nothing showing and no response to clicks.

class Button():#======================================================================================
def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    self.clicked = False

def draw(self):
    action = False
    #get mouse position
    pos = pygame.mouse.get_pos()
    print(pos)
    #check mouse over and clicked conditions
    if self.rect.collidepoint(pos):
        print('HOVERING')
        if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
            print('CLICKED')
            action = True
            self.clicked = True
    if pygame.mouse.get_pressed()[0] == 0:
        self.clicked = False
    #draw button
    screen.blit(self.image,self.rect)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Matt
  • 45
  • 7
  • Do you have multiple `Button` objects? Do you call the correct `Button` instance? – Rabbid76 Dec 20 '21 at 15:11
  • Yes `start_button = Button(screen_width/2-48,screen_height/2 -32,start_img) exit_button = Button(screen_width/2-48,screen_height/2 +16,exit_img)` – Matt Dec 20 '21 at 21:09

0 Answers0