pygame.draw.ellipse() 함수, pygame.draw.polygon() 함수, pygame.draw.arc() 함수를 이용해 타원, 다각형 및 타원호를 그려본다.
본 글에서 사용하는 기능 및 참고되는 기능에 대한 정의는 다음과 같다.
pygame.draw.ellipse(surface, color, rect, width=0) -> Rect
pygame.draw.polygon(surface, color, points, width=0) -> Rect
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
그외 도형 그리기 (ellipse, polygon, arc)
<코드 1>
# python 3.9.6
# pygame 2.0.1
import pygame
from math import pi
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))
# screen_size = screen.get_size()
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)
rect = pygame.draw.ellipse(screen, WHITE, [50, 50, 100, 50], 2)
pygame.draw.rect(screen, RED, rect, 1)
rect = pygame.draw.ellipse(screen, WHITE, [200, 50, 100, 50])
pygame.draw.rect(screen, RED, rect, 1)
rect = pygame.draw.polygon(screen, WHITE, [[50, 150], [60, 180], [210, 180], [200, 150]], 2)
pygame.draw.rect(screen, RED, rect, 1)
rect = pygame.draw.arc(screen, WHITE, [150, 250, 100, 100], (pi/2) * 0, (pi/2) * 1, 2)
pygame.draw.rect(screen, RED, rect, 1)
pygame.draw.arc(screen, GREEN, [100, 250, 100, 100], (pi/2) * 1, (pi/2) * 2, 2)
pygame.draw.arc(screen, BLUE, [100, 300, 100, 100], (pi/2) * 2, (pi/2) * 3, 2)
pygame.draw.arc(screen, RED, [150, 300, 100, 100], (pi/2) * 3, (pi/2) * 4, 2)
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == '__main__':
main()
<코드 1>을 실행하면 다양한 도형이 그려지는데, 각 도형의 경계는 pygame.draw.rect() 함수 호출에 의해 그려진다.
예제 코드 2
<코드 2>를 실행하면 좌, 우로 움직이는 도형이 그려진다.
<코드 2>
# python 3.9.6
# pygame 2.0.1
import pygame
from math import pi
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
angle = [.0, .1]
direction = True
HP_DOWN_EVENT = pygame.event.custom_type()
pygame.time.set_timer(HP_DOWN_EVENT, 100)
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
elif event.type == HP_DOWN_EVENT:
if direction:
angle[0] = round(angle[0] + .1, 1)
angle[1] = round(angle[1] + .1, 1)
if angle[1] > 2:
angle[0] = 1.8
angle[1] = 1.9
direction = False
else:
angle[0] = round(angle[0] - .1, 1)
angle[1] = round(angle[1] - .1, 1)
if angle[0] < 0:
angle[0] = .1
angle[1] = .2
direction = True
if not running:
continue
screen.fill(BLACK)
pygame.draw.line(screen, GREEN, [140, 350], [560, 350], 1)
pygame.draw.line(screen, GREEN, [350, 140], [350, 350], 1)
pygame.draw.line(screen, GREEN, [350, 140], [350, 350], 1)
pygame.draw.circle(screen, GREEN, [350, 350], 200, 1, draw_top_right=True, draw_top_left=True)
pygame.draw.circle(screen, GREEN, [350, 350], 150, 1, draw_top_right=True, draw_top_left=True)
pygame.draw.circle(screen, GREEN, [350, 350], 100, 1, draw_top_right=True, draw_top_left=True)
pygame.draw.circle(screen, GREEN, [350, 350], 50, 1, draw_top_right=True, draw_top_left=True)
rect = pygame.draw.arc(screen, GREEN, [150, 150, 400, 400], (pi / 2) * angle[0], (pi / 2) * angle[1], 200)
pygame.draw.rect(screen, RED, rect, 1)
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == '__main__':
main()
'코딩 > 파이썬 pygame' 카테고리의 다른 글
파이썬 게임 013. Pygame Documentation Summary (0) | 2021.10.07 |
---|---|
파이썬 게임 012. pygame.image.load 함수로 이미지 불러와 게임 화면에 그리고 pygame.image.save 함수로 게임 화면 저장하기 (0) | 2021.10.05 |
파이썬 게임 010. pygame.draw.circle 함수로 원 그리기 (0) | 2021.10.01 |
파이썬 게임 009. pygame.draw.rect 함수로 사각형 그리기 (0) | 2021.09.29 |
파이썬 게임 008. pygame.draw.aaline 및 pygame.draw.aalines 함수로 직선의 안티에일리어싱 효과 적용 (위신호 제거) (0) | 2021.09.27 |