본문 바로가기

코딩/파이썬 pygame

파이썬 게임 007. pygame.draw.line 및 pygame.draw.lines 함수로 직선 그리기

Game - Python - Pygame

 

pygame.draw.line() 함수 및 pygame.draw.lines() 함수로 직선을 그려본다.

 

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

 

pygame.draw.line(surface, color, start_pos, end_pos, width=1) -> Rect
pygame.draw.lines(surface, color, closed, points, width=1) -> Rect

pygame.Rect(left, top, width, height) -> Rect

pygame.draw.line() 와 pygame.draw.lines() 함수로 직선 그리기

<코드 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)


def main():
    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    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(BLACK)

        pygame.draw.line(screen, WHITE, (50, 50), (150, 50))
        pygame.draw.line(screen, WHITE, (50, 50 + 20), (150, 50 + 20), 0)
        pygame.draw.line(screen, WHITE, (50, 50 + 40), (150, 50 + 40), 2)
        pygame.draw.line(screen, WHITE, (50, 50 + 60), (150, 50 + 60), 3)
        pygame.draw.line(screen, WHITE, (50, 50 + 80), (150, 50 + 80), 4)
        pygame.draw.line(screen, WHITE, [50, 50 + 100], [150, 50 + 150], 2)

        pygame.draw.lines(screen, GREEN, False, ((200, 50), (230, 100), (270, 100), (300, 50)), 2)
        pygame.draw.lines(screen, GREEN, True, [[200, 150], [230, 200], [270, 200], [300, 150]], 2)

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

    pygame.quit()


if __name__ == '__main__':
    main()

 

 

<코드 1>을 실행하면 게임 화면에 여러 직선이 그려진다.

 

pygame.draw.line() 함수의 width 파라미터는 직선의 굵기를 결정한다. width의 값이 1보다 작으면 직선은 그려지지 않는다.

 

pygame.draw.lines() 함수의 closed 파라미터는 직선의 시작과 끝을 연결할지를 결정한다. closed 파라미터의 값이 True이면 시작과 끝 지점은 서로 연결된다.

직선의 굵기에 따라 직선이 그려지는 위치 차이

<코드 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)


def main():
    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    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.line(screen, BLACK, (50 + 10, 50), (250, 50), 2)
        pygame.draw.line(screen, BLACK, (50 + 20, 50), (250, 50), 3)
        pygame.draw.line(screen, BLACK, (50 + 30, 50), (250, 50), 4)
        pygame.draw.line(screen, BLACK, (50 + 40, 50), (250, 50), 5)
        pygame.draw.line(screen, RED, (50, 50), (250, 50), 1)

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

    pygame.quit()


if __name__ == '__main__':
    main()

 

<코드 2>를 통해 직선의 굵기에 따라 직선이 그려지는 위치의 차이를 확인할 수 있다.

아래와 같이 width가 1인 직선은 빨간색이고 굵기가 1씩 증가할 때마다 빨간색 직선의 위 및 아래 픽셀이 늘어난다.

 

예제 코드 3

 

게임 화면에 비가 내리는 코드를 작성해 본다.

 

<코드 3>

# python 3.9.6
# pygame 2.0.1

import pygame
import random

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 = pygame.display.set_mode((0, 0))
    screen_size = screen.get_size()
    clock = pygame.time.Clock()

    running = True

    falls = []

    for i in range(100):
        pos_x = random.randrange(0, screen_size[0])
        pos_y = random.randrange(0, screen_size[1])
        speed = random.randint(25, 27)
        falls.append([pos_x, pos_y, speed])

    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((0, 0, 10))

        for i in range(len(falls)):
            pos_x = falls[i][0]
            pos_y = falls[i][1]
            speed = falls[i][2]
            pygame.draw.line(screen, WHITE, [pos_x, pos_y], [pos_x, pos_y + 2], 3)
            falls[i][1] += speed

            if falls[i][1] > screen_size[1]:
                falls[i][0] = random.randrange(0, screen_size[0])
                falls[i][1] = -10

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

    pygame.quit()


if __name__ == '__main__':
    main()