S
S
Sanders Rocket2020-10-07 18:51:10
Python
Sanders Rocket, 2020-10-07 18:51:10

How to remove the background from a sprite?

import pygame, random, os
weak = 1
strong = 2

class Enemy(pygame.sprite.Sprite):
    def __init__(self,disp,enemytype):
        pygame.init()
        self.screen = disp
        pygame.sprite.Sprite.__init__(self)
        if enemytype == weak:
            self.enemytype = weak
            self.image = pygame.image.load(os.path.join("images", "enemy1.png"))
            self.dx = 2.5
            self.hp = 1
        if enemytype == strong:
            self.enemytype = strong
            self.image = pygame.image.load(os.path.join("images", "enemy2.png"))
            self.dx = 5
            self.hp = 2
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.rect.left = self.screen.get_width()
        self.rect.centery = random.randrange(0, self.screen.get_height())
        self.moving = False

    def update(self):
        if self.moving == True:
            self.rect.centerx -= self.dx
            if self.rect.right < 0:
                self.reset()
        if self.hp == 0:
            self.reset()

    def reset(self):
        self.rect.left = self.screen.get_width()
        self.rect.centery = random.randrange(0, self.screen.get_height())
        self.moving = False
        if self.enemytype == weak:
            self.hp = 1
        elif self.enemytype == strong:
            self.hp = 2

The background is not removed after changing the picture
huP3w5h.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question