PyGame 모듈은 다양한 기능을 지원하는데, 그 중에서 여러가지 도형을 그릴 수 있는 메소드들을 소개하도록 하겠습니다.



00. PyGame 지원되는 도형

PyGame에서 지원되는 도형은 다음과 같습니다.

메소드

도형

설명

 pygame.draw.rect

 사각형

 화면에 사각형을 그려줍니다.

 pygame.draw.polygon

 삼각형

 화면에 삼각혁을 그려줍니다.

 pygame.draw.circle

 원

 화면에 원을 그려줍니다.

 pygame.draw.ellipse

 타원

 화면에 타원을 그려줍니다.

 pygame.draw.arc

 원

 화면에 원하는 만큼의 원을 그려줍니다.

 원을 얼마나 그려줄지 정할 수 있습니다.

 pygame.draw.line

 선

 화면에 선을 그려줍니다.

 pygame.draw.lines

 여러 개의 선

 화면에 여러 개의 선을 이어서 그립니다.

 pygame.draw.aaline

 부드러운 선

 화면에 부드러운 선을 그려줍니다.

 pygame.draw.aalines

 부드러운 선들

 화면에 부드러운 선을 여러 개 이어서 그립니다.


여기서는 pygame.draw.arc를 이용하여 타원을 그리는 방법을 살펴볼 것입니다.

만약 다른 draw 메소드를 참고하고 싶으시다면 아래의 링크에서 선택하시면 될 것 같습니다.




사각형 - pygame.draw.rect

삼각형 - pygame.draw.polygon

원 - pygame.draw.circle

타원 - pygame.draw.ellipse

선 - pygame.draw.line

여러 개의 선 - pygame.draw.lines

부드러운 선 - pygame.draw.aaline

여러 개의 부드러운 선 - pygame.draw.aalines




01. pygame.draw.arc 함수

pygame에서 지원하는 타원 그리기 함수인 arc 함수는 다음과 같은 인자값들을 받습니다.



 pygame.draw.arc(Surface, Color, Rect, Start_angle, Stop_angle, Width=1)

  Draws an elliptical arc on the Surface. The rect argument is the area that the ellipse will fill. The two angle arguments are the initial and final angle in radians, with the zero on the right. The width argument is the thickness to draw the outer edge.




변수 

설명

Surface

 pygame을 실행할 때 전체적으로 화면을 선언한 변수 값

Color

 원의 색깔로 (R, G, B)의 형태로 데이터의 값을 삽입함

Rect

 원의 [x축, y축, 가로, 세로]의 형태로 삽입함

Start_angle

 원을 그릴 시작 위치(0 혹은 원주율인 pi를 기준으로 입력함)

Stop_angle

 원을 그릴 끝 위치(0 혹은 원주율인 pi를 기준으로 입력함)

Width

 타원의 선 크기를 말하며, 기본적으로 0으로 설정됨


위의 변수들은 예제 소스코드를 통해 설명하도록 하겠습니다.



02. pygame.draw.arc를 이용한 원 그리기(예제)

먼저 소스코드를 살펴보면 다음과 같습니다.


Source

# Import a library of functions called 'pygame'
import pygame
from math import pi

# Initialize the game engine
pygame.init()
 
# Define the colors we will use in RGB format
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE  = (  0,   0, 255)
GREEN = (  0, 255,   0)
RED   = (255,   0,   0)
 
# Set the height and width of the screen
size   = [400, 300]
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("Drawing Rectangle")
 
#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
 
while not done:
 
    # This limits the while loop to a max of 10 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(10)
     
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
 
    # All drawing code happens after the for loop and but
    # inside the main while done==False loop.
     
    # Clear the screen and set the screen background
    screen.fill(WHITE)

    # Draw an arc as part of an ellipse. 
    # Use radians to determine what angle to draw.
    # pi : 3.141592653589793
    pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 5)
    pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
    pygame.draw.arc(screen, RED,  [210, 75, 150, 125], 3*pi/2, 2*pi, 2)
     
    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()
 
# Be IDLE friendly
pygame.quit()


Variable - Surface

# Set the height and width of the screen
size   = [400, 300]
screen = pygame.display.set_mode(size)

먼저, 17번째 줄의 screen 변수를 보면, 해당 변수에는 화면 전체를 설정하기 위한 값이 들어가 있습니다.

