这是一个使用smoothscale的快速测试。它开始于一个2x2的彩色位图,然后在绘制结果之前任意缩放。
虽然PyGame会利用可用的硬件来进行拉伸,但我不认为这样很快。
import pygame
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Gradient Rect")
def gradientRect( window, left_colour, right_colour, target_rect ):
""" Draw a horizontal-gradient filled rectangle covering <target_rect> """
colour_rect = pygame.Surface( ( 2, 2 ) )
pygame.draw.line( colour_rect, left_colour, ( 0,0 ), ( 0,1 ) )
pygame.draw.line( colour_rect, right_colour, ( 1,0 ), ( 1,1 ) )
colour_rect = pygame.transform.smoothscale( colour_rect, ( target_rect.width, target_rect.height ) )
window.blit( colour_rect, target_rect )
clock = pygame.time.Clock()
finished = False
while not finished:
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
window.fill( ( 0,0,0 ) )
gradientRect( window, (0, 255, 0), (0, 100, 0), pygame.Rect( 100,100, 100, 50 ) )
gradientRect( window, (255, 255, 0), (0, 0, 255), pygame.Rect( 100,200, 128, 64 ) )
pygame.display.flip()
clock.tick_busy_loop(60)
pygame.quit()
smoothscale
来拉伸它到所需大小。这将创建一个渐变。 - Kingsleysmoothscale
已经记录在案,并且在pygame文档中有使用它的示例。 - martineau