rishi
rishi

Reputation: 640

Why is my pygame.MOUSEBUTTONDOWN not working?

I'm creating a class for pygame which will allow users to create textboxes for their game. My code doesn't reach the mousebuttondown part for some reason though. I'm attaching my whole code along with parts that I'm facing issues with.

it doesn't print done

def main(self, events, mousepos, id):
    for event in events:
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect(id, mousepos):
                print("done")

keeps printing no

def rect(self, text_id, mousepos):
    x, y, width, height = self.dict_all[text_id]
    if ((x + width) > mousepos[0] > x) and ((y + height) > mousepos[1] > y):
        print("yes")
        return True
    else:
        print("no")
        return False

whole code below, update was a method I was trying to make but didn't work for some reason.

import pygame

pygame.font.init()


class textBox:
    def __init__(self, surface, id, color, width, height, x, y, antialias, maxtextlen):
        self.surface = surface
        self.id = id
        self.color = color
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        self.antialias = antialias
        self.maxtextlen = maxtextlen

        self.text_list = []
        self.text_list_keys = []
        self.currentId = 0
        self.click_check = False
        self.font = pygame.font.SysFont('comicsans', 20)
        self.dict_all = {}

        pygame.draw.rect(self.surface, (self.color), (self.x, self.y, self.width, self.height))
        # for i in self.text_list_keys:
        #     if self.id not in i:
        #         self.text_list_keys.append(self.id)
        #         self.text_list.append(tuple(self.id))
        #     else:
        #         self.nothing()
        self.dict_all[self.id] = tuple((self.x, self.y, self.width, self.height))

    def update(self, events, mousepos):
        for event in events:
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN and ((self.x + self.width) > mousepos[0] > self.x) \
                    and ((self.y + self.height) > mousepos[1] > self.y):
                print("reached: " + mousepos)
                self.click_check = True
            else:
                self.click_check = False

            if self.click_check:
                print("1")
                if event.type == pygame.KEYDOWN:
                    print("@")
                    if event.key == pygame.K_a:
                        print("reached")
                        new_t = ""
                        for j in range(len(self.text_list)):
                            t = (self.text_list[j][0]).index(self.getId(self.currentId))
                            new_t = t
                        self.text_list[new_t].append("a")
                        self.surface.blit(self.font.render(f'{self.text_list[new_t]}', self.antialias, (0, 0, 0)),
                                          (self.x, self.y))

                    else:
                        print("this")

            else:
                pass

    def rect(self, text_id, mousepos):
        x, y, width, height = self.dict_all[text_id]
        if ((x + width) > mousepos[0] > x) and ((y + height) > mousepos[1] > y):
            print("yes")
            return True
        else:
            print("no")
            return False

    def getId(self, text_id):
        self.currentId = text_id

    def nothing(self):
        return False

    def main(self, events, mousepos, id):
        for event in events:
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect(id, mousepos):
                    print("done")

test.py

import pygame
from pygame_textbox import textBox

pygame.init()

win_width = 500
win_height = 500

screen = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("test")

run = True
while run:
    mouse = pygame.mouse.get_pressed()
    screen.fill((0, 0, 0))
    events = pygame.event.get()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            exit()

    a = textBox(screen, 1, (255,  255, 255), 100, 30, 100, 100, True, 20)
    # a.getId(1)
    a.rect(1, mouse)
    a.main(events, mouse, 1)
    pygame.display.update()

Upvotes: 1

Views: 303

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

The 2nd argument to the method main has to be the mouse position rather than the mouse buttons:

run = True
while run:
    # [...]

    mouse_pos = pygame.mouse.get_pos()
    a.main(events, mouse_pos, 1)

    # [...]

While pygame.mouse.get_pressed() a sequence of booleans representing the state of all the mouse buttons, pygame.mouse.get_pos() returns the X and Y position of the mouse cursor.

Upvotes: 1

Related Questions