Skip to content Skip to sidebar Skip to footer

Pygame How To Detect Clicks From A List Of Images

For my levels screen I use images for each level button, there are 15 on a page. I was able to blit them all to the screen in a nice layout using some iteration and math, I have be

Solution 1:

pygame.Surface.blit returns a rectangle with the area of the affected pixels:

self.rect = self.screen.blit(self.level_shade, (x, y))

Alternatively you can get the bounding rectangle of a pygame.Surface by get_rect(). The location of this rectangle is (0, 0). Hence, you have to set the position by a keyword argument:

self.rect = self.level_shade.get_rect(topleft = (x, y)) 

Solution 2:

ifself.arrow_rect.collidepoint(*mousepos)

I don't know what you're packing on the line above. If you want to detect clicks, I think you should get mouse position first. I'll do something like this:

for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
            if event.type == pg.MOUSEBUTTONDOWN:
                #to menu
                mousepos = pg.mouse.get_pos() #Here lies the magic and no need to unpackif self.back_rect.collidepoint(mousepos):
                    on_levels = False
                    self.swipe.play()
                    self.start()
                #to levels2 pageif self.arrow_rect.collidepoint(mousepos):
                    on_levels = False
                    self.swipe.play()
                    self.levels_page2()
                #to level 1if self.l1_rect.collidepoint(mousepos):
                    on_levels = False
                    self.swipe.play()
                    self.load_level(1) 

If that solves your problem please let me know and of it doesn't still let me know!

Edit: Try if/elif block instead of just if.

Solution 3:

        mousepos = pg.mouse.get_pos()
        lvls = []

        for i, lvl inenumerate(self.levels_p1):
            #x, y topleft for 5x3 grid with easement and centered
            x = ((WIDTH - 100) * (i % 5) // 5) + 110
            y = HEIGHT // 10 + ((i // 5) * 125)
            #add to screen
            temp = self.screen.blit(lvl, (x, y))
            #if not unlockedif self.level_unlocked < i:
                #darken
                self.screen.blit(self.level_shade, (x, y))
            #if unlockedelse:
                #enlarged version if hovering over and unlockedif temp.collidepoint(*mousepos):
                    self.screen.blit(self.levels_1l[i], (x-6, y-6))
                #rect list for click detection
                lvls.append(temp)

        #back button interactiveif self.back_rect.collidepoint(*mousepos):
            self.screen.blit(self.t_back2, self.back_rect2)  # biggerelse:
            self.screen.blit(self.t_back, self.back_rect)  # normal#arrow button interactiveif self.arrow_rect.collidepoint(*mousepos):
            self.screen.blit(self.arrowr2, self.arrow_rect2)  # biggerelse:
            self.screen.blit(self.arrowr, self.arrow_rect)  # normal

        pg.display.flip()
        #all button pressesfor event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
            if event.type == pg.MOUSEBUTTONDOWN:
                #to menuif self.back_rect.collidepoint(*mousepos):
                    on_levels = False
                    self.swipe.play()
                    self.start()
                #to levels2 pageif self.arrow_rect.collidepoint(*mousepos):
                    on_levels = False
                    self.swipe.play()
                    self.levels_page2()
                #to levelfor i, val inenumerate(lvls):
                    #if clicked pos = level posif lvls[i].collidepoint(*mousepos):
                        on_levels = False
                        self.swipe.play()
                        #level to load
                        self.load_level(i+1)

Post a Comment for "Pygame How To Detect Clicks From A List Of Images"