Home > Software design >  I can click around my button and it still activates?
I can click around my button and it still activates?

Time:03-20

I've made 2 buttons and made them send a message "START" or "EXIT" when I click them but the range that I can click them is greater than the actual button, so I can click around the button and it still works. It also, sends both messages when clicking near both of them. I've tried changing the picture, but that it still does the same thing. I don't know what I should do? I am new so pls help.

This is my main:

import pygame
import button

window_width, window_height = 1000, 650

win = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("My game!")

grey = (25, 25, 25)

FPS = 60

start_img = pygame.image.load("img/start_button.png")
exit_img = pygame.image.load("img/exit_button.png")

start_button = button.Button(250, 220, start_img, 1.2)
exit_button = button.Button(265, 350, exit_img, 1.2)

def draw_window():
    win.fill(grey)

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        draw_window()
        if start_button.draw_button(win):
            print("START")
        if exit_button.draw_button(win):
            print("EXIT")
        pygame.display.flip()

    pygame.quit()


if __name__ == "__main__":
main()

and this is my button:

import pygame

class Button:
    def __init__(self, x, y, image, scale):
    width = image.get_width()
    height = image.get_height()
    self.image = pygame.transform.scale(image, ((width * scale), (height * scale)))
    self.rect = self.image.get_rect()
    self.rect.topleft = (x, y)
    self.clicked = False

def draw_button(self, surface):
    action = False

pos = pygame.mouse.get_pos()

if self.rect.collidepoint(pos):
    if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
    self.clicked = True
    action = True

if pygame.mouse.get_pressed()[0] == 0:
        self.clicked = False 


surface.blit(self.image, (self.rect.x, self.rect.y))
return action

I don't know what it could be pls give any suggestion, help is appreciated.

CodePudding user response:

There is no problem with your code, I tested it. The problem is with the button images. Likely your images have a large transparent area.You can compensate for this by creating a smaller hit rectangle using get_bounding_rect().
See also How to get the correct dimensions for a pygame rectangle created from an image

import pygame

class Button:
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, ((width * scale), (height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False
        self.hit_rect = self.image.get_bounding_rect()
        self.hit_rect.x  = x
        self.hit_rect.y  = y

    def draw_button(self, surface):
        action = False
        pos = pygame.mouse.get_pos()

        if self.hit_rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False 


        surface.blit(self.image, (self.rect.x, self.rect.y))
        return action
  • Related