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.polygon을 이용하여 삼각형을 그리는 방법을 살펴볼 것입니다.

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




사각형 - pygame.draw.rect

원 - pygame.draw.circle

타원 - pygame.draw.ellipse

원(특정 부분까지 그리기) - pygame.draw.arc

선 - pygame.draw.line

여러 개의 선 - pygame.draw.lines

부드러운 선 - pygame.draw.aaline

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




01. pygame.draw.polygon 함수

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



 pygame.draw.polygon(Surface, Color, PointList, Width=0)

  Draws a polygonal shape on the Surface. The pointlist argument is the vertices of the polygon. The width argument is the thickness to draw the outer edge. If width is zero then the polygon will be filled.

  For aapolygon, use aalines with the 'closed' parameter.




변수 

설명

Surface

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

Color

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

PointList

 삼각형의 세 개의 점을 [[x축, y축], [x축, y축], [x축, y축]의 형태로 삽입함

Width

 삼각형의 선 크기를 말하며, 기본적으로 0으로 설정됨


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



02. pygame.draw.polygon을 이용한 삼각형 그리기(예제)

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


Source

# Import a library of functions called 'pygame'
import pygame
 
# 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)

    # This draws a triangle using the polygon command
    # Draw a polygon outline
    pygame.draw.polygon(screen, BLACK, [[50, 50], [0, 100], [100, 100]], 5)

    # Draw a solid polygon
    pygame.draw.polygon(screen, BLACK, [[200, 50], [150, 100], [250, 100]])
     
    # 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)

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

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

pygame.draw.polygon 함수의 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)

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

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


Variable - PointList

    # This draws a triangle using the polygon command
    # Draw a polygon outline
    pygame.draw.polygon(screen, BLACK, [[50, 50], [0, 100], [100, 100]], 5)

    # Draw a solid polygon
    pygame.draw.polygon(screen, BLACK, [[200, 50], [150, 100], [250, 100]])

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

42번째 라인의 값을 예시로 살펴보면, [[50, 50], [0, 100], [100, 100]]으로 된 값은 세 개의 점을 나타냅니다.

첫 번째 점의 x좌표와 y좌표 : [50, 50]

두 번째 점의 x좌표와 y좌표 : [0, 100]

세 번째 점의 x좌표와 y좌표 : [100, 100]

이렇게 세 점의 값을 리스트 형태로 묶어서 인자값으로 설정해주시면 됩니다.


Variable - Width

    # This draws a triangle using the polygon command
    # Draw a polygon outline
    pygame.draw.polygon(screen, BLACK, [[50, 50], [0, 100], [100, 100]], 5)

    # Draw a solid polygon
    pygame.draw.polygon(screen, BLACK, [[200, 50], [150, 100], [250, 100]])

다음으로 두 삼각형 함수를 비교해보도록 하겠습니다.

42번째 라인과 45번째 라인을 보시면 두 가지 방법으로 삼각형을 그린 것을 볼 수 있습니다.

42번째 라인의 삼각형은 Width의 값을 5로 변경해준 것이고, 45번째 라인의 삼각형은 값을 따로 지정해주지 않아 기본으로 지정된 Width=0을 사용하겠다고하여 변수의 값을 따로 지정해주지 않은 형태입니다.

만약 Width의 값을 지정해주게 되면, 색깔 채움 없는 빈 상자로 그려주는 것을 의미합니다.

즉, 삼각형의 선의 두께를 지정해준 것과 같은 것입니다.

그리고 45번째 라인의 삼각형은 Width의 값을 따로 지정해주지 않아 0으로 사용함을 의미합니다.

이는 삼각형의 바깥 선의 두께를 지정해주지 않고, 삼각형에 색을 채운 채로 그리는 것을 의미합니다.


42번째 라인의 삼각형첫 번째 하얀 삼각형이고,

45번째 라인의 삼각형두 번째 검은 삼각형입니다.



42번째 라인의 삼각형의 선 두께가 5로 지정되어 있으니, 꽤 두꺼운 형태로 출력이되었습니다.

또한 45번째 라인의 삼각형은 선이 따로 없는 검은색으로 채운 삼각형이 출력됩니다.


+ Recent posts