pygame.sprite.spritecollide() not remove sprite

hi I'm making a brick breaking game with pygame,as far as I know

pygame.sprite.spritecollide(ball, block_list, True, pygame.sprite.collide_mask)

this part confirms the collision between the sprite group and the sprite and removes the colliding brick object from block_list

to make it easier to see the collision with the ball, this code allowed the ball to move freely with the mouse and I comfirm that the bumped wall is remove from group with "print(block_list)" However, the bricks on the screen were not removed

How do I make a brick disapear from screen as soon as ball hits the brick?

import pygame
import random


class Block(pygame.sprite.Sprite):
    def __init__(self, img):
        pygame.sprite.Sprite.__init__(self)
        self.image = img
        self.rect = img.get_rect()

pygame.init()
screen = pygame.display.set_mode((940, 768))

stickimage = pygame.image.load('stick.png')
stick = stickimage.get_rect()
stick.center = (500, 700)

background = pygame.Surface(screen.get_size())

pic = [pygame.image.load("half/brick1.png").convert_alpha(), pygame.image.load("half/brick2.png").convert_alpha(),
       pygame.image.load("half/brick3.png").convert_alpha(), pygame.image.load("half/brick4.png").convert_alpha()]

block_list = pygame.sprite.Group()

for j in range(0, 5):
    for i in range(0, 7):
        block = Block(pic[random.randrange(4)])
        block.rect.x = i * 135
        block.rect.y = j * 30
        block.mask = pygame.mask.from_surface(block.image)
        block_list.add(block)

ball_pic = pygame.image.load("ball.png").convert_alpha()
ball = Block(ball_pic)
ball.rect.center = (500, 500)
ball.mask = pygame.mask.from_surface(ball.image)
ball.radius = 23

score = 0

done = True

while done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:  
            done = False  
        if event.type == pygame.MOUSEMOTION: 
            ball.rect.center = event.pos
        break

    hit_list = pygame.sprite.spritecollide(ball, block_list, True, pygame.sprite.collide_mask)

    for h in hit_list:
        score += 1

    print(block_list)
    print(hit_list)

    block_list.draw(background)  
    
    screen.blit(background, (0, 0))  
    screen.blit(ball.image, ball.rect)
    screen.blit(stickimage, stick)

    pygame.display.flip()  

Upvotes: 1

Views: 211

Answers (1)

Rabbid76
Rabbid76

Reputation: 211116

I recommend to convert the pygame.Surface of the Sprite to a surface with convert_alpha(). The mask attribute can be created in the constructor of the Sprite object:

class Block(pygame.sprite.Sprite):
    def __init__(self, img):
        pygame.sprite.Sprite.__init__(self)
        self.image = img.convert_alpha()                 # <----
        self.rect = img.get_rect()
        self.mask = pygame.mask.from_surface(self.image) # <----

Anyway, the major issue is, that you draw the bricks on the background surface rather than on the window surface:

block_list.draw(background)  

The background surface is never cleared. Draw the background in the window, then draw the bricks on top of the background:

while done:
    # [...]

    screen.blit(background, (0, 0))  
    block_list.draw(screen)      
    screen.blit(ball.image, ball.rect)
    screen.blit(stickimage, stick)

    pygame.display.flip() 

Upvotes: 2

Related Questions