0

I have been fiddling with this for hours, I am new to this stuff and for this one I can figure it out so any help would be much appreciated.

I have been trying to get my code to only detect 1 click when you click once. About 80% of the time this happens however occasionally it will click in an area 3-5 times and I was wondering how I could avoid that.

This is part of my code that I am struggling with:

for event in pygame.event.get():
            state=pygame.key.get_pressed()
            print (state)
            if event.type == pygame.QUIT:
                        pygame.quit()
                        break
            mouse=pygame.mouse.get_pos()
            print (mouse)
            if event.type == pygame.MOUSEBUTTONDOWN and 1015>mouse[0]>825 and 220>mouse[1]>160 and rand==1:
                    mouse= pygame.mouse.get_pos()
                    setDisplay.blit(boxz, (0,0))
                    pygame.display.update()
                    time.sleep(0.4)


            if event.type == pygame.MOUSEBUTTONDOWN and 1235>mouse[0]>1045 and 220>mouse[1]>160 and rand==1:


                    setDisplay.blit(a2z, (0,0))
                    setDisplay.blit(zzz3, (800, 20))
                    pygame.display.update()
                    time.sleep(0.4)
Camil
  • 17
  • 6
  • I do not suggest calling time.sleep() or pygame.display.update() in the event processing loop. I'm not sure if this is causing your problem, but it's bad practice. Also note that pygame.MOUSEBUTTONDOWN includes all mouse buttons, including (I think) the scrollwheel. You should be checking what button was pressed. – user3757614 Jan 09 '15 at 18:15
  • You need to keep track of the current time and the time when the click happened and assert that it cannot happen more than 1 time within a given duration. – Mike McMahon Jan 09 '15 at 19:54
  • Also, you really should define the area you are asserting in a pygame rectangle object, and set its vaues in a separate part of the code. It is simply too weird to have these hard-coded numbers inside the code dealing with the mouse. And it is better to call `pygame.time.delay` instead of `time.sleep` – jsbueno Jan 14 '15 at 13:14

0 Answers0