본문 바로가기

코딩/파이썬 pygame

파이썬 게임 008. pygame.draw.aaline 및 pygame.draw.aalines 함수로 직선의 안티에일리어싱 효과 적용 (위신호 제거)

Game - Python - Pygame

 

pygame.draw.aaline() 함수 및 pygame.draw.aalines() 함수로 직선에 안티에일리어싱 효과(위신호 제거)를 적용해 본다.

 

본 글에서 사용한 기능 및 참고되는 기능에 대한 정의는 다음과 같다.

 

pygame.draw.aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
pygame.draw.aalines(surface, color, closed, points, blend=1) -> Rect

pygame.draw.aaline() 함수 및 pygame.draw.aalines() 함수로 직선의 위신호 제거

<코드 1>

# python 3.9.6
# pygame 2.0.1

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)


def main():
    pygame.init()
    
    screen_width, screen_height = (640, 480)
    screen = pygame.display.set_mode((screen_width, screen_height))
    clock = pygame.time.Clock()

    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT\
                    or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break
        if not running:
            continue

        screen.fill(WHITE)

        pygame.draw.aaline(screen, BLACK, (50, 50), (150, 50), True)
        pygame.draw.aaline(screen, BLACK, [50, 50 + 20], [150, 50 + 20], False)

        pygame.draw.aaline(screen, BLACK, (50, 100), (150, 150), True)
        pygame.draw.aaline(screen, BLACK, [50, 120], [150, 170], False)

        pygame.draw.aalines(screen, BLUE, False, ((200, 50), (230, 100), (270, 100), (300, 50)), True)
        pygame.draw.aalines(screen, BLUE, True, [[200, 150], [230, 200], [270, 200], [300, 150]], False)

        pygame.display.update()
        clock.tick(30)

    pygame.quit()


if __name__ == '__main__':
    main()

 

 

<코드 1>을 실행하면 사선에 안티에일리어싱이 적용되어 있음을 확인할 수 있다.

 

아래의 이미지에서 pygame.draw.aaline() 함수 및 pygame.draw.aalines() 함수의 파라미터 blend의 값이 False이면 안티에일리어싱은 적용되지 않음을 볼 수 있다.

 

 

pygame.draw.aaline() 함수 및 pygame.draw.aalines() 함수는 pygame.draw.line() 함수 및 pygame.draw.lines() 함수와 달리 width 파라미터를 가지고 있지 않다.

예제 코드 2

 

게임 화면에 사선들이 오른쪽으로 나아가는 코드를 작성해 본다.

 

<코드 2>

# python 3.9.6
# pygame 2.0.1

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)


def main():
    pygame.init()

    screen_width, screen_height = (0, 0)
    screen = pygame.display.set_mode((screen_width, screen_height))
    screen_size = screen.get_size()
    clock = pygame.time.Clock()

    running = True

    runs = []

    for i in range(1, 15):
        runs.append(i * 50)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT\
                    or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break
        if not running:
            continue

        screen.fill(BLACK)

        for i in range(len(runs)):
            pygame.draw.aaline(screen, RED, (runs[i], 0), (runs[i] + 100, screen_size[1]), True)
            runs[i] = 0 if (runs[i] + 50) > screen_size[0] else runs[i] + 5

        pygame.display.update()
        clock.tick(60)

    pygame.quit()


if __name__ == '__main__':
    main()