파이썬은 초보자부터 전문가까지 누구나 손쉽게 다룰 수 있는 강력한 프로그래밍 언어입니다. 이번 블로그에서는 게임 개발의 기본을 배우기 위해 테트리스 게임을 만들어보겠습니다. pygame 라이브러리를 활용하여 간단한 코드로 구현할 수 있으며, 단계별 코드 설명을 통해 초보자도 따라 할 수 있습니다. 또한, 게임 개발의 기본 원리인 이벤트 처리, 충돌 감지, 렌더링을 학습할 수 있는 좋은 기회가 될 것입니다.
특징:
- 테트로미노의 생성과 회전
- 키보드 이벤트를 통한 제어
- 보드에 테트로미노 고정
- 간단하고 깔끔한 코드 구조
import pygame import random # 초기 설정 pygame.init() screen_width, screen_height = 300, 600 block_size = 30 screen = pygame.display.set_mode((screen_width, screen_height)) clock = pygame.time.Clock() # 색상 정의 colors = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] # 테트로미노 모양 tetrominos = [ [[1, 1, 1], [0, 1, 0]], [[0, 2, 2], [2, 2, 0]], [[3, 3, 0], [0, 3, 3]], [[4, 4], [4, 4]] ] # 게임 보드 설정 def create_board(): return [[0 for _ in range(screen_width // block_size)] for _ in range(screen_height // block_size)] # 테트로미노 회전 def rotate(shape): return [[shape[y][x] for y in range(len(shape))] for x in range(len(shape[0]) - 1, -1, -1)] # 테트로미노 이동 확인 def is_valid_move(board, shape, offset): offset_x, offset_y = offset for y, row in enumerate(shape): for x, cell in enumerate(row): if cell and (x + offset_x < 0 or x + offset_x >= len(board[0]) or y + offset_y >= len(board) or board[y + offset_y][x + offset_x]): return False return True # 테트로미노 고정 def place_piece(board, shape, offset): offset_x, offset_y = offset for y, row in enumerate(shape): for x, cell in enumerate(row): if cell: board[y + offset_y][x + offset_x] = cell # 게임 루프 def run_game(): board = create_board() current_piece = random.choice(tetrominos) piece_position = [len(board[0]) // 2, 0] running = True while running: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: new_position = [piece_position[0] - 1, piece_position[1]] if is_valid_move(board, current_piece, new_position): piece_position = new_position elif event.key == pygame.K_RIGHT: new_position = [piece_position[0] + 1, piece_position[1]] if is_valid_move(board, current_piece, new_position): piece_position = new_position elif event.key == pygame.K_DOWN: new_position = [piece_position[0], piece_position[1] + 1] if is_valid_move(board, current_piece, new_position): piece_position = new_position elif event.key == pygame.K_UP: rotated_piece = rotate(current_piece) if is_valid_move(board, rotated_piece, piece_position): current_piece = rotated_piece # 테트로미노 고정 및 새로운 조각 생성 new_position = [piece_position[0], piece_position[1] + 1] if not is_valid_move(board, current_piece, new_position): place_piece(board, current_piece, piece_position) current_piece = random.choice(tetrominos) piece_position = [len(board[0]) // 2, 0] else: piece_position = new_position # 게임 종료 조건 확인 if not is_valid_move(board, current_piece, piece_position): running = False # 보드 및 테트로미노 그리기 for y, row in enumerate(board): for x, cell in enumerate(row): pygame.draw.rect(screen, colors[cell], (x * block_size, y * block_size, block_size, block_size)) for y, row in enumerate(current_piece): for x, cell in enumerate(row): if cell: pygame.draw.rect(screen, colors[cell], ((piece_position[0] + x) * block_size, (piece_position[1] + y) * block_size, block_size, block_size)) pygame.display.flip() clock.tick(10) run_game() pygame.quit() |
댓글 없음:
댓글 쓰기