def textlined(self, color, text, center=None, pos='center'):
darkcolor = [int(c//2) for c in color]
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img1 = self.font.render(text, 1, color)
img2 = self.font.render(text, 1, darkcolor)
else:
img1 = img2 = self.font.render(text, 0, color)
img2 = self.font.render(text, 0, darkcolor)
except (pygame.error, TypeError):
img1 = img2 = pygame.Surface((10, 10))
newsize = img1.get_width()+4, img1.get_height()+4
img = pygame.Surface(newsize)
img.blit(img2, (0, 0))
img.blit(img2, (0, 4))
img.blit(img2, (4, 0))
img.blit(img2, (4, 4))
img.blit(img1, (2, 2))
img = img.convert()
img.set_colorkey((0,0,0), pygame.RLEACCEL)
r = self._positionrect(img, center, pos)
return [img, r]
python类RLEACCEL的实例源码
def textshadowed(self, color, text, center=None, pos='center'):
darkcolor = [int(c//2) for c in color]
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img1 = self.font.render(text, 1, color)
img2 = self.font.render(text, 1, darkcolor)
else:
img1 = img2 = self.font.render(text, 0, color)
img2 = self.font.render(text, 0, darkcolor)
except (pygame.error, TypeError):
img1 = img2 = pygame.Surface((10, 10))
newsize = img1.get_width()+2, img1.get_height()+2
img = pygame.Surface(newsize)
img.blit(img2, (2, 2))
img.blit(img1, (0, 0))
img = img.convert()
img.set_colorkey((0,0,0), pygame.RLEACCEL)
r = self._positionrect(img, center, pos)
return [img, r]
def image_at(self, rect, colorkey=None, x_mirror=False, y_mirror=False, rotation=0):
'''
Load image at rect
'''
rect = pygame.Rect(rect)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if x_mirror or y_mirror:
image = pygame.transform.flip(image, x_mirror, y_mirror)
if rotation > 0:
image = pygame.transform.rotate(image, rotation)
# Determine the color key after transforms. Fix for weird graphics issue
# in pygame_sdl2 refs #62
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
def todo_test_set_alpha(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.set_alpha:
# Surface.set_alpha(value, flags=0): return None
# Surface.set_alpha(None): return None
# set the alpha value for the full Surface image
#
# Set the current alpha value fo r the Surface. When blitting this
# Surface onto a destination, the pixels will be drawn slightly
# transparent. The alpha value is an integer from 0 to 255, 0 is fully
# transparent and 255 is fully opaque. If None is passed for the alpha
# value, then the Surface alpha will be disabled.
#
# This value is different than the per pixel Surface alpha. If the
# Surface format contains per pixel alphas, then this alpha value will
# be ignored. If the Surface contains per pixel alphas, setting the
# alpha value to None will disable the per pixel transparency.
#
# The optional flags argument can be set to pygame.RLEACCEL to provide
# better performance on non accelerated displays. An RLEACCEL Surface
# will be slower to modify, but quicker to blit as a source.
#
s = pygame.Surface((1,1), SRCALHPA, 32)
s.fill((1, 2, 3, 4))
s.set_alpha(None)
self.failUnlessEqual(s.get_at((0, 0)), (1, 2, 3, 255))
self.fail()
def image_at(self, rectangle, colorkey=None):
"""Loads image from x,y,x+offset,y+offset"""
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
# Load a whole bunch of images and return them as a list
def text(self, color, text, center=None, pos='center', bgd=(0,0,0)):
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img = self.font.render(text, 1, color, bgd)
img.set_colorkey(bgd, pygame.RLEACCEL)
else:
img = self.font.render(text, 0, color)
except (pygame.error, TypeError):
img = pygame.Surface((10, 10))
img = img.convert()
r = self._positionrect(img, center, pos)
return [img, r]
def image_at(self, rectangle, colorkey = None):
"Loads image from x,y,x+offset,y+offset"
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
# Load a whole bunch of images and return them as a list
def todo_test_set_alpha(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.set_alpha:
# Surface.set_alpha(value, flags=0): return None
# Surface.set_alpha(None): return None
# set the alpha value for the full Surface image
#
# Set the current alpha value fo r the Surface. When blitting this
# Surface onto a destination, the pixels will be drawn slightly
# transparent. The alpha value is an integer from 0 to 255, 0 is fully
# transparent and 255 is fully opaque. If None is passed for the alpha
# value, then the Surface alpha will be disabled.
#
# This value is different than the per pixel Surface alpha. If the
# Surface format contains per pixel alphas, then this alpha value will
# be ignored. If the Surface contains per pixel alphas, setting the
# alpha value to None will disable the per pixel transparency.
#
# The optional flags argument can be set to pygame.RLEACCEL to provide
# better performance on non accelerated displays. An RLEACCEL Surface
# will be slower to modify, but quicker to blit as a source.
#
s = pygame.Surface((1,1), SRCALHPA, 32)
s.fill((1, 2, 3, 4))
s.set_alpha(None)
self.failUnlessEqual(s.get_at((0, 0)), (1, 2, 3, 255))
self.fail()
def load_image(path, transparent):
try: image = pygame.image.load(path)
# Manages error if image cannot be loaded
except (pygame.error) as message:
raise(message)
# Converting to inner pygame format (more efficient)
image = image.convert()
if transparent:
color = image.get_at((0, 0))
image.set_colorkey(color, pygame.RLEACCEL)
return image
# Function to manage texts