[python] Pygame을 활용한 플랫폼 점프 게임 만들기

이 튜토리얼에서는 Python의 게임 개발 라이브러리인 Pygame을 사용하여 플랫폼 점프 게임을 만드는 방법에 대해 알아보겠습니다.

목차

Pygame 소개

Pygame은 Python으로 2D 게임을 만들 수 있는 라이브러리입니다. Pygame은 마우스, 키보드, 그림 그리기 등 다양한 게임 개발 기능을 제공합니다.

게임 개발 환경 설정

먼저, Pygame을 설치해야 합니다. 다음 명령을 통해 Pygame을 설치할 수 있습니다:

pip install pygame

화면 생성

Pygame을 사용하여 게임 화면을 생성하는 코드는 다음과 같습니다:

import pygame

pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Platform Jump Game")

# 게임 루프
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))
    pygame.display.flip()

pygame.quit()

위 코드에서 pygame.init() 함수를 사용해 Pygame을 초기화하고, 화면의 크기를 정의한 후 pygame.display.set_mode() 함수로 화면을 생성합니다. pygame.display.set_caption() 함수는 게임 창의 제목을 설정합니다.

게임 루프는 게임이 종료될 때까지 반복되며, pygame.event.get() 함수를 통해 발생한 이벤트를 처리합니다. 현재는 닫기 버튼 이벤트만 처리하도록 구현되어 있습니다. 게임 화면은 흰색으로 채워지고, pygame.display.flip() 함수를 호출하여 화면을 갱신합니다.

주인공 캐릭터 만들기

게임에서 주인공 캐릭터를 표현하기 위해 다음과 같이 이미지를 로드하고 화면에 그릴 수 있습니다:

import pygame

pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Platform Jump Game")

player_image = pygame.image.load("player.png")
player_rect = player_image.get_rect()
player_rect.topleft = (width // 2, height - player_rect.height)

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

    screen.fill((255, 255, 255))
    screen.blit(player_image, player_rect)
    pygame.display.flip()

pygame.quit()

위 코드에서는 player.png 이미지를 pygame.image.load() 함수를 통해 로드한 후, player_rect 변수를 통해 이미지의 위치를 설정합니다. screen.blit() 함수를 사용해 플레이어 이미지를 그릴 수 있습니다.

플랫폼 만들기

게임 화면에 플랫폼을 추가하여 캐릭터가 이동할 수 있는 평면을 만들어봅시다. 플랫폼은 이미지 대신에 색상으로 표현됩니다:

import pygame

pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Platform Jump Game")

player_image = pygame.image.load("player.png")
player_rect = player_image.get_rect()
player_rect.topleft = (width // 2, height - player_rect.height)

platform_color = (0, 0, 255)
platform_rect = pygame.Rect(0, height - 40, width, 40)

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

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, platform_color, platform_rect)
    screen.blit(player_image, player_rect)
    pygame.display.flip()

pygame.quit()

위 코드에서 pygame.Rect() 함수를 사용하여 플랫폼의 크기와 위치를 설정합니다. pygame.draw.rect() 함수를 통해 플랫폼을 화면에 그릴 수 있습니다.

점프 기능 추가하기

주인공 캐릭터에 점프 기능을 추가해봅시다:

import pygame

pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Platform Jump Game")

player_image = pygame.image.load("player.png")
player_rect = player_image.get_rect()
player_rect.topleft = (width // 2, height - player_rect.height)

jumping = False
jump_count = 10

platform_color = (0, 0, 255)
platform_rect = pygame.Rect(0, height - 40, width, 40)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if not jumping:
                    jumping = True

    if jumping:
        if jump_count >= -10:
            player_rect.y -= (jump_count * abs(jump_count)) * 0.5
            jump_count -= 1
        else:
            jumping = False
            jump_count = 10

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, platform_color, platform_rect)
    screen.blit(player_image, player_rect)
    pygame.display.flip()

pygame.quit()

위 코드에서 jumping 변수는 점프 중인지 여부를 나타냅니다. jump_count 변수는 점프의 높이를 결정합니다. 점프 중일 때는 주인공 캐릭터의 위치를 변경하여 점프하는 동작을 표현합니다.

게임 루프 만들기

마지막으로 게임 루프를 완성해봅시다:

import pygame

pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Platform Jump Game")

player_image = pygame.image.load("player.png")
player_rect = player_image.get_rect()
player_rect.topleft = (width // 2, height - player_rect.height)

jumping = False
jump_count = 10

platform_color = (0, 0, 255)
platform_rect = pygame.Rect(0, height - 40, width, 40)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if not jumping:
                    jumping = True

    if jumping:
        if jump_count >= -10:
            player_rect.y -= (jump_count * abs(jump_count)) * 0.5
            jump_count -= 1
        else:
            jumping = False
            jump_count = 10

    if player_rect.colliderect(platform_rect):
        jump_count = 10

    player_rect.y += 5

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, platform_color, platform_rect)
    screen.blit(player_image, player_rect)
    pygame.display.flip()

pygame.quit()

위 코드에서는 player_rect.colliderect(platform_rect)를 사용하여 캐릭터와 플랫폼이 충돌하는지 확인하고, 충돌했을 때 점프 기능을 초기화하는 코드를 추가했습니다. 또한, player_rect.y += 5를 통해 캐릭터를 아래로 이동시킵니다.

이제 실행해보면 플랫폼 위에서 캐릭터가 점프하는 플랫폼 점프 게임을 만들 수 있습니다.

결론

이번 튜토리얼에서는 Pygame을 사용해 플랫폼 점프 게임을 개발하는 방법을 알아보았습니다. Pygame을 활용하면 Python으로 간단한 게임을 쉽게 만들 수 있습니다. 게임 개발에 관심이 있는 분들은 Pygame을 사용해 다양한 게임을 만들어보세요.