[python] Pygame을 사용하여 기억력 게임 만들기

이번에는 Python의 Pygame 라이브러리를 사용하여 간단한 기억력 게임을 만들어보겠습니다. 이 게임은 사용자가 화면에 나타난 도형의 순서를 기억하고 입력하는 게임입니다.

Pygame 설치

먼저 Pygame을 설치해야 합니다. 아래 명령을 사용하여 설치합니다:

pip install pygame

게임 구현

아래는 기억력 게임을 구현하는 Python 코드 예시입니다:

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("Memory Game")

# 색깔 정의 (RGB)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 게임 변수 설정
shapes = [pygame.Rect(50, 50, 100, 100), pygame.Rect(200, 200, 100, 100), pygame.Rect(350, 350, 100, 100)]
sequence = []
user_input = []
game_over = False

clock = pygame.time.Clock()

# 게임 루프
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONUP:  # 마우스 클릭 시
            if not game_over:
                user_input.append(event.pos)

    # 게임 로직
    if not game_over:
        if len(user_input) == len(sequence):  # 입력이 완료되면
            if user_input == sequence:  # 정답 확인
                sequence.append(random.choice(shapes))
                user_input = []
            else:  # 오답
                game_over = True

    # 화면 그리기
    screen.fill(WHITE)
    for shape in shapes:
        pygame.draw.rect(screen, RED, shape)
    for pos in sequence:
        pygame.draw.circle(screen, BLUE, pos, 50)
    for pos in user_input:
        pygame.draw.circle(screen, GREEN, pos, 50)
    if game_over:
        pygame.draw.rect(screen, BLACK, pygame.Rect(0, 0, 800, 600), 128)

    # 화면 업데이트
    pygame.display.flip()
    clock.tick(60)

# 게임 종료
pygame.quit()

실행하기

위의 코드를 복사하여 Python 파일로 저장한 뒤 실행하면 기억력 게임이 시작됩니다. 도형들이 나타나고, 사용자는 도형들의 순서를 기억하고 클릭하여 입력해야 합니다. 정확하게 입력하면 다음 도형이 추가되고, 오답이면 게임이 종료됩니다.

결론

Pygame을 사용하여 기억력 게임을 만들어보았습니다. 이 게임을 통해 기억력과 집중력을 향상시킬 수 있으며, Pygame을 활용하여 다양한 게임을 제작할 수 있는 가능성도 열립니다. 간단한 게임이지만 게임 개발에 대한 기본적인 지식과 문제 해결 능력을 향상시킬 수 있는 좋은 예시입니다.