파이썬으로 만드는 나만의 게임 개발 강좌 6강 – 점수판 만들기와 실시간 점수 출력

파이썬 게임 개발 강좌 6강에 오신 것을 환영합니다!

이전 강의에서는 시간 제한과 게임 종료 메시지를 구현해봤습니다. 이번 강의에서는 게임에 또 하나의 재미 요소를 더해줄 **점수판(Scoreboard)** 을 만들어보겠습니다.

캐릭터가 특정 행동을 할 때 점수를 얻고, 그 점수가 화면에 실시간으로 표시되도록 해볼 거예요.

이번 강의 목표

  • 점수 변수와 누적 구조 만들기
  • 특정 조건에서 점수 상승
  • 게임 화면에 점수 실시간 출력

1. 점수를 언제 줄까?

우리는 이번 예제에서 다음 상황일 때 점수를 올려보겠습니다:

  • 캐릭터가 아이템에 닿았을 때 (충돌)

그러기 위해선 캐릭터 외에 아이템 이미지와 위치도 설정해야겠죠!


2. 아이템 이미지 추가

게임 파일 안에 다음 이미지를 준비해주세요:

  • item.png (예: 코인 이미지)

사이즈는 50×50 정도면 좋습니다.


3. 전체 코드 실습

import pygame
import random

pygame.init()

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("점수판 예제")

# 배경, 캐릭터, 아이템 이미지 불러오기
background = pygame.image.load("background.png")
character = pygame.image.load("character.png")
item = pygame.image.load("item.png")

# 캐릭터 설정
char_size = character.get_rect().size
char_w = char_size[0]
char_h = char_size[1]
char_x = (screen_width - char_w) / 2
char_y = screen_height - char_h

# 이동값, 속도
to_x = 0
to_y = 0
speed = 0.5

# 아이템 위치 랜덤 생성
item_size = item.get_rect().size
item_w = item_size[0]
item_h = item_size[1]
item_x = random.randint(0, screen_width - item_w)
item_y = random.randint(0, screen_height - item_h)

# 점수
score = 0
font = pygame.font.Font(None, 40)

clock = pygame.time.Clock()
running = True

while running:
    dt = clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_x -= speed
            elif event.key == pygame.K_RIGHT:
                to_x += speed
            elif event.key == pygame.K_UP:
                to_y -= speed
            elif event.key == pygame.K_DOWN:
                to_y += speed

        if event.type == pygame.KEYUP:
            if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:
                to_x = 0
            if event.key in [pygame.K_UP, pygame.K_DOWN]:
                to_y = 0

    char_x += to_x * dt
    char_y += to_y * dt

    # 화면 경계 체크
    char_x = max(0, min(screen_width - char_w, char_x))
    char_y = max(0, min(screen_height - char_h, char_y))

    # 충돌 체크
    char_rect = character.get_rect(topleft=(char_x, char_y))
    item_rect = item.get_rect(topleft=(item_x, item_y))

    if char_rect.colliderect(item_rect):
        score += 10
        print("아이템 획득! 점수:", score)
        item_x = random.randint(0, screen_width - item_w)
        item_y = random.randint(0, screen_height - item_h)

    screen.blit(background, (0, 0))
    screen.blit(character, (char_x, char_y))
    screen.blit(item, (item_x, item_y))

    # 점수 출력
    score_display = font.render(f"Score: {score}", True, (255, 255, 0))
    screen.blit(score_display, (10, 10))

    pygame.display.update()

pygame.quit()

4. 코드 설명

  • score += 10: 아이템 충돌 시 10점 추가
  • font.render: 점수를 문자열로 표시
  • colliderect: 아이템과 캐릭터가 겹칠 때 감지

5. 연습 과제

  • 아이템을 여러 개 등장시켜보기
  • 획득 시 사운드 효과 추가하기
  • 획득한 아이템 개수도 별도 카운팅해보기

6. 다음 강의 예고

7강: 게임 배경음과 효과음 넣기 – 사운드 추가로 몰입감 높이기
다음 강의에서는 점점 완성되어 가는 게임에 **배경 음악과 효과음을 추가**해서 몰입감을 한 단계 끌어올려보겠습니다. 게임 분위기를 살리는 핵심 요소죠!

이 강의는 Pygame을 활용한 파이썬 게임 개발 시리즈입니다. 기초부터 기능 확장까지 단계별로 배워보세요!