2017-01-13 4 views
1

파이 게임에서 반원을 그리는 방법이 있습니까? 이런 식으로 뭔가 :파이 게임에서 반원 그리기

semicircle

pygame.surface.set_clip()이 작동하지 않습니다 -이처럼뿐만 아니라 원형 조각처럼 원이 필요합니다

pie slice

+0

는 TigerhawkT3 @ I "는 다른 방법을 설명하기 위해"질문을 편집했다. 열어 주시겠습니까? –

+0

괜찮은 다각형은 어떻습니까? http://stackoverflow.com/questions/23246185/python-draw-pie-shapes-with-colour-filled – dodell

답변

1

PyGame 생성하는 기능이 없습니다 arc/pie으로 채우지 만 PIL/pillow을 사용하면 pieslice으로 비트 맵을 생성하고 PyGame 이미지로 변환하여 표시 할 수 있습니다.

import pygame 
#import pygame.gfxdraw 
from PIL import Image, ImageDraw 

# --- constants --- 

BLACK = ( 0, 0, 0) 
WHITE = (255, 255, 255) 
BLUE = ( 0, 0, 255) 
GREEN = ( 0, 255, 0) 
RED = (255, 0, 0) 
GREY = (128, 128, 128) 

#PI = 3.1415 

# --- main ---- 

pygame.init() 
screen = pygame.display.set_mode((800,600)) 

# - generate PIL image with transparent background - 

pil_size = 300 

pil_image = Image.new("RGBA", (pil_size, pil_size)) 
pil_draw = ImageDraw.Draw(pil_image) 
#pil_draw.arc((0, 0, pil_size-1, pil_size-1), 0, 270, fill=RED) 
pil_draw.pieslice((0, 0, pil_size-1, pil_size-1), 330, 0, fill=GREY) 

# - convert into PyGame image - 

mode = pil_image.mode 
size = pil_image.size 
data = pil_image.tobytes() 

image = pygame.image.fromstring(data, size, mode) 

image_rect = image.get_rect(center=screen.get_rect().center) 

# - mainloop - 

clock = pygame.time.Clock() 
running = True 

while running: 

    clock.tick(10) 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       running = False 

    screen.fill(WHITE) 
    #pygame.draw.arc(screen, BLACK, (300, 200, 200, 200), 0, PI/2, 1) 
    #pygame.gfxdraw.pie(screen, 400, 300, 100, 0, 90, RED) 
    #pygame.gfxdraw.arc(screen, 400, 300, 100, 90, 180, GREEN) 

    screen.blit(image, image_rect) # <- display image 

    pygame.display.flip() 

# - end - 

pygame.quit() 

결과 :

enter image description here

GitHub의는 : furas/python-examples/pygame/pillow-image-pieslice