Changing Opacity
This section will demonstrate how you can change the opacity of a picture file when you draw it.
Changing Opacity
The function in this tutorial was obtained from the following website:
https://nerdparadise.com/programming/pygameblitopacity
Instructions:
You can download the file Opacity Example.py and the file boo.png from the Files section below into the same folder to see it work.
All you need to do is add the blit_alpha() function definition to the top of your python program and use it to draw your image instead of the regular blit().
You can determine how opaque the image is by altering the opacity argument you pass to the function. The opacity argument needs to be an integer from 0-255, where 0 is fully transparent, and 255 is normal.
Add this function to your Python program:
# Opacity needs to be a value from 0 - 255, where 0 is fully transparent
# and 255 is normal.
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)