PyGame 소개
게임 개발의 기초를 배웁니다.
PyGame 소개은(는) CoddyKit의 무료 Python For Kids 강의입니다. 이것은 3개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Python For Kids 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Python For Kids 강의에는 총 3개의 강의가 포함되어 있습니다.
PyGame 소개
PyGame 레슨에 오신 것을 환영합니다! 오늘은 그래픽, 사운드, 사용자 상호 작용이 포함된 게임을 만들 수 있도록 도와주는 Python 라이브러리인 PyGame을 사용하여 게임 개발의 기초를 배워 보겠습니다.

PyGame이란?
PyGame은 2D 게임을 만들기 위한 Python 라이브러리입니다. 다음과 같은 도구를 제공합니다.
- 그래픽 표시
- 사용자 입력 처리
- 음향 효과와 음악 추가
- 애니메이션 만들기
간단한 게임이든 복잡한 게임이든 PyGame을 사용하면 게임 개발을 더 쉽고 재미있게 할 수 있습니다!
PyGame 설치
PyGame을 사용하려면 먼저 설치해야 합니다. 터미널이나 명령 프롬프트에서 다음 명령을 사용하세요.
pip install pygame
설치가 완료되면 첫 번째 게임을 만들 준비가 됩니다!
기본 게임 창 만들기
PyGame을 사용하여 기본 게임 창을 만드는 것부터 시작해 보겠습니다. 이 창은 게임의 기반이 됩니다.
예:
import pygame
# Initializes PyGame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600)) # Width: 800, Height: 600
pygame.display.set_caption("My First Game")
# Runs the game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Checks if the user closes the window
running = False
# Quits PyGame
pygame.quit()
색상 추가
RGB 값을 사용하여 게임 창을 색상으로 채울 수 있습니다. 각 색상은 빨간색, 초록색, 파란색 구성 요소로 정의됩니다.
예:
import pygame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Colorful Window")
# Colors in RGB
BLUE = (0, 0, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fills the screen with blue color
screen.fill(BLUE)
pygame.display.update()
pygame.quit()
도형 그리기
PyGame을 사용하면 직사각형, 원, 선과 같은 도형을 그릴 수 있습니다.
pygame.draw.rect(): 직사각형을 그립니다.pygame.draw.circle(): 원을 그립니다.pygame.draw.line(): 선을 그립니다.
예:
import pygame
pygame.init()
# Sets up the game window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Drawing Shapes")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fills the screen with white color
screen.fill(WHITE)
# Draws a red rectangle
pygame.draw.rect(screen, RED, (100, 100, 200, 150))
# Draws a green circle
pygame.draw.circle(screen, GREEN, (400, 300), 75)
pygame.display.update()
pygame.quit()
사용자 입력 처리
게임에서는 키를 누르거나 마우스를 클릭하는 것과 같은 사용자 입력이 필요한 경우가 많습니다. PyGame에서는 이벤트를 사용하여 입력을 처리할 수 있습니다.
예:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("User Input")
WHITE = (255, 255, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN: # Checks if a key is pressed
if event.key == pygame.K_SPACE:
print("Spacebar was pressed!")
elif event.type == pygame.MOUSEBUTTONDOWN: # Checks if the mouse is clicked
print("Mouse clicked at", event.pos)
screen.fill(WHITE)
pygame.display.update()
pygame.quit()
객체 이동
애니메이션이나 상호 작용이 가능한 게임을 만들려면 각 프레임에서 객체의 위치를 업데이트하여 객체를 이동할 수 있습니다.
예:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Moving Object")
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
x, y = 50, 50 # Starting position of the rectangle
velocity = 5 # Speed of movement
running = True
while running:
pygame.time.delay(30) # Delays the loop for 30ms
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Gets keys pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= velocity
if keys[pygame.K_RIGHT]:
x += velocity
if keys[pygame.K_UP]:
y -= velocity
if keys[pygame.K_DOWN]:
y += velocity
screen.fill(WHITE)
pygame.draw.rect(screen, BLUE, (x, y, 50, 50)) # Draws the moving rectangle
pygame.display.update()
pygame.quit()
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Question")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 255, 0)) # Fills the screen with green
pygame.display.update()
pygame.quit()
잘하셨습니다!
게임 창 만들기, 도형 그리기, 사용자 입력 처리, 객체 이동을 포함하여 PyGame을 게임 개발에 사용하는 기초를 배웠습니다. PyGame은 게임 아이디어를 실제로 구현할 수 있게 해 주는 강력한 라이브러리입니다. 계속 기능을 실험하며 재미있고 상호 작용이 가능한 나만의 게임을 만들어 보세요!

자주 묻는 질문
“PyGame 소개” 강의는 무료인가요?
네 — “PyGame 소개” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Python For Kids 강의 전체를 잠금 해제할 수 있습니다. Python For Kids 강의에는 총 3개의 강의가 포함되어 있습니다.
“PyGame 소개”에서 뭘 배우나요?
게임 개발의 기초를 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Python For Kids을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Python For Kids을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Python For Kids은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 3번째 강의입니다.
“PyGame 소개” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Python For Kids 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Python For Kids 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Python 라이브러리 소개
- math 라이브러리 사용하기
- PyGame 소개