[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] font alpha



Got it. You need to blit the rendered text to a new surface then change the alpha value of that new surface before blitting it to screen.

import pygame
pygame.init()
screen = pygame.display.set_mode((640,480), pygame.SRCALPHA)
screen.fill((0,0,0))

myFont = pygame.font.Font(None, 30)

nonTransparent = myFont.render("This text should be fully opaque", 1, (255,255,255))
screen.blit(nonTransparent, (0,0))

myText = 'This text should be somewhat transparent'
semiTransparent = myFont.render(myText, 1, (255,255,255))
newSurf = pygame.Surface(myFont.size(myText))
newSurf.blit(semiTransparent,(0,0))
newSurf.set_alpha(100)
screen.blit(newSurf, (0,100))

pygame.display.flip()



On 5-Sep-07, at 9:47 PM, Mike Lawrence wrote:

Good call Luke. Removing antialiasing did indeed result in a surface with a usable set_alpha component.

However, I'd really like to have antialiased text. Inserting 'semiTransparent.convert_alpha()' between lines 9 and 10 didn't seem to help either.

On 5-Sep-07, at 9:18 PM, Luke Paireepinart wrote:

Mike Lawrence wrote:
Hi all,

Apologies for the seemingly newb help request but I can't seem to figure out how to vary the alpha value of rendered font surfaces. Here are my attempts thusfar:

import pygame
pygame.init()
screen = pygame.display.set_mode((640,480), pygame.SRCALPHA)
screen.fill((0,0,0))

myFont = pygame.font.Font(None, 30)
nonTransparent = myFont.render("This text should be fully opaque", 1, (255,255,255))
screen.blit(nonTransparent, (0,0))
semiTransparent = myFont.render("This text should be somewhat transparent", 1, (255,255,255))
semiTransparent.set_alpha(100)
screen.blit(semiTransparent, (0,100))

pygame.display.flip()
#When I flip, both messages are the same color, indicating that the transparency on the second failed
I don't think that fonts are rendered with surface alpha to start.
does set_alpha apply surface alpha if they don't have it already?
Maybe try convert_alpha first?
It's possible that since you're aliasing the fonts they might be using per-pixel alpha which might conflict with surface alpha.
Just some suggestions, I've never tried this.
-Luke


--
Mike Lawrence
Graduate Student, Department of Psychology, Dalhousie University

Website: http://memetic.ca

Public calendar: http://icalx.com/public/informavore/Public

"The road to wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
    - Piet Hein





--
Mike Lawrence
Graduate Student, Department of Psychology, Dalhousie University

Website: http://memetic.ca

Public calendar: http://icalx.com/public/informavore/Public

"The road to wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
	- Piet Hein



--
Mike Lawrence
Graduate Student, Department of Psychology, Dalhousie University

Website: http://memetic.ca

Public calendar: http://icalx.com/public/informavore/Public

"The road to wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
	- Piet Hein