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

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




사각형 - pygame.draw.rect

삼각형 - pygame.draw.polygon

원 - pygame.draw.circle

타원 - pygame.draw.ellipse

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

여러 개의 선 - pygame.draw.lines

부드러운 선 - pygame.draw.aaline

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



01. pygame.draw.line 함수

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



 pygame.draw.line(Surface, Color, Start_pos, End_pos, Width=1)

  Draw a straight line segment.

  Draw a straight line segment on a Surface. There are no endcaps, the ends are squared off for thick lines.




변수 

설명

Surface

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

Color

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

Start_pos

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

End_pos

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

Width

 선의 두께를 말하며, 기본적으로 1으로 설정됨


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


02. pygame.draw.line을 이용한 선 그리기(예제)

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


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,0) to (100, 150) 
    # 5 pixels wide.
    pygame.draw.line(screen, GREEN, [0, 0], [100,150], 5)

    # Draw on the screen a GREEN line from (0,300) to (100, 150) 
    # 5 pixels wide.
    pygame.draw.line(screen, BLUE, [0, 300], [100,150], 5)
     
    # 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.line 함수의 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 - Start_pos, End_pos

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

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

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

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

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

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

End_pos의 x축의 값은 100, y축의 값은 150

이라고 보시면 됩니다.


또한 두 선은 다른 시작 위치 같은 끝 위치를 가지고 있습니다.

이를 그림으로 표현해보면 다음과 같습니다.




초록색 선은 ( 0, 0 )에서 ( 100, 150 )으로 그려지고,

파란색 선은 ( 0, 300 )에서 ( 100, 150 )으로 그려집니다.


Variable - Width

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

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

42번째 라인과 46번째 라인을 보시면 두 함수의 선 두께가 모두 5로 같은 것을 볼 수 있습니다.

기본적으로 line 함수의 경우 Width가 1로 되어 있으나, 이를 수정해줄 수 있습니다.

단, 0 이하의 수는 사용할 수 없습니다.

만약 사용하더라도 선이 그려지지 않습니다.(선의 두께가 0이라는 것은 선이 없다라는 걸 의미하지요.)



+ Recent posts