화면을 새로고침 해주거나, 화면을 채워주거나 할 때 해당 변수를 이용합니다.

pygame.draw.arc 함수의 Surface는 이러한 화면 설정 변수를 넣어주시면 됩니다.


Variable - Color

# Define the colors we will use in RGB format
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE  = (  0,   0, 255)
GREEN = (  0, 255,   0)
RED   = (255,   0,   0)

다음으로, 9 ~ 13번 라인을 보시면, Tuple 형태의 값이 설정되어 있는 것을 보실 수 있습니다.

세 개의 값이 0 ~ 255의 범위를 가지게 되는데, 이렇게 설정된 값이 색깔(RGB) 값으로 Color 변수가 들어갈 곳에 넣어주시면 됩니다.


Variable - Rect

    # Draw an arc as part of an ellipse. 
    # Use radians to determine what angle to draw.
    # pi : 3.141592653589793
    pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2)
    pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
    pygame.draw.arc(screen, RED,  [210, 75, 150, 125], 3*pi/2, 2*pi, 2)

먼저 44번째 라인부터 47번째 라인을 살펴보면 리스트 형태의 값이 인자값으로 사용된 것을 보실 수 있습니다.

모두 같은 값으로 되어 있습니다. 해당 Rect 변수에 들어갈 값인 [210, 75, 150, 125]으로 된 값을 살펴보면 [x, y, w, h]라고 설정된 값이 들어가 있다고 보시면 됩니다.

즉, x축의 위치는 210, y축의 위치는 75, 가로는 150, 세로는 125이라고 보시면 됩니다.


단, 여기서 원을 그리는데 필요한 변수가 왜 Rect(사각형)라고 되어있는가에 대한 의문이 들 수 있으실 것입니다.

이는 다음과 같은 그림으로 예시를 들 수 있겠습니다.


위의 그림을 설명하면 다음과 같습니다.

먼저 사각형 Rect 변수의 값을 통해 사각형을 그릴 위치와 가로, 세로 값을 정해줍니다.

그 다음 우리가 그릴 원을 그려주는 것입니다.


Variable -Start_angle, Stop_angle

    # Draw an arc as part of an ellipse. 
    # Use radians to determine what angle to draw.
    # pi : 3.141592653589793
    pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2)
    pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
    pygame.draw.arc(screen, RED,  [210, 75, 150, 125], 3*pi/2, 2*pi, 2)

다음으로 원을 원을 그리기 위한 첫 번째 위치인 시작 위치(Start_angle)와 끝 위치(Stop_angle)를 살펴봅시다.

위의 그림으로 설명드리면, 시작 위치는 0, pi/2, 3*pi/2, 2*pi인 네 가지가 있다고 봅시다.

4개의 위치를 기반으로 시작 위치를 잡아주고, 끝 위치도 잡아주시면 됩니다.

각각의 색깔별로 설명드리면 다음과 같습니다.

검정색으로 그릴 원은 0 ~ pi/2로 시계방향으로 원을 그렸을 때 0에서 1/4만큼 그려주는 것입니다.

빨간색으로 그릴 원은 pi/2 ~ pi로 시계방향으로 원을 그렸을 때 1/4에서 2/4만큼 그려주는 것입니다.

파란색으로 그릴 원은 pi ~ 3*pi/2로 시계방향으로 원을 그렸을 때 2/4에서 3/4만큼 그려주는 것입니다.

초록색으로 그릴 원은 3*pi/2 ~ 2*pi로 시계방향으로 원을 그렸을 때 3/4에서 4/4만큼 그려주는 것입니다.


Variable - Width

    # Draw an arc as part of an ellipse. 
    # Use radians to determine what angle to draw.
    # pi : 3.141592653589793
    pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2)
    pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2)
    pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2)
    pygame.draw.arc(screen, RED,  [210, 75, 150, 125], 3*pi/2, 2*pi, 2)

다음으로 두 타원 함수를 비교해보도록 하겠습니다.

44번째 라인과 47번째 라인을 보시면 선 두께를 2로 고정해준 것을 볼 수 있습니다.

Arc 함수의 경우 기본적으로 Width의 크기가 1이기 때문에 그려주는 원의 부분만 색깔 채움을 적용할 수 없습니다.

즉, 반드시 선으로만 그릴 수 있는 원이라는 의미입니다.





+ Recent posts