2

I used set_colorkey((255,255,255)) on my sprites but it doesn't make all the white transparent. I already tried doing .convert() but it is still the same. This is how it looks: image1 This is the code where I try to make the color transparent:

class Snake:
    def __init__(self, parent_screen):
        self.parent_screen = parent_screen
        self.stationary = pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/stationary down.png')

        self.left = pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/cycle left.png')


        self.right = pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/cycle right.png')


        self.up = [pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/left leg up.png'),
                   pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/right leg up.png')]

        self.down =[pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/right leg down.png'),
                    pygame.image.load('/Users/gersh/PycharmProjects/snakeeo/venv/lib/resources/ash trainer sprites/left leg down.png')]
        self.x = 100
        self.y = 100
        self.direction = 'down'

    def draw(self):
        global stepIndex
        self.parent_screen.fill((0, 0, 0))
        if stepIndex >= 2:
            stepIndex = 0
        if self.direction == 'left':
            self.left.set_colorkey((255, 255, 255))
            self.parent_screen.blit(self.left, (self.x, self.y))

        elif self.direction == 'right':
            self.right.set_colorkey((255, 255, 255))
            self.parent_screen.blit(self.right, (self.x, self.y))

        elif self.direction == 'up':
            self.up[0].set_colorkey((255, 255, 255))
            self.up[1].set_colorkey((255, 255, 255))
            self.parent_screen.blit(self.up[stepIndex], (self.x, self.y))
            stepIndex += 1
        elif self.direction == 'down':
            self.down[0].set_colorkey((255, 255, 255))
            self.down[1].set_colorkey((255, 255, 255))
            self.parent_screen.blit(self.down[stepIndex], (self.x, self.y))
            stepIndex += 1
        pygame.display.flip()
        pygame.time.delay(30)
gershinho
  • 33
  • 4
  • 1
    Maybe adding one of your sprites to the question would help. From the screen-shot, it looks like the background of the sprite is white. But the edges of the sprite "fade" into the background, so you have a grey edge. Obviously grey is not white, so the colour-key does not remove it. One great way to fix this is to re-do your sprites to be based on a transparent background. – Kingsley Aug 02 '22 at 00:50
  • *"but it doesn't make all the white transparent"* - not all of the pixels are completely white (255, 255, 255). The pixels at the borders are almost white (e.g. (250, 252, 253). Why do you not use an image with a transparent background? See [Pygame image transparency confusion](https://stackoverflow.com/questions/64704789/pygame-image-transparency-confusion/64704923#64704923) – Rabbid76 Aug 02 '22 at 05:01
  • @Rabbid76 how to I get an image with a transparent background? if I download an image off the web it always has some sort of background, even if it is black or white. – gershinho Aug 05 '22 at 02:33
  • @Kingsley how do I make my sprite have a transparent background? – gershinho Aug 05 '22 at 02:33
  • In whatever image editing package you use, start with an "Alpha Channel" or "Transparent Background". In The GNU Image Manipulation Program (https://www.gimp.org) the step is Layer->Transparency->Add Alpha Channel. If you paste your sprite on top of this transparent background, it may then be possible to erase the existing background making it transparent with the Eraser Tool. Failing that, starting with a transparent background, re-draw the sprite. – Kingsley Aug 08 '22 at 06:26

0 Answers0