我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用pygame.BLEND_RGBA_MAX。
def test_blend_rgba(self): bitsizes = [16, 32] blends = ['BLEND_RGBA_ADD', 'BLEND_RGBA_SUB', 'BLEND_RGBA_MULT', 'BLEND_RGBA_MIN', 'BLEND_RGBA_MAX'] for bitsize in bitsizes: surf = self._make_surface(bitsize, srcalpha=True) comp = self._make_surface(bitsize, srcalpha=True) for blend in blends: self._fill_surface(surf) self._fill_surface(comp) comp.blit(surf, (3, 0), special_flags=getattr(pygame, blend)) surf.blit(surf, (3, 0), special_flags=getattr(pygame, blend)) self._assert_same(surf, comp)
def todo_test_blit(self): # __doc__ (as of 2008-08-02) for pygame.surface.Surface.blit: # Surface.blit(source, dest, area=None, special_flags = 0): return Rect # draw one image onto another # # Draws a source Surface onto this Surface. The draw can be positioned # with the dest argument. Dest can either be pair of coordinates # representing the upper left corner of the source. A Rect can also be # passed as the destination and the topleft corner of the rectangle # will be used as the position for the blit. The size of the # destination rectangle does not effect the blit. # # An optional area rectangle can be passed as well. This represents a # smaller portion of the source Surface to draw. # # An optional special flags is for passing in new in 1.8.0: BLEND_ADD, # BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX new in 1.8.1: # BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN, # BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT, # BLEND_RGB_MIN, BLEND_RGB_MAX With other special blitting flags # perhaps added in the future. # # The return rectangle is the area of the affected pixels, excluding # any pixels outside the destination Surface, or outside the clipping # area. # # Pixel alphas will be ignored when blitting to an 8 bit Surface. # special_flags new in pygame 1.8. self.fail()
def test_fill_blend_rgba(self): destinations = [self._make_surface(8), self._make_surface(16), self._make_surface(16, srcalpha=True), self._make_surface(24), self._make_surface(32), self._make_surface(32, srcalpha=True)] blend = [('BLEND_RGBA_ADD', (0, 25, 100, 255), lambda a, b: min(a + b, 255)), ('BLEND_RGBA_SUB', (0, 25, 100, 255), lambda a, b: max(a - b, 0)), ('BLEND_RGBA_MULT', (0, 7, 100, 255), lambda a, b: (a * b) // 256), ('BLEND_RGBA_MIN', (0, 255, 0, 255), min), ('BLEND_RGBA_MAX', (0, 255, 0, 255), max)] for dst in destinations: dst_palette = [dst.unmap_rgb(dst.map_rgb(c)) for c in self._test_palette] for blend_name, fill_color, op in blend: fc = dst.unmap_rgb(dst.map_rgb(fill_color)) self._fill_surface(dst) p = [] for dc in dst_palette: c = [op(dc[i], fc[i]) for i in range(4)] if not dst.get_masks()[3]: c[3] = 255 c = dst.unmap_rgb(dst.map_rgb(c)) p.append(c) dst.fill(fill_color, special_flags=getattr(pygame, blend_name)) self._assert_surface(dst, p, ", %s" % blend_name)
def rounded_rect(surface, rect, color, radius=0.4): """ rounded_rect(surface,rect,color,radius=0.4) surface : destination rect : rectangle color : rgb or rgba radius : 0 <= radius <= 1 """ rect = pygame.Rect(rect) color = pygame.Color(*color) alpha = color.a color.a = 0 pos = rect.topleft rect.topleft = 0, 0 rectangle = pygame.Surface(rect.size, pygame.SRCALPHA) circle = pygame.Surface([min(rect.size) * 3] * 2, pygame.SRCALPHA) pygame.draw.ellipse(circle, (0, 0, 0), circle.get_rect(), 0) circle = pygame.transform.smoothscale(circle, [int(min(rect.size) * radius)] * 2) radius = rectangle.blit(circle, (0, 0)) radius.bottomright = rect.bottomright rectangle.blit(circle, radius) radius.topright = rect.topright rectangle.blit(circle, radius) radius.bottomleft = rect.bottomleft rectangle.blit(circle, radius) rectangle.fill((0, 0, 0), rect.inflate(-radius.w, 0)) rectangle.fill((0, 0, 0), rect.inflate(0, -radius.h)) rectangle.fill(color, special_flags=pygame.BLEND_RGBA_MAX) rectangle.fill((255, 255, 255, alpha), special_flags=pygame.BLEND_RGBA_MIN) return surface.blit(rectangle, pos)
def filled_rounded_rect(surface,rect,color,radius=0.4): """ AAfilledRoundedRect(surface,rect,color,radius=0.4) surface : destination rect : rectangle color : rgb or rgba radius : 0 <= radius <= 1 """ rect = pygame.Rect(rect) color = pygame.Color(*color) alpha = color.a color.a = 0 pos = rect.topleft rect.topleft = 0,0 rectangle = pygame.Surface(rect.size,pygame.SRCALPHA) circle = pygame.Surface([min(rect.size)*3]*2,pygame.SRCALPHA) pygame.draw.ellipse(circle,(0,0,0),circle.get_rect(),0) circle = pygame.transform.smoothscale(circle,[int(min(rect.size)*radius)]*2) radius = rectangle.blit(circle,(0,0)) radius.bottomright = rect.bottomright rectangle.blit(circle,radius) radius.topright = rect.topright rectangle.blit(circle,radius) radius.bottomleft = rect.bottomleft rectangle.blit(circle,radius) rectangle.fill((0,0,0),rect.inflate(-radius.w,0)) rectangle.fill((0,0,0),rect.inflate(0,-radius.h)) rectangle.fill(color,special_flags=pygame.BLEND_RGBA_MAX) rectangle.fill((255,255,255,alpha),special_flags=pygame.BLEND_RGBA_MIN) return surface.blit(rectangle,pos)
def draw_roundrect(surface, rect, color, radius=0.4): rect = pygame.Rect(rect) color = pygame.Color(*color) alpha = color.a color.a = 0 pos = rect.topleft rect.topleft = 0, 0 rectangle = pygame.Surface(rect.size, pygame.SRCALPHA) circle = pygame.Surface([min(rect.size) * 3] * 2, pygame.SRCALPHA) pygame.draw.ellipse(circle, (0, 0, 0), circle.get_rect(), 0) circle = pygame.transform.smoothscale(circle, [int(min(rect.size) * radius)] * 2) radius = rectangle.blit(circle, (0, 0)) radius.bottomright = rect.bottomright rectangle.blit(circle, radius) radius.topright = rect.topright rectangle.blit(circle, radius) radius.bottomleft = rect.bottomleft rectangle.blit(circle, radius) rectangle.fill((0, 0, 0), rect.inflate(-radius.w, 0)) rectangle.fill((0, 0, 0), rect.inflate(0, -radius.h)) rectangle.fill(color, special_flags=pygame.BLEND_RGBA_MAX) rectangle.fill((255, 255, 255, alpha), special_flags=pygame.BLEND_RGBA_MIN) return surface.blit(rectangle, pos)
def test_fill(self): pygame.init() try: screen = pygame.display.set_mode((640, 480)) # Green and blue test pattern screen.fill((0, 255, 0), (0, 0, 320, 240)) screen.fill((0, 255, 0), (320, 240, 320, 240)) screen.fill((0, 0, 255), (320, 0, 320, 240)) screen.fill((0, 0, 255), (0, 240, 320, 240)) # Now apply a clip rect, such that only the left side of the # screen should be effected by blit opperations. screen.set_clip((0, 0, 320, 480)) # Test fills with each special flag, and additionaly without any. screen.fill((255, 0, 0, 127), (160, 0, 320, 30), 0) screen.fill((255, 0, 0, 127), (160, 30, 320, 30), pygame.BLEND_ADD) screen.fill((0, 127, 127, 127), (160, 60, 320, 30), pygame.BLEND_SUB) screen.fill((0, 63, 63, 127), (160, 90, 320, 30), pygame.BLEND_MULT) screen.fill((0, 127, 127, 127), (160, 120, 320, 30), pygame.BLEND_MIN) screen.fill((127, 0, 0, 127), (160, 150, 320, 30), pygame.BLEND_MAX) screen.fill((255, 0, 0, 127), (160, 180, 320, 30), pygame.BLEND_RGBA_ADD) screen.fill((0, 127, 127, 127), (160, 210, 320, 30), pygame.BLEND_RGBA_SUB) screen.fill((0, 63, 63, 127), (160, 240, 320, 30), pygame.BLEND_RGBA_MULT) screen.fill((0, 127, 127, 127), (160, 270, 320, 30), pygame.BLEND_RGBA_MIN) screen.fill((127, 0, 0, 127), (160, 300, 320, 30), pygame.BLEND_RGBA_MAX) screen.fill((255, 0, 0, 127), (160, 330, 320, 30), pygame.BLEND_RGB_ADD) screen.fill((0, 127, 127, 127), (160, 360, 320, 30), pygame.BLEND_RGB_SUB) screen.fill((0, 63, 63, 127), (160, 390, 320, 30), pygame.BLEND_RGB_MULT) screen.fill((0, 127, 127, 127), (160, 420, 320, 30), pygame.BLEND_RGB_MIN) screen.fill((255, 0, 0, 127), (160, 450, 320, 30), pygame.BLEND_RGB_MAX) # Update the display so we can see the results pygame.display.flip() # Compare colors on both sides of window y = 5 while y < 480: self.assertEquals(screen.get_at((10, y)), screen.get_at((330, 480 - y))) y += 10 finally: pygame.quit()
def getOverlay(self, rect): """ Draw the ShaderOverlay and return the result parameters: pygame.Rect bounds of the Overlay return values: pygame.Surface the result """ try: surface = pygame.Surface(rect.size, pygame.SRCALPHA, 32) except: return pygame.Surface((0, 0), pygame.SRCALPHA, 32) surface.fill(self._background) shapes = pygame.Surface(rect.size, pygame.SRCALPHA, 32) lightsources = pygame.Surface(rect.size, pygame.SRCALPHA, 32) removeAfter = [] for ls in self._lightsources: quality = self._lightsources[ls][3] intensity = self._lightsources[ls][2] color = self._lightsources[ls][1] radius = self._lightsources[ls][0] r = ls.rect polygon = [] try: for x in xrange(1, 2 * radius + r.w, quality): polygon.append(self.raycast(r.center, (r.left - radius + x, r.top - radius), rect, radius)) for y in xrange(1, 2 * radius + r.h, quality): polygon.append(self.raycast(r.center, (r.right + radius, r.top - radius + y), rect, radius)) for x in xrange(1, 2 * radius + r.w, quality): polygon.append(self.raycast(r.center, (r.right + radius - x, r.bottom + radius), rect, radius)) for y in xrange(1, 2 * radius + r.h, quality): polygon.append(self.raycast(r.center, (r.left - radius, r.bottom + radius - y), rect, radius)) except: removeAfter.append(ls) if polygon: pygame.draw.polygon(shapes, color, polygon) gradient = pygame.Surface(rect.size, pygame.SRCALPHA, 32) try: for n in range(2 * radius, 0, -1): alpha = intensity - int(intensity * (float(n) / (2 * radius))) pygame.draw.ellipse(gradient, color + (alpha,), r.inflate(n, n)) except: removeAfter.append(ls) shapes.blit(gradient, (0, 0), special_flags = pygame.BLEND_RGBA_MULT) lightsources.blit(shapes, (0, 0), special_flags = pygame.BLEND_RGBA_MAX) surface.blit(lightsources, (0, 0)) for ls in removeAfter: self.removeLightsource(ls) return surface