def todo_test_lock(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.lock:
# Surface.lock(): return None
# lock the Surface memory for pixel access
#
# Lock the pixel data of a Surface for access. On accelerated
# Surfaces, the pixel data may be stored in volatile video memory or
# nonlinear compressed forms. When a Surface is locked the pixel
# memory becomes available to access by regular software. Code that
# reads or writes pixel values will need the Surface to be locked.
#
# Surfaces should not remain locked for more than necessary. A locked
# Surface can often not be displayed or managed by Pygame.
#
# Not all Surfaces require locking. The Surface.mustlock() method can
# determine if it is actually required. There is no performance
# penalty for locking and unlocking a Surface that does not need it.
#
# All pygame functions will automatically lock and unlock the Surface
# data as needed. If a section of code is going to make calls that
# will repeatedly lock and unlock the Surface many times, it can be
# helpful to wrap the block inside a lock and unlock pair.
#
# It is safe to nest locking and unlocking calls. The surface will
# only be unlocked after the final lock is released.
#
self.fail()
python类Surface()的实例源码
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 test_unmap_rgb(self):
# Special case, 8 bit-per-pixel surface (has a palette).
surf = pygame.Surface((2, 2), 0, 8)
c = (1, 1, 1) # Unlikely to be in a default palette.
i = 67
pygame.init()
try:
pygame.display.set_mode((100, 50))
surf.set_palette_at(i, c)
unmapped_c = surf.unmap_rgb(i)
self.failUnlessEqual(unmapped_c, c)
# Confirm it is a Color instance
self.failUnless(isinstance(unmapped_c, pygame.Color))
finally:
pygame.quit()
# Remaining, non-pallete, cases.
c = (128, 64, 12, 255)
formats = [(0, 16), (0, 24), (0, 32),
(SRCALPHA, 16), (SRCALPHA, 32)]
for flags, bitsize in formats:
surf = pygame.Surface((2, 2), flags, bitsize)
unmapped_c = surf.unmap_rgb(surf.map_rgb(c))
surf.fill(c)
comparison_c = surf.get_at((0, 0))
self.failUnlessEqual(unmapped_c, comparison_c,
"%s != %s, flags: %i, bitsize: %i" %
(unmapped_c, comparison_c, flags, bitsize))
# Confirm it is a Color instance
self.failUnless(isinstance(unmapped_c, pygame.Color))
def test_copy(self):
"""Ensure method copy() preserves the surface's class
When Surface is subclassed, the inherited copy() method will return
instances of the subclass. Non Surface fields are uncopied, however.
This includes instance attributes.
"""
ms1 = self.MySurface((32, 32), pygame.SRCALPHA, 32)
ms2 = ms1.copy()
self.assertTrue(isinstance(ms2, self.MySurface))
self.assertTrue(ms1.an_attribute)
self.assertRaises(AttributeError, getattr, ms2, "an_attribute")
def test_subsurface(self):
"""Ensure method subsurface() preserves the surface's class
When Surface is subclassed, the inherited subsurface() method will
return instances of the subclass. Non Surface fields are uncopied,
however. This includes instance attributes.
"""
ms1 = self.MySurface((32, 32), pygame.SRCALPHA, 32)
ms2 = ms1.subsurface((4, 5, 10, 12))
self.assertTrue(isinstance(ms2, self.MySurface))
self.assertTrue(ms1.an_attribute)
self.assertRaises(AttributeError, getattr, ms2, "an_attribute")
def test_GET_PIXELVALS(self):
# surface.h GET_PIXELVALS bug regarding whether of not
# a surface has per-pixel alpha. Looking at the Amask
# is not enough. The surface's SRCALPHA flag must also
# be considered. Fix rev. 1923.
src = self._make_surface(32, srcalpha=True)
src.fill((0, 0, 0, 128))
src.set_alpha(None) # Clear SRCALPHA flag.
dst = self._make_surface(32, srcalpha=True)
dst.blit(src, (0, 0), special_flags=BLEND_RGBA_ADD)
self.failUnlessEqual(dst.get_at((0, 0)), (0, 0, 0, 255))
def setUp(self):
# Needed for 8 bits-per-pixel color palette surface tests.
pygame.init()
def test_overlap_check(self):
# Ensure overlapping blits are properly detected. There are two
# places where this is done, within SoftBlitPyGame() in alphablit.c
# and PySurface_Blit() in surface.c. SoftBlitPyGame should catch the
# per-pixel alpha surface, PySurface_Blit the colorkey and blanket
# alpha surface. per-pixel alpha and blanket alpha self blits are
# not properly handled by SDL 1.2.13, so Pygame does them.
bgc = (0, 0, 0, 255)
rectc_left = (128, 64, 32, 255)
rectc_right = (255, 255, 255, 255)
colors = [(255, 255, 255, 255), (128, 64, 32, 255)]
overlaps = [(0, 0, 1, 0, (50, 0)),
(0, 0, 49, 1, (98, 2)),
(0, 0, 49, 49, (98, 98)),
(49, 0, 0, 1, (0, 2)),
(49, 0, 0, 49, (0, 98))]
surfs = [pygame.Surface((100, 100), SRCALPHA, 32)]
surf = pygame.Surface((100, 100), 0, 32)
surf.set_alpha(255)
surfs.append(surf)
surf = pygame.Surface((100, 100), 0, 32)
surf.set_colorkey((0, 1, 0))
surfs.append(surf)
for surf in surfs:
for s_x, s_y, d_x, d_y, test_posn in overlaps:
surf.fill(bgc)
surf.fill(rectc_right, (25, 0, 25, 50))
surf.fill(rectc_left, (0, 0, 25, 50))
surf.blit(surf, (d_x, d_y), (s_x, s_y, 50, 50))
self.failUnlessEqual(surf.get_at(test_posn), rectc_right)
def __init__(self, size=(640, 480), xlim=None, ylim=None):
pygame.init()
screen = pygame.display.set_mode(size)
#surface = pygame.surface(size, pygame.SRCALPHA)
if xlim is None:
xlim = (0, size[0])
if ylim is None:
ylim = (0, size[1])
self._screen = screen
#self._surface = surface
#self.screen.blit(self.surface, (0, 0))
self._xlim = xlim
self._ylim = ylim
def rect(self, color, center, size):
cx, cy = self.scale_point(center)
w, h = self.scale_size(size)
if len(color) > 3:
s = pygame.Surface((w, h), pygame.SRCALPHA)
s.fill(color)
self.screen.blit(s, (cx-w/2, cy-h/2))
#pygame.draw.rect(self.surface, color, pygame.Rect(cx-w/2, cy-h/2, w, h))
else:
pygame.draw.rect(self.screen, color, pygame.Rect(cx-w/2, cy-h/2, w, h))