0

I have no idea how to make my ggift dissapear. I think i need to acces the particular gift in the list but i can't figure out how. This is the part:

 for ggift in GGift:
        if pygame.sprite.spritecollideany(personaj, ggift):
            GGift.remove(ggift)
            sunet_ciocnire.play()

the full code:

from turtle import *
import pygame
import random
from pygame.locals import (
    RLEACCEL,
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
)

latime_fereastra = 800
inaltime_fereastra = 600




class Avion(pygame.sprite.Sprite):
    def __init__(self):
        super(Avion, self).__init__()
        self.surf = pygame.image.load("santa.png").convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect()

    def update(self, tasta_apasata):
        if tasta_apasata[K_UP]:
            self.rect.move_ip(0, -5)
            sunet_urcare.play()
        if tasta_apasata[K_DOWN]:
            self.rect.move_ip(0, 5)
            sunet_coborire.play()
        if tasta_apasata[K_LEFT]:
            self.rect.move_ip(-5, 0)
        if tasta_apasata[K_RIGHT]:
            self.rect.move_ip(5, 0)

        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > latime_fereastra:
            self.rect.right = latime_fereastra
        if self.rect.top <= 0:
            self.rect.top = 0
        elif self.rect.bottom >= inaltime_fereastra:
            self.rect.bottom = inaltime_fereastra


class WGift(pygame.sprite.Sprite):
    def __init__(self):
        super(WGift, self).__init__()
        self.surf = pygame.image.load("gift.png").convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
                random.randint(latime_fereastra + 20, latime_fereastra + 100),
                random.randint(0, inaltime_fereastra),
            )
        )
        self.speed = random.randint(5, 15)


    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()

class GGift(pygame.sprite.Sprite):
    def __init__(self):
        super(GGift, self).__init__()
        self.surf = pygame.image.load("cadou.png").convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
                random.randint(latime_fereastra + 20, latime_fereastra + 100),
                random.randint(0, inaltime_fereastra),
            )
        )
        self.speed = random.randint(5, 15)


    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()

class Norisor(pygame.sprite.Sprite):
    def __init__(self):
        super(Norisor, self).__init__()
        self.surf = pygame.image.load("nor.png").convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
                random.randint(latime_fereastra + 20, latime_fereastra + 100),
                random.randint(0, inaltime_fereastra),
            )
        )


    def update(self):
        self.rect.move_ip(-5, 0)
        if self.rect.right < 0:
            self.kill()






pygame.mixer.init()
pygame.init()

cronometru = pygame.time.Clock()
fereastra = pygame.display.set_mode((latime_fereastra, inaltime_fereastra))

adauga_wgift = pygame.USEREVENT + 1
pygame.time.set_timer(adauga_wgift, 500)

adauga_ggift = pygame.USEREVENT + 3
pygame.time.set_timer(adauga_ggift, 500)

adauga_norisor = pygame.USEREVENT + 2
pygame.time.set_timer(adauga_norisor, 1000)

personaj = Avion()

wgift = pygame.sprite.Group()
ggift = pygame.sprite.Group()

norisori = pygame.sprite.Group()
lista_cadre = pygame.sprite.Group()
lista_cadre.add(personaj)

pygame.mixer.music.load("muzica.mp3")
pygame.mixer.music.play(loops = -1)

sunet_urcare = pygame.mixer.Sound("sus.ogg")
sunet_coborire = pygame.mixer.Sound("jos.ogg")
sunet_ciocnire = pygame.mixer.Sound("ciocnire.ogg")

sunet_urcare.set_volume(0.5)
sunet_coborire.set_volume(0.5)
sunet_ciocnire.set_volume(0.5)

joc_activ = True

while joc_activ:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                joc_activ = False
        elif event.type == QUIT:
            joc_activ = False
        elif event.type == adauga_wgift:
            wgift_noua = WGift()
            wgift.add(wgift_noua)
            lista_cadre.add(wgift_noua)
        elif event.type == adauga_norisor:
            norisor_nou = Norisor()
            norisori.add(norisor_nou)
            lista_cadre.add(norisor_nou)
        elif event.type == adauga_ggift:
            ggift_noua = GGift()
            ggift.add(ggift_noua)
            lista_cadre.add(ggift_noua)

    tasta_apasata = pygame.key.get_pressed()
    personaj.update(tasta_apasata)

    wgift.update()
    ggift.update()
    norisori.update()

    fereastra.fill((42, 73, 122))

    for cadru in lista_cadre:
        fereastra.blit(cadru.surf, cadru.rect)

    if pygame.sprite.spritecollideany(personaj, wgift):
        personaj.kill()
        sunet_urcare.stop()
        sunet_coborire.stop()
        sunet_ciocnire.play()
        joc_activ = False

    for ggift in GGift:
        if pygame.sprite.spritecollideany(personaj, ggift):
            GGift.remove(ggift)
            sunet_ciocnire.play()

    pygame.display.flip()

    cronometru.tick(30)


pygame.mixer.music.stop()
pygame.mixer.quit()

I need after personaj and that one ggift collide, just the ggift to dissapear

  • You're looking for the kill method, I think. and also a reference. You probably want to do: collided = pygame.sprite.spritecollideany(personaj, ggift): collided.kill() – yeeshue99 Dec 28 '22 at 21:06

0 Answers0