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.aaline을 이용하여 여러 개의 선을 그리는 방법을 살펴볼 것입니다.

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




사각형 - pygame.draw.rect

삼각형 - pygame.draw.polygon

원 - pygame.draw.circle

타원 - pygame.draw.ellipse

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

선 - pygame.draw.line

여러 개의 선 - pygame.draw.lines

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



01. pygame.draw.aaline 함수

pygame에서 지원하는 여러 개의 선 그리기 함수인 aaline 함수는 다음과 같은 인자값들을 받습니다.



 pygame.draw.aaline(Surface, Color, StartPos, EndPos, Blend=1)

  Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.




변수 

설명

Surface

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

Color

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

StartPos

 선이 시작하는 점을 말하며 [x, y]형태로 값을 삽입함

EndPos

 선이 끝나는 점을 말하며 [x, y]형태로 값을 삽입함

Blend

 Anti-Aliasing(부드러움) 설정 여부로, True 혹은 False를 입력함


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


02. pygame.draw.aaline를 이용한 여러 개의 선 그리기(예제)

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


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)

    # Draw on the screen a GREEN line from (0,50) to (50, 80) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, BLUE, [0, 50], [50, 80], True)

    # Draw on the screen a GREEN line from (0,100) to (150, 180) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, GREEN, [0, 100], [150, 180], False)
     
    # 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.lines 함수의 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 - StartPos, EndPos

    # Draw on the screen a GREEN line from (0,50) to (50, 80) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, BLUE, [0, 50], [50, 80], True)

    # Draw on the screen a GREEN line from (0,100) to (150, 180) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, GREEN, [0, 100], [150, 180], False)

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

42번째 라인의 값을 예시로 살펴보면, Start_pos의 값인 경우 [0, 50]이고, End_pos의 경우 [50, 80]입니다.

Start_pos와 End_pos는 모두 [x, y]의 형태로 값을 삽입합니다.

Start_pos의 x축의 값은 0, y축의 값은 50

End_pos의 x축의 값은 50, y축의 값은 80

이라고 보시면 됩니다.


Variable - Blend

    # Draw on the screen a GREEN line from (0,50) to (50, 80) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, BLUE, [0, 50], [50, 80], True)

    # Draw on the screen a GREEN line from (0,100) to (150, 180) 
    # 5 pixels wide.
    pygame.draw.aaline(screen, GREEN, [0, 100], [150, 180], False)

42번째 라인과 46번째 라인을 보시면 두 함수의 Blend는 각각 True와 False인 것을 확인할 수 있습니다.

Blend는 Anti-Aliasing 즉, 부드럽게 선을 그려줄지에 대한 선택을 말합니다.

만약 True로 값을 설정하여 함수를 실행한다면, 그려지는 선은 매우 부드럽게 그려지게 됩니다.

반대로 False로 값을 설정하여 함수를 실행항다면, 그려지는 선은 그림판으로 그린 것처럼 부드럽지 않게 그려지게 됩니다.

기본적으로 Blend는 True로 설정되어 있습니다.

True와 False의 비교는 아래의 그림과 같습니다.




03. 주의할 점

소스코드의 인자값을 보시면 아실 수 있겠지만, 해당 함수는 선의 두께를 지정해줄 수 없습니다.

즉, 기본적으로 선분의 길이가 1이라는 의미입니다.

부드러운 선, 부드럽지 않은 선 모두 두께가 1로 고정되어 있기 때문에, 부드러운 선을 그리고 싶으시다면, 두께가 1인 것을 감안하여 사용해야 합니다.






+ Recent posts