Pygame only display picture if certain condition is met

Viewed 20

I am making a game in pygame and I want a picture to appear, only if the cursor is on it. Here is what I did:

while True:

        clock.tick(FPS)

        mx, my = pygame.mouse.get_pos()

        
        darktower_rect = pygame.Rect(x, y, DARK_TOWER.get_width(), DARK_TOWER.get_height())
        if darktower_rect.collidepoint((mx,my)):
            WIN.blit(DARK_TOWERT, (x, y))
            if click:
                main()
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()

            if event.type==pygame.MOUSEBUTTONDOWN:
                if event.button==1:
                    click=True
        
        pygame.display.update()

This works, but the problem is, that blit keeps the picture there, and I want the picture to disappear, after the mouse is no longer on it.

1 Answers

If you blit (draw) to the WIN surface it will stay there until something is drawn over it, or the surface is cleared.

To clear the display after updating append to your code right after pygame.display.update()

WIN.fill(0)

This will fill the window surface with a color, if you specify 0 instead of an rgb value, the display will fill with black.

Related