pygame.draw.circle() 함수로 원을 그려본다.
본 글에서 사용하는 기능 및 참고되는 기능에 대한 정의는 다음과 같다.
pygame.draw.circle(surface, color, center, radius, width=0,
draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None) -> Rect
pygame.draw.circle() 함수로 원 그리기
<코드 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(BLACK)
pygame.draw.circle(screen, WHITE, [100, 100], 50, 2)
pygame.draw.circle(screen, WHITE, [300, 100], 50)
pygame.draw.circle(screen, BLUE, [100, 300], 50, 0, draw_top_right=True)
pygame.draw.circle(screen, RED, [100, 300], 50, 0, draw_top_left=True)
pygame.draw.circle(screen, GREEN, [100, 300], 50, 4, draw_bottom_left=True)
pygame.draw.circle(screen, WHITE, [100, 300], 50, 4, draw_bottom_right=True)
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == '__main__':
main()
<코드 1>을 실행하면 게임 화면에 여러 원이 그려진다.
예제 코드 2
<코드 2>를 실행해 눈가 비숫할지 상상해 본다.
<코드 2>
# 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_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
falls = []
for i in range(100):
pos_x = random.randrange(0, screen_size[0])
pos_y = random.randrange(0, screen_size[1])
size = random.randint(1, 3)
speed = random.randint(1, 2)
falls.append([pos_x, pos_y, size, 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(BLACK)
for i in range(len(falls)):
pygame.draw.circle(screen, WHITE, falls[i][0:2], falls[i][2])
falls[i][1] += falls[i][3]
if falls[i][1] > screen_size[1]:
pos_x = random.randrange(0, screen_size[0])
falls[i][0] = pos_x
falls[i][1] = falls[i][3] * -2
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == '__main__':
main()