Skip to content Skip to sidebar Skip to footer

How Do I Delete Rect Object From Screen Once Player Collides With It?

in draw() function I am trying to delete rect object when player pos = enemy pos but 'del' will not work. Any way to delete the enemy object completely? Is there a built in pygame

Solution 1:

You cannot "delete" something" what is draw on a Surface. A Surface contains just a bunch pixel organized in rows and columns. If you want to "delete" the rectangle, then you must not draw it. Create to pygame.Rect and do the collision test before. For instance:

defdraw():

    enemy_rect = pygame.Rect(enemy_x, enemy_y, 25, 25)
    player_rect = pygame.Rect(player_x, player_y, 25, 25)

    if player_rect.colliderect(enemy_rect):
        # create new enemy# [...]else:
        enemy = pygame.draw.rect(screen, enemy_color, enemy_rect)
    
    player = pygame.draw.rect(screen, player_color, player_rect)

Anyway I recommend to use pygame.sprite.Sprite objects organized in pygame.sprite.Group. pygame.sprite.Sprite.kill remove the Sprite from all Groups.

Post a Comment for "How Do I Delete Rect Object From Screen Once Player Collides With It?"