Search code examples
pythonpygamecollisiondel

pygame object not being deleted and square appearing only during collision


I am trying to make a new rectangle appear when collided with an obstacle and the obstacle to be deleted but the del command doesn't work like in a tutorial i saw and the object only appears while the shape is colliding with the obstacle. How do i make the new rect appear permanently after the collision and not only while colliding and how do I fix the del command not working?

#just the code for the collision   
if main.colliderect(obstacle):
    del obstacle
    pygame.draw.rect(window, color2, pygame.Rect(xr2, yr2, 30, 30))
#all of the code        
import pygame
import math
import random
import time
from random import randint
import sys
pygame.init()
fps = 30
fpsclock=pygame.time.Clock()
window = pygame.display.set_mode((600, 600))

x = 275
y = 275
xr = randint(30,270)
yr = randint(30,270)
xr2 = randint(30,270)
yr2 = randint(30,270)
color = (255,0,0)
color2 = (0,0,255)

# main application loop
run = True
while run:
    # limit frames per second
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # clear the display
    window.fill(0)

    # draw the scene
    key_input = pygame.key.get_pressed() #key imputs
    main = pygame.draw.rect(window, color, pygame.Rect(x,y,30,30))
    obstacle = pygame.draw.rect(window, color2,pygame.Rect(xr,yr,30,30))
    pygame.display.flip()
    if key_input[pygame.K_LEFT]:
        x -= 5
    if key_input[pygame.K_RIGHT]:
        x += 5
    if key_input[pygame.K_DOWN]:
        y += 5
    if key_input[pygame.K_UP]:
        y -= 5
    if main.colliderect(obstacle):
        del obstacle
        pygame.draw.rect(window, color2, pygame.Rect(xr2, yr2, 30, 30))

    pygame.display.update()
    fpsclock.tick(fps)

    # update the display
    pygame.display.flip()

pygame.quit()
exit()

Solution

  • You create the obstacle in each frame from the coordinates (xr, yr). I recommend to manage the obstacles in a list and remove the obstacle from the list.

    Create a list of obstacles:

    obstacles = []
    obstacles.append(pygame.Rect(xr,yr,30,30))
    

    Use a for-loop to draw the obstacles:

    for obstacle in obstacles:
        pygame.draw.rect(window, color2, obstacle)
    

    So the collision test in a for-loop and remove the obstacles from the list (see How to remove items from a list while iterating?)

    for obstacle in obstacles[:]:
        if main.colliderect(obstacle):
            obstacles.remove(obstacle)
    

    Minimal example:

    import pygame
    from random import randint
    pygame.init()
    fps = 30
    fpsclock=pygame.time.Clock()
    window = pygame.display.set_mode((600, 600))
    
    x = 275
    y = 275
    xr = randint(30,270)
    yr = randint(30,270)
    xr2 = randint(30,270)
    yr2 = randint(30,270)
    color = (255,0,0)
    color2 = (0,0,255)
    
    obstacles = []
    obstacles.append(pygame.Rect(xr,yr,30,30))
    
    # main application loop
    run = True
    while run:
        # limit frames per second
        # event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        # clear the display
        window.fill(0)
    
        # draw the scene
        key_input = pygame.key.get_pressed() #key imputs
        main = pygame.draw.rect(window, color, pygame.Rect(x,y,30,30))
        for obstacle in obstacles:
            pygame.draw.rect(window, color2, obstacle)
        
        pygame.display.flip()
        if key_input[pygame.K_LEFT]:
            x -= 5
        if key_input[pygame.K_RIGHT]:
            x += 5
        if key_input[pygame.K_DOWN]:
            y += 5
        if key_input[pygame.K_UP]:
            y -= 5
    
        for obstacle in obstacles[:]:
            if main.colliderect(obstacle):
                obstacles.remove(obstacle)
    
        pygame.display.update()
        fpsclock.tick(fps)
    
        # update the display
        pygame.display.flip()
    
    pygame.quit()
    exit()