how to remove sprite image from screen using pygame.sprite.spritecollide() for coin collection, powerups etc.?

33 Views Asked by At

I am a beginner at pygame/python and classes but I have been following a tutorial to create a top down game (https://www.youtube.com/playlist?list=PLkkm3wcQHjT7gn81Wn-e78cAyhwBW3FIc). I have done the enemy collisions part, but I want to add coin collection, powerups, items etc. in my game.

The code works, but it just doesn't remove from the screen or even print out a coin count when I was seeing if it would collide with it atleast.

I am unsure whether it is my parameters where I went wrong, or just something else as a whole. If anyone could please give me some advice on how to remove the sprite image when my player collides with it and add it to a coin count where I can use it in other parts of my game, I would really appreciate it!

This is the code for enemy collisions in my Player class:

    def collide_enemy(self):
        hits = pygame.sprite.spritecollide(self, self.game.enemies, False)
        if hits:
            #if hits it will remove player from screen
            self.kill()
            self.game.playing = False

This is in my Coins class (different file):

    def update(self):
        self.animate()
        self.collide_player()

    def collide_player(self):
        coins_count = 0
        hits = pygame.sprite.spritecollide(self, self.game.coins, False)
        if hits:
            self.kill() #to check if it will remove player and is working
            coins_count += 1
            print(coins_count)

And this is where the group is created for layering and creating my tilemap in my Game class (also diff. file):

    def new_game(self):
        #a new game starts
        self.playing = True
        
        self.all_sprites = pygame.sprite.LayeredUpdates()
        self.blocks = pygame.sprite.LayeredUpdates()
        self.enemies = pygame.sprite.LayeredUpdates()
        self.platforms = pygame.sprite.LayeredUpdates()
        self.coins = pygame.sprite.LayeredUpdates()

        
        self.createTilemap()    

If you need more code, please do ask because I am unsure on what else to give.

This is all of my Coins class if needed:

class Coins(pygame.sprite.Sprite):
    def __init__(self, game, x, y):
        
        self.game = game
        self._layer = COIN_LAYER #tell pygame in what layer the sprite will appear
        #the bottom line does not draw my coins on screen
        #self.groups = self.game.all_sprites, self.game.coins
        self.groups = self.game.all_sprites
        pygame.sprite.Sprite.__init__(self,self.groups) #adding coins to allsprites groups
        
        self.x = x * TILESIZE - OFFSET_X
        self.y = y * TILESIZE - OFFSET_Y
        self.width = TILESIZE
        self.height = TILESIZE
        
        self.animation_loop = 1
        
        #creating a rect that is calling the method and then setting the cut out
        self.image = self.game.coin_spritesheet.get_sprite(6, 7, self.width, self.height)        
                
        self.rect = self.image.get_rect()
        self.rect.x = self.x
        self.rect.y = self.y
    
    def update(self):
        self.animate()
        self.collide_player()

    def collide_player(self):
        coins_count = 0
        hits = pygame.sprite.spritecollide(self, self.game.coins, False)
        if hits:
            self.kill() #to check if it will remove player and is working
            coins_count += 1
            print(coins_count)
            

    def animate(self):
        rotating_coin = [self.game.coin_spritesheet.get_sprite(4, 4, self.width, self.height),
                           self.game.coin_spritesheet.get_sprite(37, 4, self.width, self.height),
                           self.game.coin_spritesheet.get_sprite(70, 4, self.width, self.height),
                           self.game.coin_spritesheet.get_sprite(104, 4, self.width, self.height),
                           self.game.coin_spritesheet.get_sprite(131, 4, self.width, self.height),
                           self.game.coin_spritesheet.get_sprite(163, 4, self.width, self.height)
        ]        
        
        self.image = rotating_coin[math.floor(self.animation_loop)]
        self.animation_loop += 0.1
        if self.animation_loop >= 6:
            self.animation_loop = 1
0

There are 0 best solutions below