1

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.

DXNI
  • 149
  • 14

1 Answers1

0

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.

Glitch__
  • 304
  • 1
  • 10
  • but that deletes the whole thing, i only want that 1 picture to disappear – DXNI May 18 '22 at 20:05
  • Then there are 2 solutions: clear the frame and redraw the entire frame without the picture or redraw the section where the image was showing. – Glitch__ May 18 '22 at 20:07
  • That seems like a bit overkill. Maybe i should just run a simple sprite when mouse collides with the rect – DXNI May 18 '22 at 20:12
  • You can only draw (modify pixels) to a surface. Another solution could be to store a copy of the window surface without the image, but that's a bit overkill. – Glitch__ May 19 '22 at 05:12