how to make image/images disappear in pygame?
Solution 1:
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
- handle the events by either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by either
pygame.display.update()
orpygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()