3

Below is some code I'm working on that is printing doubles to the CMD window. It's not a huge issue that I'm seeing doubles, I'm just worried that sooner or later it may turn into a larger problem when this program grows.

This is an example of what CMD shows me when I left click:

Left mouse pressed at (451, 279)
Left mouse pressed at (451, 279)
Left mouse released at (451, 279)
Left mouse released at (451, 279)

I've left out a lot of code for brevity, and pinpointed it to these classes and functions. I apologize in advance - I'm very new to this site, and python.

class Button:       
    def whichButton(self):
        #this function takes the mousebuttondown event, and returns WHICh button is pressed
        if self.button == 1:
            return "Left"
        if self.button == 2:
            return "Middle"
        if self.button == 3:
            return "Right"
        if self.button == 4:
            return "Wheel Up"
        if self.button == 5:
            return "Wheel Down"     

    def handleEvent(self, event):

        if event.type == pygame.MOUSEBUTTONDOWN:
            print( Button.whichButton(event)+' mouse pressed at ' + str(event.pos))
            if self.rect.collidepoint(event.pos):
                self.buttonDown = True
                self.image = self.imageDown

class Game:
    self.all_sprites.add(self.startButton, self.quitButton)
    def run(self):
        while not self.done:
            self.frameRate = self.clock.tick(60) / 1000
            self.handleEvent()


    def handleEvent(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.done = True
            for button in self.all_sprites:
                button.handleEvent(event)




if __name__ == '__main__':
    pygame.init()
    Game(screen).run()
    pygame.quit()
HDPZR
  • 73
  • 6
  • How many items are contained within `self.all_sprites`? – esqew Aug 29 '18 at 19:32
  • Do you have overlapping `Button` sprites? – Blckknght Aug 29 '18 at 19:35
  • @esqew made an interesting point. I added another (third) button to all_sprites list and duplicated (actually tripled) the buggy error. Is there a better way to loop button.handleEvent without using "for button in self.all_sprites:" ? – HDPZR Aug 29 '18 at 20:40
  • Please post a [minimal, runnable example](https://stackoverflow.com/help/mcve), otherwise it's difficult or even impossible to figure out what's going wrong. – skrx Aug 30 '18 at 06:13
  • @skrx Wow. Hello! This is actually (mostly) **your code** from https://stackoverflow.com/questions/47639826/pygame-button-single-click/47664205#47664205 (addendum 1). I am trying to learn how this all works by dissecting and experimenting with everything. I wanted to keep track of all the actions in the CMD window, and came up with this odd bug. I was actually nervous about posting all the code since it isn't 100% written by me. (not sure of the rules on that yet). Anyways, here is a link to the full code with the error being duplicated: https://pastebin.com/VYrTuxhV – HDPZR Aug 30 '18 at 11:29
  • 1
    BTW, since you're using the `whichButton` method like a [static method](https://stackoverflow.com/questions/735975/static-methods-in-python), you could also decorate it with `@staticmethod`. Then you can call it in this way `self.whichButton()`. And rename the `self` parameter in the method to `event`, otherwise it's confusing. You could also just turn `whichButton` into a global function. – skrx Aug 31 '18 at 08:38

1 Answers1

1

That happens because you're calling button.handle_event(event) for each button in the self.all_sprites group in a for loop, so you're printing print( Button.whichButton(event)+' mouse pressed at ' + str(event.pos)) for each button.

def handle_events(self):
    for event in pg.event.get():
        if event.type == pg.QUIT:
            self.done = True
        # Iterates over all buttons in the group and calls their `handle_event` method.
        for button in self.all_sprites:
            button.handle_event(event)
skrx
  • 19,980
  • 5
  • 34
  • 48
  • Would it be more efficient to handle all the events at once, rather than defined by how many buttons are in the group ? – HDPZR Aug 31 '18 at 09:35
  • That probably won't matter unless you have hundreds of buttons. Only optimize the code if you have a performance problem. You can use a profiler to figure out where programs could be improved. I'm not sure if there would be a noticeable difference between passing the events separately or passing a list of all events to each button. You could write a program to test this. – skrx Aug 31 '18 at 10:11