Search code examples
pythonpygamecollision-detection

deleting rectangles (eating apples)


im trying to make a game where the player eats an apple and has to delete a rectangle after the player has collided with it but im getting an errer of: AttributeError: 'pygame.Rect' object has no attribute 'remove'

import pygame
import sys




player_pos=x,y,width,height=(0,0,50,50)
enemy_pos=e_x,e_y,e_width,e_height=(200,0,50,50)

pygame.init()
screen=pygame.display.set_mode((1000,440))
FPS=pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_UP:
                y-=25

            if event.key==pygame.K_DOWN:
                y+=25

            if event.key==pygame.K_RIGHT:
                x+=25

            if event.key==pygame.K_LEFT:
                x-=25


            if player1.colliderect(enemy1):
                enemy1.remove()

screen.fill((0,0,0))
player1=pygame.draw.rect(screen,(255,0,0),(x,y,width,height))
enemy1=pygame.draw.rect(screen,(0,0,255),(e_x,e_y,e_width,e_height))
pygame.display.update()
FPS.tick(120)

Solution

  • If you want to "remove" an object form the scene, all you have to do is stop drawing it.
    Add a variable draw_enemy and initialize it to True. When the player collides with the enemy set the variable to False. Draw the enemy depending on the state of draw_enemy:

    draw_enemy = True
    
    while True:
        # [...]
    
        if player1.colliderect(enemy1):
            draw_enemy = False
    
        screen.fill((0,0,0))
        player1 = pygame.draw.rect(screen,(255,0,0),(x,y,width,height))
    
        if draw_enemy:
            enemy1 = pygame.draw.rect(screen,(0,0,255),(e_x,e_y,e_width,e_height))
    
        pygame.display.update()
    

    Alternatively create the pygame.Rect before ahead:

    player_rect = pygame.Rect(0, 0, 50, 50)
    enemy_rect = pygame.Rect(200, 0, 50, 50)
    

    Create a new random position of the object when the collision is detected:

    if player_rect.colliderect(enemy_rect):
        enemy_rect.x = random.randrange(0, 975, 25)
        enemy_rect.y = random.randrange(0, 425, 25)
    

    Minimal example:

    import pygame
    import random
    pygame.init()
    screen = pygame.display.set_mode((1000,440))
    clock = pygame.time.Clock()
    
    player_rect = pygame.Rect(0, 0, 50, 50)
    enemy_rect = pygame.Rect(200, 0, 50, 50)
    
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                exit()
    
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP:
                    player_rect.y -= 25
                if event.key==pygame.K_DOWN:
                    player_rect.y += 25
                if event.key==pygame.K_RIGHT:
                    player_rect.x += 25
                if event.key==pygame.K_LEFT:
                    player_rect.x -= 25
        
        if player_rect.colliderect(enemy_rect):
            enemy_rect.x = random.randrange(0, 975, 25)
            enemy_rect.y = random.randrange(0, 425, 25)
    
        screen.fill((0,0,0))
        pygame.draw.rect(screen, (255,0,0), player_rect)
        pygame.draw.rect(screen, (0,0,255), enemy_rect)
        pygame.display.update()
        clock.tick(120)