Colday96
Colday96

Reputation: 91

How can i get rid of an object after a collision in pygame?

I have created a program which makes squares bounce all around the screen. Every time they make contact with the player, they disappear (i change their location so that they are outside the display) and therefore cannot be seen by the user. Is it possible to not only change their location but also to completely get rid of them so that the CPU doesn't have to process them.

import pygame
import random
import sys
import time

Height = 800
Width = 800
Steps = 0
running = True
Number = 2000
x = 0

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

pygame.init()
display = pygame.display.set_mode((Height, Width))
clock = pygame.time.Clock()
pygame.display.set_caption("Game")


class Ball(object):

    def __init__(self, ball_pos_x, ball_pos_y, ball_height, ball_width, delta_x, delta_y):
        self.ball_pos_x = ball_pos_x
        self.ball_pos_y = ball_pos_y
        self.ball_height = ball_height
        self.ball_width = ball_width
        self.delta_x = delta_x
        self.delta_y = delta_y

    def draw(self):

        pygame.draw.rect(display, WHITE, (self.ball_pos_x, self.ball_pos_y,
                                          self.ball_height, self.ball_height))

    def update(self):
        self.ball_pos_x += self.delta_x
        self.ball_pos_y += self.delta_y

        if self.ball_pos_x < 0:
            self.delta_x = self.delta_x * -1
        if self.ball_pos_x > Width - self.ball_width:
            self.delta_x = self.delta_x * -1

        if self.ball_pos_y < 0:
            self.delta_y = self.delta_y * -1
        if self.ball_pos_y > Height - self.ball_height:
            self.delta_y = self.delta_y * -1


class Player:
    def __init__(self, player_height, player_width, pos_x, pos_y, player_vel):
        self.player_height = player_height
        self.player_width = player_width
        self.player_vel = player_vel
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.rect = self.pos_x

    def draw(self):
        pygame.draw.rect(display, BLUE, (self.pos_x, self.pos_y,
                                         self.player_height, self.player_width))

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.pos_x > 0:
            self.pos_x -= self.player_vel
        if keys[pygame.K_RIGHT] and self.pos_x < Width - player.player_width:
            self.pos_x += self.player_vel
        if keys[pygame.K_UP] and self.pos_y > 0:
            self.pos_y -= self.player_vel
        if keys[pygame.K_DOWN] and self.pos_y < Height - player.player_height:
            self.pos_y += self.player_vel

    def collision(self):

        pass


player = Player(10, 10, Width/2, Height/2, 5)
"""
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 0:
    x -= vel
if keys[pygame.K_RIGHT] and x < 800 - 30:
    x += vel
"""

list = []
for i in range(Number):
    ball = Ball(random.randrange(0, Width - 10), random.randrange(0, Height - 10),
                10, 10, random.randint(-10, 10), random.randint(-10, 10))
    list.append(ball)


while running:
    display.fill(BLACK)
    clock.tick(60)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()

    # Update

    # Draw
    for ball in list:
        ball.draw()
        ball.update()

        if (ball.ball_pos_x >= player.pos_x - ball.ball_width and ball.ball_pos_x <= player.pos_x + player.player_width and
                ball.ball_pos_y >= player.pos_y - ball.ball_height and ball.ball_pos_y <= player.pos_y + player.player_height):
            ball.ball_pos_x = -20
            ball.delta_x = -1

            x += 1
            print(x)

    player.draw()
    player.update()

    pygame.display.flip()

pygame.quit()

Upvotes: 2

Views: 1733

Answers (1)

Mercury Platinum
Mercury Platinum

Reputation: 1569

You can use self.kill() in the sprite class. That should remove them from the sprite group. Although as far as I know you need to inherit pygame.sprite.Spritealong with adding the sprite to a group.

So lets say:

examplegroup = pygame.sprite.Group()

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__(examplegroup):

And in the update() function...

if collided:
    self.kill()

Also, one of the advantages of using a sprite group over a list is that its generally faster, and can allow you do perform easier collision with pygame.sprite.spritecollide().

Upvotes: 1

Related Questions