Pygame create colored button using mask [duplicate] - python

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.

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

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

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

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:

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?

Is There a Way to Make a Rect Transparent in Pygame?

Is it possible to make a rect transparent in pygame?
I need it because I'm using rects as particles for my game. :P
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.
What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Draw a transparent rectangle in pygame
I have had this question as a pygame user before, and this is a method of solving your problem.

Categories