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

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




사각형 - pygame.draw.rect

삼각형 - pygame.draw.polygon

원 - pygame.draw.circle

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

선 - pygame.draw.line

여러 개의 선 - pygame.draw.lines

부드러운 선 - pygame.draw.aaline

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




01. pygame.draw.ellipse 함수

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



 pygame.draw.ellipse(Surface, Color, Rect, Width=0)

  Draw a round shape inside a rectangle.

  Draws an elliptical shape on the Surface. The given rectangle is the area that the circle will fill. The width argument is the thickness to draw the outer edge. If width is zero then the ellipse will be filled.




변수 

설명

Surface

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

Color

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

Rect

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

Width

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


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



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

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



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 an ellipse outline, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) 

    # Draw an solid ellipse, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) 
     
    # 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.ellipse 함수의 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 - Rect

    # Draw an ellipse outline, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) 

    # Draw an solid ellipse, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) 

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

41번째 라인의 값을 예시로 살펴보면, [225, 10, 50, 20]으로 된 값에는 [x, y, w, h]라고 설정된 값이 들어가 있다고 보시면 됩니다.

즉, x축의 위치는 225, y축의 위치는 10, 가로는 50, 세로는 20이라고 보시면 됩니다.


단, 여기서 왜 Rect라고 되어있는가에 대한 의문이 들 수 있으실 것입니다.

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



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

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

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


Variable - Width

    # Draw an ellipse outline, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) 

    # Draw an solid ellipse, using a rectangle as the outside boundaries
    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) 

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

41번째 라인과 44번째 라인을 보시면 두 가지 방법으로 타원을 그린 것을 볼 수 있습니다.

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

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

즉, 타원의 선의 두께를 지정해준 것과 같은 것입니다.

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

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


41번째 라인의 ellipse 함수는 첫 번째 하얀 타원이고,

44번째 라인의 ellipse 함수는 두 번째 빨간 타원입니다.



41번째 라인의 ellipse 함수의 선 두께가 2로 지정되어 있으니, 적당한 두께의 형태로 출력이되었습니다.

또한 44번째 라인의 ellipse 함수는 선이 따로 없는 빨간색으로 채운 타원이 출력됩니다.


+ Recent posts