Is there a way to draw a semicircle in Pygame? Something like this:
pygame.surface.set_clip()
will not work for this – I need circles that look like pie slices as well, like this one:
Advertisement
Answer
PyGame
has no function to create filled arc/pie
but you can use PIL/pillow
to generate bitmap with pieslice
and convert to PyGame
image to display it.
JavaScript
x
68
68
1
import pygame
2
#import pygame.gfxdraw
3
from PIL import Image, ImageDraw
4
5
# --- constants ---
6
7
BLACK = ( 0, 0, 0)
8
WHITE = (255, 255, 255)
9
BLUE = ( 0, 0, 255)
10
GREEN = ( 0, 255, 0)
11
RED = (255, 0, 0)
12
GREY = (128, 128, 128)
13
14
#PI = 3.1415
15
16
# --- main ----
17
18
pygame.init()
19
screen = pygame.display.set_mode((800,600))
20
21
# - generate PIL image with transparent background -
22
23
pil_size = 300
24
25
pil_image = Image.new("RGBA", (pil_size, pil_size))
26
pil_draw = ImageDraw.Draw(pil_image)
27
#pil_draw.arc((0, 0, pil_size-1, pil_size-1), 0, 270, fill=RED)
28
pil_draw.pieslice((0, 0, pil_size-1, pil_size-1), 330, 0, fill=GREY)
29
30
# - convert into PyGame image -
31
32
mode = pil_image.mode
33
size = pil_image.size
34
data = pil_image.tobytes()
35
36
image = pygame.image.fromstring(data, size, mode)
37
38
image_rect = image.get_rect(center=screen.get_rect().center)
39
40
# - mainloop -
41
42
clock = pygame.time.Clock()
43
running = True
44
45
while running:
46
47
clock.tick(10)
48
49
for event in pygame.event.get():
50
if event.type == pygame.QUIT:
51
running = False
52
if event.type == pygame.KEYDOWN:
53
if event.key == pygame.K_ESCAPE:
54
running = False
55
56
screen.fill(WHITE)
57
#pygame.draw.arc(screen, BLACK, (300, 200, 200, 200), 0, PI/2, 1)
58
#pygame.gfxdraw.pie(screen, 400, 300, 100, 0, 90, RED)
59
#pygame.gfxdraw.arc(screen, 400, 300, 100, 90, 180, GREEN)
60
61
screen.blit(image, image_rect) # <- display image
62
63
pygame.display.flip()
64
65
# - end -
66
67
pygame.quit()
68
Result: