Search code examples
pythonpygametransparencysurfacepygame-surface

make a rect transparent in pygame


I'm trying to make a kind of a Brick Breaker and got a problem with the transparency of the rectangle that surrounds the ball. every time it hits something you can see the rectangle. any suggestions?it also forces me to use a white background there is an image of the problem

import pygame
pygame.init()
bg_color = (255,255,255)
width, height = 600, 400
dx, dy = 2, 2
screen = pygame.display.set_mode((width, height))
screen.fill(bg_color)
ball = pygame.image.load("medicine-ball.png").convert()
ball = pygame.transform.scale(ball, (50, 50))
ball_rect = ball.get_rect()
ball_color = False
def rect(x1,y1,x2,y2):
    pygame.draw.rect(screen, (0,0,0), (x1,y1,x2,y2))
game_loop = True
while game_loop:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        game_loop = False
    ball_rect = ball_rect.move(dx,dy)
    if ball_rect.left < 0 or ball_rect.right > width:
        dx *= -1
    if ball_rect.top < 0 or ball_rect.bottom > height:
        dy *= -1

    mouse_pos = list(pygame.mouse.get_pos())
    rect(mouse_pos[0]-40,300-10,80,20)
    if ball_rect.bottom == 300 and ball_rect.x > mouse_pos[0]-89 and ball_rect.x < mouse_pos[0]+129:
        dy *= -1
    screen.blit(ball, ball_rect)
    pygame.time.wait(1)
    pygame.display.flip()
    screen.fill(bg_color)

another thing that is bothering me is that I can't change the speed of the ball, I am pretty sure it is a problem in my mac because it works on my friend's pc(it's about the pygame.time.wait())


Solution

  • If you want to make the images transparent, you need to make sure that the alpha channel of the images is set. Additionally you must use convert_alpha() instead of convert():

    if ball_color:
        ball = pygame.image.load("ball.png").convert_alpha()
    else:
        ball = pygame.image.load("medicine-ball.png").convert_alpha()
    

    See also the answers to the questions:


    make a rect transparent in pygame

    Unfortunately there is no good way to draw a transparent shape. See the answers to the question Draw a transparent rectangle in pygame and see pygame.draw module:

    A color's alpha value will be written directly into the surface [...], but the draw function will not draw transparently.

    Hence you need to do a workaround:

    1. Create a pygame.Surface object with a per-pixel alpha format large enough to cover the shape.
    2. Draw the shape on the _Surface.
    3. Blend the Surface with the target Surface. blit() by default blends 2 Surfaces

    For example 3 functions, which can draw transparent rectangles, circles and polygons:

    def draw_rect_alpha(surface, color, rect):
        shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
        pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
        surface.blit(shape_surf, rect)
    

    Use the function in your code instead of pygame.draw.rect, alpha is a value in range [0, 255]:

    def rect(x1, y1, x2, y2, alpha = 255):
        #pygame.draw.rect(screen, (0,0,0), (x1,y1,x2,y2))
        draw_rect_alpha(screen, (0, 0, 0, alpha), (x1, y1, x2, y2))