Skip to content

Commit a0ab55d

Browse files
committed
pygame-pillow-pieslice
1 parent ffb288a commit a0ab55d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
## example-1.py
3+
4+
`PyGame` has no function to create filled `arc`/`pie` but we can use `PIL/pillow` to generate transparent bitmap with `pieslice` and convert to `PyGame` image to display it.
5+
6+
![#1](screenshots/image-1.png?raw=true)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
import pygame
4+
import pygame.gfxdraw
5+
from PIL import Image, ImageDraw
6+
7+
# --- constants ---
8+
9+
BLACK = ( 0, 0, 0)
10+
WHITE = (255, 255, 255)
11+
BLUE = ( 0, 0, 255)
12+
GREEN = ( 0, 255, 0)
13+
RED = (255, 0, 0)
14+
GREY = (128, 128, 128)
15+
16+
PI = 3.1415
17+
18+
# --- main ----
19+
20+
pygame.init()
21+
screen = pygame.display.set_mode((800,600))
22+
23+
# - generate PIL image -
24+
25+
pil_size = 300
26+
27+
pil_image = Image.new("RGBA", (pil_size, pil_size))
28+
pil_draw = ImageDraw.Draw(pil_image)
29+
#pil_draw.arc((0, 0, pil_size-1, pil_size-1), 0, 270, fill=RED)
30+
pil_draw.pieslice((0, 0, pil_size-1, pil_size-1), 330, 0, fill=GREY)
31+
32+
# - convert to PyGame image -
33+
34+
mode = pil_image.mode
35+
size = pil_image.size
36+
data = pil_image.tobytes()
37+
38+
image = pygame.image.fromstring(data, size, mode)
39+
image_rect = image.get_rect(center=screen.get_rect().center)
40+
41+
# - mainloop -
42+
43+
clock = pygame.time.Clock()
44+
running = True
45+
46+
while running:
47+
48+
clock.tick(10)
49+
50+
for event in pygame.event.get():
51+
if event.type == pygame.QUIT:
52+
running = False
53+
if event.type == pygame.KEYDOWN:
54+
if event.key == pygame.K_ESCAPE:
55+
running = False
56+
57+
screen.fill(RED)
58+
#pygame.draw.arc(screen, BLACK, (300, 200, 200, 200), 0, PI/2, 1)
59+
#pygame.gfxdraw.pie(screen, 400, 300, 100, 0, 90, RED)
60+
#pygame.gfxdraw.arc(screen, 400, 300, 100, 90, 180, GREEN)
61+
screen.blit(image, image_rect)
62+
pygame.display.flip()
63+
64+
# - end -
65+
66+
pygame.quit()
Loading

0 commit comments

Comments
 (0)