728x90

타이틀 화면 추가
이번 강의에서는 게임이 실행되자마자 바로 시작되지 않고,
타이틀 화면을 먼저 보여주는 구조를 구현합니다.
이 강의의 핵심은
게임에 상태(State) 개념을 도입하는 것입니다.
이번 강의가 끝나면 게임의 흐름은 다음과 같이 바뀝니다.
- 게임 실행
- 타이틀 화면 표시
- 키 입력 후 게임 시작
타이틀 화면이 필요한 이유
지금까지의 게임은 실행하자마자 공이 움직이기 시작했습니다.
하지만 대부분의 게임은 다음 단계를 거칩니다.
- 게임의 이름을 보여줍니다
- 플레이어에게 준비 시간을 줍니다
- 시작을 명확하게 알립니다
이를 위해
타이틀 상태와 플레이 상태를 구분해야 합니다.
게임 상태(State) 개념 도입
게임 상태를 문자열로 관리합니다.
game_state = "TITLE"
이번 강의에서는 다음 두 가지 상태를 사용합니다.
- "TITLE" : 타이틀 화면
- "PLAY" : 실제 게임 진행
타이틀 화면에서 처리할 것들
타이틀 상태에서는 다음 작업만 수행합니다.
- 배경을 그립니다
- 게임 제목을 표시합니다
- 시작 안내 문구를 표시합니다
- 키 입력을 기다립니다
공 이동, 충돌, 점수 계산은
절대 처리하지 않습니다.
타이틀 텍스트 준비
타이틀 화면에 표시할 텍스트를 준비합니다.
title_font = pygame.font.SysFont(None, 64)
info_font = pygame.font.SysFont(None, 28)
title_text = title_font.render("BREAKOUT GAME", True, WHITE)
info_text = info_font.render("Press SPACE to Start", True, WHITE)
타이틀 화면 그리기
게임 루프 안에서
상태가 "TITLE" 일 때 화면을 그립니다.
if game_state == "TITLE":
screen.fill(BLACK)
screen.blit(
title_text,
(SCREEN_WIDTH // 2 - title_text.get_width() // 2, 200)
)
screen.blit(
info_text,
(SCREEN_WIDTH // 2 - info_text.get_width() // 2, 300)
)
pygame.display.flip()
continue
continue 를 사용해
타이틀 상태에서는 아래 게임 로직이 실행되지 않도록 합니다.
타이틀 화면에서 시작 처리
타이틀 화면에서는
스페이스 키를 누르면 게임이 시작됩니다.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_state == "TITLE":
game_state = "PLAY"
상태가 "PLAY" 로 바뀌는 순간부터
기존 게임 로직이 실행됩니다.
현재 단계의 결과
이번 강의까지 구현된 결과는 다음과 같습니다.
- 게임 실행 시 타이틀 화면이 먼저 표시됩니다
- 게임 제목과 시작 안내 문구가 나타납니다
- 스페이스 키를 누르면 게임이 시작됩니다
이제 게임은
명확한 시작 지점을 가진 구조가 되었습니다.

전체 코드
더보기
import pygame
import sys
import random
pygame.init()
pygame.mixer.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Breakout Game")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
EFFECT_COLOR = (255, 200, 200)
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.SysFont(None, 30)
title_font = pygame.font.SysFont(None, 64)
info_font = pygame.font.SysFont(None, 28)
brick_sound = pygame.mixer.Sound("brick.wav")
paddle_sound = pygame.mixer.Sound("paddle.wav")
item_sound = pygame.mixer.Sound("item.wav")
game_state = "TITLE"
score = 0
lives = 3
paddle_width = 100
paddle_height = 15
paddle_speed = 7
paddle_rect = pygame.Rect(
(SCREEN_WIDTH - paddle_width) // 2,
SCREEN_HEIGHT - 40,
paddle_width,
paddle_height
)
ball_radius = 8
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_speed_x = 5
ball_speed_y = -5
ball_rect = pygame.Rect(
ball_x - ball_radius,
ball_y - ball_radius,
ball_radius * 2,
ball_radius * 2
)
brick_width = 60
brick_height = 20
brick_padding = 10
rows = 4
cols = 10
bricks = []
items = []
effects = []
item_speed = 4
def create_bricks():
bricks.clear()
for row in range(rows):
for col in range(cols):
brick_x = col * (brick_width + brick_padding) + 35
brick_y = row * (brick_height + brick_padding) + 50
bricks.append(
pygame.Rect(brick_x, brick_y, brick_width, brick_height)
)
def create_item(x, y):
items.append(pygame.Rect(x, y, 20, 20))
create_bricks()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_state == "TITLE":
game_state = "PLAY"
if game_state == "TITLE":
screen.fill(BLACK)
screen.blit(
title_font.render("BREAKOUT GAME", True, WHITE),
(SCREEN_WIDTH // 2 - 200, 200)
)
screen.blit(
info_font.render("Press SPACE to Start", True, WHITE),
(SCREEN_WIDTH // 2 - 110, 300)
)
pygame.display.flip()
continue
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
paddle_rect.x -= paddle_speed
if keys[pygame.K_RIGHT]:
paddle_rect.x += paddle_speed
paddle_rect.left = max(paddle_rect.left, 0)
paddle_rect.right = min(paddle_rect.right, SCREEN_WIDTH)
ball_x += ball_speed_x
ball_y += ball_speed_y
ball_rect.x = ball_x - ball_radius
ball_rect.y = ball_y - ball_radius
if ball_rect.left <= 0 or ball_rect.right >= SCREEN_WIDTH:
ball_speed_x *= -1
if ball_rect.top <= 0:
ball_speed_y *= -1
if ball_rect.colliderect(paddle_rect):
ball_speed_y *= -1
ball_rect.bottom = paddle_rect.top
ball_y = ball_rect.centery
paddle_sound.play()
for brick in bricks:
if ball_rect.colliderect(brick):
overlap_left = ball_rect.right - brick.left
overlap_right = brick.right - ball_rect.left
overlap_top = ball_rect.bottom - brick.top
overlap_bottom = brick.bottom - ball_rect.top
min_overlap = min(overlap_left, overlap_right, overlap_top, overlap_bottom)
if min_overlap == overlap_left or min_overlap == overlap_right:
ball_speed_x *= -1
else:
ball_speed_y *= -1
if random.random() < 0.3:
create_item(brick.centerx - 10, brick.centery)
effects.append({
"rect": pygame.Rect(brick.x, brick.y, brick.width, brick.height),
"timer": 10
})
brick_sound.play()
bricks.remove(brick)
score += 10
break
for item in items[:]:
item.y += item_speed
if item.colliderect(paddle_rect):
paddle_rect.width += 30
item_sound.play()
items.remove(item)
elif item.top > SCREEN_HEIGHT:
items.remove(item)
for effect in effects[:]:
effect["timer"] -= 1
if effect["timer"] <= 0:
effects.remove(effect)
if ball_rect.top > SCREEN_HEIGHT:
lives -= 1
if lives <= 0:
running = False
else:
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_speed_x = 5
ball_speed_y = -5
paddle_rect.width = paddle_width
paddle_rect.x = (SCREEN_WIDTH - paddle_width) // 2
create_bricks()
items.clear()
effects.clear()
screen.fill(BLACK)
screen.blit(font.render(f"Score: {score}", True, WHITE), (10, 10))
screen.blit(font.render(f"Lives: {lives}", True, WHITE), (SCREEN_WIDTH - 100, 10))
pygame.draw.rect(screen, WHITE, paddle_rect)
pygame.draw.circle(screen, WHITE, (ball_x, ball_y), ball_radius)
for brick in bricks:
pygame.draw.rect(screen, WHITE, brick)
for item in items:
pygame.draw.rect(screen, GREEN, item)
for effect in effects:
pygame.draw.rect(screen, EFFECT_COLOR, effect["rect"])
pygame.display.flip()
pygame.quit()
sys.exit()
728x90
'⚙️ Python > 🎮 Pygame 실전' 카테고리의 다른 글
| [Pygame] 🧱 벽돌 깨기 게임 만들기 14강 | 재시작 기능 추가 (0) | 2026.02.26 |
|---|---|
| [Pygame] 🧱 벽돌 깨기 게임 만들기 13강 | 게임 시작 흐름 개선 (0) | 2026.02.26 |
| [Pygame] 🧱 벽돌 깨기 게임 만들기 11강 | 이펙트 & 사운드 추가 (0) | 2026.02.25 |
| [Pygame] 🧱 벽돌 깨기 게임 만들기 10강 | 아이템 추가 (0) | 2026.02.24 |
| [Pygame] 🧱 벽돌 깨기 게임 만들기 9강 | 점수 & 라이프 시스템 (0) | 2026.02.24 |