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

Re: [pygame] font alpha



I found Numeric indispensable for shadow effects.

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

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))

# Get access to the alpha band of the image.
pixels_alpha = pygame.surfarray.pixels_alpha(semiTransparent)
# Do a floating point multiply, by alpha 100, on each alpha value.
# Then truncate the values (convert to integer) and copy back into the surface.
pixels_alpha[...] = (pixels_alpha * (100 / 255.0)).astype(Numeric.UInt8)
# Unlock the surface.
del pixels_alpha

screen.blit(semiTransparent, (0,100))

pygame.display.flip()


Hope it helps.

Lenard Lindstrom



Mike Lawrence wrote:
Ah, well eventually I'll be overlaying different font layers over top of one another so maybe you're right in that my solution is still unsatisfactory.

On 6-Sep-07, at 2:00 PM, Brian Fisher wrote:

haha...  I assumed you wanted something font letters with alpha - so
you had something that could be drawn over something other than a
solid color - but I guess you could always grab the background behind
where you want the text in a surface, blit the text over the piece of
the background you grabbed in the surface, then set alpha on the
background surface and blit that, and it would work for the effect you
want no matter the background.


On 9/6/07, Mike Lawrence <Mike.Lawrence@xxxxxx> wrote:
Rendering the semi-transparent text with the appropriate background
'seems' to make it work again.

#---------------------------------
# Mike's Code modified to use non-black screen
import pygame
import time

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

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), (255,0,255))
newSurf = pygame.Surface(myFont.size(myText))
newSurf.blit(semiTransparent,(0,0))
newSurf.set_alpha(100)
screen.blit(newSurf, (0,100))

pygame.display.flip()

time.sleep(5)

--
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




-