whenever I try to fill a rect with a transparent variable (to remove it from the screen) one of the rects turns brown

Viewed 31

I was trying to make a ending screen and whenever I fill the ball with the transparent variable I made, the tack disappears (as it should) but the ball turns into a brown square. or am I doing something else wrong? is there a workaround? Is there a way I can just delete the two rects and not the text? enter image description here

import pygame, sys, math, random



pygame.init()



size = width, height = 800, 600
white = 255, 255, 255
red = 255, 0, 0
black = 0, 0, 0
transparent = 0, 0, 0, 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

character = pygame.image.load("intro_ball.gif")
charrect = character.get_rect(topleft = (340, 480))
enemy = pygame.image.load("ho.png")
enrect = enemy.get_rect(topleft = (random.randint(0, 690), 0))
lose = False
run = True
font = pygame.font.SysFont(None, 25)
losemsg = font.render("You Lose", True, black)
msgrect = losemsg.get_rect(topleft = (340, 240))


while lose != True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            pygame.quit()
            quit()

    keys = pygame.key.get_pressed()
    charrect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
    charrect.clamp_ip(screen.get_rect())

    
    if enrect.y >= 600 and lose != True:
        enrect.y = 0
        enrect.x = random.randint(0, 690)

    collide = pygame.Rect.colliderect(charrect, enrect)




    if collide:
        lose = True
    else: lose = False





    enrect.y += 6
    screen.fill(white)
    screen.blit(enemy, enrect)
    screen.blit(character, charrect)
    pygame.display.update()

while lose == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: lose = False
    character.fill(transparent)
    enemy.fill(transparent)
    screen.fill(red)

    screen.blit(losemsg, msgrect)
    screen.blit(enemy, enrect)
    screen.blit(character, charrect)
    pygame.display.update()


pygame.quit()
quit()
1 Answers

The character has no alpha channel. Use pygame.Surface.convert_alpha() to change the pixel format including per pixel alphas:

character = pygame.image.load("intro_ball.gif")

character = pygame.image.load("intro_ball.gif").convert_alpha()

However, if you want to make an object "invisible", just don't draw it:

while lose == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: lose = False
    #character.fill(transparent)       <--- DELETE
    #enemy.fill(transparent)           <--- DELETE
    screen.fill(red)

    screen.blit(losemsg, msgrect)
    # screen.blit(enemy, enrect)       <--- DELETE
    # screen.blit(character, charrect) <--- DELETE
    pygame.display.update()
Related