Mouse events do not work correctly in Pygame and Python

0

I have this:

while menu:
    #capturaEventos()
    global click
    global gameOver

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                gameOver = True
                pygame.quit()
                quit()
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                click = True
                print(click)
            else:
                click = False
                print(click)
    botonStart.imprimir()
    botonStart.interactuar()
    if botonStart.accion(0) and botonStart.interactuar():
        menu = False
        gameLoop()

The problem is that pressing the mouse button does not react correctly to the code, taking too long to react and having to press several times until True is printed. Not only that, but it does not react to the event KEYDOWN or pygame.QUIT . For everything to work correctly I have to delete these lines:

botonStart.imprimir()
botonStart.interactuar()

These lines are an object of the Boton class that contains this:

class Boton():
    def __init__(self, texto, color, x, y, ancho, altura):
        self.texto = texto
        self.color = color
        self.x = x
        self.y = y
        self.ancho = ancho
        self.altura = altura
        self.xCenter = self.x + ancho / 2
        self.yCenter = self.y + altura / 2
        self.on_click = False


    #Cuando se llama a esta función, se imprime un botón.   
    def imprimir(self):
        pygame.draw.rect(pantalla, self.color, (self.x, self.y, self.ancho, self.altura))

    #Cuando se llama a esta función, se interactua con el botón de forma que se enciende al pasar con el ratón. Retorna True si el ratón está sobre el botón 
    def interactuar(self):
        variacion = 75
        mouse = pygame.mouse.get_pos()
        if self.x + self.ancho > mouse[0] > self.x and self.y + self.altura > mouse[1] > self.y:
            pygame.draw.rect(pantalla, (math.fabs(self.color[0]-variacion), math.fabs(self.color[1]-variacion),math.fabs(self.color[2]-variacion)), (self.x, self.y, self.ancho, self.altura))
            #print("estoy dentro del boton")
            return True
        else:   
            pygame.draw.rect(pantalla, self.color, (self.x, self.y, self.ancho, self.altura))
            #print("estoy fuera del boton")
            return False
    def accion(self, boton):
        pass

Why is it malfunctioning? Why that lag that makes the code unusable? Can you show me the light, please?

    
asked by Toni 19.10.2018 в 20:29
source

0 answers