示例用绿色填充背景,然后用红色矩形/多边形绘制表面。
先用convert_alpha(),再填充fill([0,0,0,0]),就可以画画了。
在此版本中,您可以使用颜色[R,G,B,Alpha] 为每个像素设置不同的透明度。但是您不能使用set_alpha(128) 来同时更改所有像素的透明度。
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 25
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
surface = pygame.surface.Surface(screen.get_size()).convert_alpha()
surface.fill([0,0,0,0])
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
# --- draws ---
screen.fill(GREEN)
screen.blit(surface, (0,0))
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
#pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
fps = clock.get_fps()
pygame.display.set_caption('FPS: {}'.format(fps))
# --- end ---
pygame.quit()
或
使用set_colorkey([0,0,0]) 和blit() 不会复制颜色为[0,0,0] 的像素。
在这个版本中,您可以使用set_alpha(128) 为所有像素设置相同的透明度,但您不能使用颜色[R,G,B,Alpha] 为每个像素分别设置不同的透明度。
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 25 # for more than 220 it has no time to update screen
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0]) # don't copy color [0,0,0] on screen
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
# --- draws ---
screen.fill(GREEN)
#surface.set_alpha(128)
screen.blit(surface, (0,0))
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
#pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
fps = clock.get_fps()
pygame.display.set_caption('FPS: {}'.format(fps))
# --- end ---
pygame.quit()
编辑:
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 25 # for more than 220 it has no time to update screen
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255,255,0)
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
# ---
background = pygame.surface.Surface(screen.get_size())
color = (128,128,128)
for x in range(0, SCREEN_WIDTH, 40):
for y in range(0, SCREEN_HEIGHT, 40):
pygame.draw.rect(background, color, [x,y,40,40])
if color == (128,128,128):
color = (192,192,192)
else:
color = (128,128,128)
# ---
surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0]) # don't copy color [0,0,0] on screen
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])
pygame.draw.polygon(surface, GREEN, [(100,300), (200,200), (300,300)])
pygame.draw.polygon(surface, BLUE, [(100,100), (200,200), (100,300)])
pygame.draw.polygon(surface, YELLOW, [(300,100), (200,200), (300,300)])
surface.set_alpha(128) # semi transparent all pixels
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
# --- draws ---
screen.blit(background, (0,0))
screen.blit(surface, (0,0))
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
#pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
fps = clock.get_fps()
pygame.display.set_caption('FPS: {}'.format(fps))
# --- end ---
pygame.quit()