0

I don't know what happend with my code but sometimes detects that I´m clicking something and sometimes doesn't, here is some code

import pygame
from pygame.locals import *
import sys

pygame.init()

modeX=500
modeY=600

rectangulo=pygame.Rect(1,1,2,2)


num_dados=0

ven=pygame.display.set_mode((modeX, modeY))

fps=pygame.time.Clock()

def fill():     
    ven.fill((0,0,0))

def text(txt, x, y, size,font, color):  
    myfont=pygame.font.SysFont(font,size)
    myText=myfont.render(txt,0,(color))
    ven.blit(myText,(x,y))

class hitbox_things():
    def __init__(self, X, Y,width, height):
        global escena, num_dados

        self.hitbox=pygame.Rect(X,Y,width,height)   

        pygame.draw.rect(ven, (255,0,255), self.hitbox)

        if rectangulo.colliderect(self.hitbox):

            for event in pygame.event.get():  

                if event.type==pygame.MOUSEBUTTONDOWN:

                    if event.button==1:
                        num_dados=num_dados+1

def hi_th_sprites():

    hitbox_things(180,30,30,30)
    hitbox_things(40,30,30,30)


    text(str(int(fps.get_fps())), 2, 22, 40, "Fixedsys", (255,255,255))
    text(str(num_dados), 100, 22, 40, "Fixedsys", (255,255,255))

def ipp():
    fill()
    hi_th_sprites()

################### UPDATE ##########################
class update:
    def __init__(self):

        while True:

            FPS=fps.tick(60)

            rectangulo.left, rectangulo.top=pygame.mouse.get_pos()

            ipp()

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

            pygame.display.flip()

ven=pygame.display.set_mode((modeX, modeY))

update()

You can copy it if you want and If you htterclick the pink button you will see that in some occasions doesn't work correctly, thank you

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Adding garbage to your post to circumvent the quality filter is a bad idea, as it does not add to the quality of the post in any way. On the contrary, it makes people downvote and flag this post for closure even faster than without. Please [edit] the question to contain more details and/or less code: narrow down the issue to the line causing the problems. Please see [ask] and [mcve]. – Adriaan Oct 05 '19 at 12:43
  • sorry It didn't let me publish it if I didn't put that, never again – Eduardo Delfante Oct 05 '19 at 12:51

1 Answers1

1

[...] sometimes detects that I´m clicking something and sometimes doesn't [...]

This is cause by the multiple event loops in your code. Note, pygame.event.get() gets all the messages and removes them from the queue. So one of the event loops randomly gets the events and the other loops miss it. Never all the event loops will get all the events. That causes that some events seems to be missed.

Get the list of events in the main loop and pass them to the functions, to solve the issue:

while True:

    # [...]

    # get the list of events
    events = pygame.event.get()

    # pass the vents to ipp
    ipp(events)            

    # handle quit event
    for event in events :
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
def hi_th_sprites(events):

    hitbox_things(events, 180,30,30,30)
    hitbox_things(events, 40,30,30,30)

    # [...]

def ipp(events):
    fill()
    hi_th_sprites(events)
class hitbox_things():
    def __init__(self, events, X, Y,width, height):

        # [...]

        if rectangulo.colliderect(self.hitbox):

            for event in events: # <---- use events 

                if event.type==pygame.MOUSEBUTTONDOWN:
                    if event.button==1:
                        num_dados=num_dados+1
Rabbid76
  • 202,892
  • 27
  • 131
  • 174