convert_alpha() not creating image transparency in PyGame png sprites [duplicate] - python

This question already has answers here:
Pygame image transparency confusion
(1 answer)
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
Closed 1 year ago.
I have a function that renders sprites:
self.sheet = pygame.image.load(file).convert_alpha()
But the sprites with transparency still render with black backgrounds. I can't use colorkey because for some reason my sprites always export with random discrepancies in colour where the colour is a few RGB units off the original colour, causing dots to randomly appear around the sprite.
And yes, the sprites I'm using are .png files, not jpg or jpeg.
So my question is, why isn't convert_alpha working, what can I do to make it work, or what else can I do to reach the same goal?
EDIT: here's the spritesheet:

Related

Using different colours as mask and changing colour on target image [duplicate]

This question already has answers here:
Is it possible to change sprite colours in Pygame?
(1 answer)
Changing colour of a surface without overwriting transparency
(1 answer)
Trying to make sections of sprite change colour, but whole sprite changes instead
(1 answer)
Changing ememy's color to show that it is aking damage?
(1 answer)
Closed 6 months ago.
Image: Target Image
Image: Mask
I was wondering if there is a way using pygame to change the target image colours based on a mask that uses different colours for different areas.
What I thought of doing was to loop through the mask, check each pixel for its colour and if said colour is yellow for example, which is the area of the head, then to alter the saturation of the colour of the target image.
I'd like to know if there is a better way to do? Thank you

Pygame create colored button using mask [duplicate]

This question already has answers here:
Is it possible to change sprite colours in Pygame?
(1 answer)
Changing colour of a surface without overwriting transparency
(1 answer)
Trying to make sections of sprite change colour, but whole sprite changes instead
(1 answer)
Closed 10 months ago.
I'm working on a pygame project and i need to create a button that changes color when hovered. I can't just use 2 images (one with the normal color and one with the hovered one) because i want to have a smooth transition between the 2 colors over a few frames. I could create all the images but it wouldn't be a very efficient method at all. So i got the idea of using an alpha mask like this one for the quit button:
Where white mean the color with full opacity and black means 0 opacity. To display my button, i wanted to create a surface of the same size as the mask, fill it with the current button color, and then use my image as the alpha mask like in this example:
mask = pygame.image.load("quit.png").convert()
button = pygame.surface(mask.get_size())
button.fill(ButtonColor)
---Here goes the part where i use my mask as the alpha for my button---
screen.blit(button, ButonPosition)
But i searched the pygame doc and it seams that i can't set per-pixel alpha. So i wanted to know if you had a solution, or an alternative idea to make my button.

Is there a function in Pygame to save a portion of the screen as an image? [duplicate]

This question already has answers here:
How can I crop an image with Pygame?
(4 answers)
Closed 1 year ago.
I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).
screen is a Surface object which has method subsurface(Rect) to get part of surface - and this part will be also a Surface object.
And there is function pygame.image.save(surface, filename)
Other method: at start create smaller Surface and draw on this surface (instead of drawing directly on screen) and blit() this surface on screen to display it and save() this surface to file.
You can even use many surfaces and keep them on list to create function Undo. When you use new tool then you duplicate surface and put it on list. When you use Undo then you get last surface from list.
If it's a drawing game, then you'll probably find it very easy to manually make the image.
I know it sounds scary, but it really isn't, especially in python.
You can use PyPng to convert a 2D array into an image, as I assume you already use a 2D array to store the drawing

Set opacity of image in pygame [duplicate]

This question already has answers here:
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
How can I make an Image with a transparent Backround in Pygame?
(2 answers)
Closed 2 years ago.
Okay guys, I'm trying to fade out an image. So given this:
img_ = pygame.image.load('circle.png')
screen.blit(img_, [100, 100])
how can I change the opacity of img_? Thanks!
Pygame surfaces have a method named set_alpha() for this, which takes a value between 0 and 255.
Relevant docs: https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha

Pygame draw a surface within a circle [duplicate]

This question already has answers here:
how to make circular surface in pygame
(1 answer)
How do I focus light or how do I only draw certain circular parts of the window in pygame?
(1 answer)
Closed 1 year ago.
I've been trying to figure out how to draw a texture within a circle with no luck.
For example, I have a red colored polygon that I drew on a surface called surf using pg.draw.polygon. Now I want to crop that surface into a circle (see images below)
From this
To this.
The way I was doing it was:
Create surf1 with a white circle and black background
Create surf2 with a white polygon and black background
Blit surf1 into surf2 with a multiply blend mode
Finally blit surf2 into the main game display surface
But this is too slow to even be considered a good idea... So is there an efficient way to create this?

Categories