Paria
Paria

Reputation: 38

How to remove an image in pygame?

I'm working on a game and I have some problems working with images. I have loaded a few images . loading them and using screen.blit() was okay like below:

img1 = pygame.image.load("leaf.png")
img1 = pygame.transform.scale(img1, (25,25))
leaf = img1.get_rect()
leaf.x = random.randint(0, 570)
leaf.y =  random.randint(0, 570)

but I don't know how to remove them in an if statement like this for example:

if count == 1:
...

and I though maybe there is no way and I should draw a rectangle on the image to disappear it. Also I don't know how to use screen.fill() while I don't want the other images to get disappeared. Is there any other way?

Upvotes: 0

Views: 21569

Answers (2)

wajdan ali
wajdan ali

Reputation: 1

you can use screen.fill() for that following is an example that shows how to do it:

import pygame
import time
screen = pygame.display.set_mode((500,500))
screen.fill((255,255,255))#white
img = pygame.image.load('img.png')
img = pygame.transform.scale(img,(25,25))
img2 = pygame.image.load('bubble.png')
img2 = pygame.transform.scale(img,(25,25))
screen.blit(img,(250,250))
screen.blit(img2,(350,250))
pygame.display.update()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        screen.fill((255,255,255))
        screen.blit(img2, (350, 250))
        pygame.display.update()
        time.sleep(1)
        screen.blit(img, (255,255))
        pygame.display.update()

for making any type of animation om pygame you have to redraw every thing including the background

Upvotes: 0

Noobles
Noobles

Reputation: 87

You can fill individual images, since they are pygame Surfaces.

First, what I would do is I would put something like this after defining the leaf's x/y:

leaf.image = img1

Then, I would create a color variable called transparent:

transparent = (0, 0, 0, 0)

The first 3 numbers, as you might know, represent RGB color values. The last number is the alpha (transparency) value of a color. 0 is completely invisible.

Finally, I would add this code to make the leaf completely transparent:

leaf.image.fill(transparent)

This makes the leaf transparent without making every other image in your window disappear. Hope this helped!

Upvotes: 5

Related Questions