Python Forum
[PyGame] Problem with colorkey - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] Problem with colorkey (/thread-39971.html)



Problem with colorkey - Phidias618 - May-14-2023

Hello, in one of my project I encounter a problem with pygame.set_colorkey,
I have a code like this, where Sprite is just a class to load and get Sprite
The problem is that even if the color of the background of my image is (194, 195, 196) the background is still blit on screen
from sprites import Sprite
import pygame as py
screen = py.display.set_mode((300, 300))
Sprite.load('button_icon', 'Ressources\\Sprites\\button_icon.png')
sprt = Sprite.get('button_icon', (256, 64), (128, 0, 64, 64))
sprt.set_colorkey((194, 195, 196))
print(sprt.get_at((0, 0)))
screen.blit(sprt, (0, 0))
running = True
screen.set_colorkey((194, 195, 196))
while running:
    for event in py.event.get():
        if event.type == py.QUIT:
            running = False
            break
    py.display.flip()
This is the result of the program
[Image: image.png]


RE: Problem with colorkey - deanhystad - May-15-2023

What happens if you set the color key to the pixel color at (0, 0)?
sprt.set_colorkey(spri.get_at(0, 0))
What is the Sprite class? Is it some enhanced sprite?

Normally after loading an image you call convert() or convert_alpha(). Does your Sprite class do that autmatically?


RE: Problem with colorkey - Phidias618 - May-15-2023

The Sprite class is just a class that i created to store all the sprite of my game
it just store a dict that contain sprite name and Sprite and some classmethod to get a part of a sprite loaded in Sprite or the complete sprite, with some size

And I found a solution, after setting the surface's colorkey i just blit the surface onto itself, even it's not elegant that work


RE: Problem with colorkey - Windspar - May-15-2023

Converting the image first. Then set colorkey. You don't add colorkey to screen. Also you need to refresh screen.
It also better to use a path tool for joining paths. It makes it more portable.

from pathlib import Path
from sprites import Sprite
import pygame as py
screen = py.display.set_mode((300, 300))
Sprite.load('button_icon', Path('Ressources', 'Sprites', 'button_icon.png'))
sprt = Sprite.get('button_icon', (256, 64), (128, 0, 64, 64))
sprt.convert()
sprt.set_colorkey((sprt.get_at(0, 0))

running = True
while running:
    for event in py.event.get():
        if event.type == py.QUIT:
            running = False
    
    # Refresh screen
    screen.fill('black')
    screen.blit(sprt, (0, 0))
    py.display.flip()



RE: Problem with colorkey - Nivea04365 - Jun-09-2023

Have you tried reviewing those codes? If I'm not mistaken, the problem is not with the sprite but with the keyboard.
<url snipped>