[python] pygame 원 그리기 샘플

pygame에서 원을 그릴 때 좌표계는 아래와 같습니다.


아래는 샘플코드입니다.


import pygame

class Particle:
    def __init__(self, position, size):
        self.x, self.y = position
        self.size = size
        self.colour = (0, 0, 255)
        self.thickness = 1

    def display(self):
        pygame.draw.circle(screen, self.colour, (self.x, self.y), self.size, self.thickness)

# 윈도우 타이틀
pygame.display.set_caption('Tutorial 2')

# 배경화면 색상
background_color = (255,255,255)

# 윈도우 사이즈
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
screen.fill(background_color)

# 파티클 생성
my_first_particle = Particle((150, 50), 15)
my_first_particle.display()

# 윈도우 표기
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        # 닫기 버튼 감지
        if event.type == pygame.QUIT:
            running = False



아래는 실행결과입니다.


위 코드를 활용하여 여러 원형 파티클(particle)을 만들어 봅니다. 아래는 최종코드입니다.


import pygame
import random

class Particle():
    def __init__(self, position, size):
        self.x, self.y = position
        self.size = size
        self.colour = (0, 0, 255)
        self.thickness = 1

    def display(self):
        pygame.draw.circle(screen, self.colour, (self.x, self.y), self.size, self.thickness)

# 윈도우 타이틀
pygame.display.set_caption('Tutorial 3')

# 배경화면 색상
background_color = (255,255,255)

# 윈도우 사이즈
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
screen.fill(background_color)

# 파티클 생성
number_of_particles = 10
my_particles = []

for n in range(number_of_particles):
    size = random.randint(10, 20)
    x = random.randint(size, width-size)
    y = random.randint(size, height-size)
    my_particles.append(Particle((x, y), size))

for particle in my_particles:
    particle.display()

# 윈도우 표기
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        # 닫기 버튼 감지
        if event.type == pygame.QUIT:
            running = False



아래는 실행결과입니다.


끝.

댓글

이 블로그의 인기 게시물

[PLC] PLC 아날로그 입출력 기본

[농사] 실내 식물 재배 조명, 어떤 걸 선택해야 할까?

[아두이노] 가변저항(Potential Divider)과 전압분배(Voltage Divider)

전력(kW) 계산하기 (직류, 교류 단상, 교류 삼상)

공압 속도 제어: 미터인 vs 미터아웃

제너 다이오드에 저항을 연결하는 이유

[자동화] 안쓰는 안드로이드폰을 활용한 식물 성장 타임랩스 촬영

커패시터에 저장된 에너지 계산

[스마트팜] 아쿠아포닉스에서 pH 제어를 자동화해보자! (Python 활용)

[PLC] PLC 입출력 타입 - 싱크 & 소스 (Sink & Source)