/
test_blit_matrix.py
109 lines (70 loc) · 2.25 KB
/
test_blit_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
try:
import pygame_sdl2
pygame_sdl2.import_as_pygame()
except:
pass
import pygame
A = 255
def bottom_pattern(s):
s.subsurface((0, 0, 150, 50)).fill((255, 255, 0, A))
s.subsurface((0, 100, 150, 50)).fill((255, 255, 0, A))
def top_pattern(s):
s.subsurface((15, 15, 120, 120)).fill((255, 0, 255, A))
s.subsurface((30, 30, 90, 90)).fill((0, 255, 255, A))
def case(src_alpha, dst_alpha, colorkey, alpha):
if src_alpha:
src = pygame.Surface((150, 150), pygame.SRCALPHA)
else:
src = pygame.Surface((150, 150), 0)
if dst_alpha:
dst = pygame.Surface((150, 150), pygame.SRCALPHA)
else:
dst = pygame.Surface((150, 150), 0)
top_pattern(src)
bottom_pattern(dst)
if colorkey:
src.set_colorkey((255, 0, 255, A))
if alpha:
src.set_alpha(128)
dst.blit(src, (0, 0))
return dst
PATTERN = [ (False, False), (False, True), (True, False), (True, True) ]
def draw():
screen = pygame.display.get_surface()
f = pygame.font.Font(None, 18)
screen.fill((128, 128, 128, 255))
for xi, (src_alpha, dst_alpha) in enumerate(PATTERN):
for yi, (colorkey, alpha) in enumerate(PATTERN):
surf = case(src_alpha, dst_alpha, colorkey, alpha)
screen.blit(surf, (xi * 250 + 50, yi * 250 + 50))
s = "src={}, dst={}".format(
"RGBA" if src_alpha else "RGB",
"RGBA" if dst_alpha else "RGB",
)
surf = f.render(s, True, (255, 255, 255, 255))
screen.blit(surf, (xi * 250 + 50, yi * 250 + 30))
s = ""
if colorkey:
s += "colorkey "
if alpha:
s += "alpha "
surf = f.render(s, True, (255, 255, 255, 255))
screen.blit(surf, (xi * 250 + 50, yi * 250 + 200))
pygame.display.flip()
def main():
global A
pygame.init()
screen = pygame.display.set_mode((1000, 1000))
draw()
while True:
ev = pygame.event.wait()
if ev.type == pygame.KEYDOWN:
if A == 255:
A = 128
else:
A = 255
draw()
if ev.type == pygame.QUIT:
break
if __name__ == "__main__":
main()