import pygame import random # 초기화 pygame.init() # 화면 설정 width = 800 height = 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("블록 게임") # 색상 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # 블록 설정 block_width = 50 block_height = 20 block_rows = 5 block_cols = 10 # 패들 설정 paddle_width = 100 paddle_height = 10 paddle_x = width // 2 - paddle_width // 2 paddle_y = height - 40 # 공 설정 ball_radius = 10 ball_x = width // 2 ball_y = height // 2 ball_dx = 3 ball_dy = -3 # 블록 생성 blocks = [] for row in range(block_rows): for col in range(block_cols): block = pygame.Rect(col * (block_width + 5) + 50, row * (block_height + 5) + 50, block_width, block_height) blocks.append(block) # 게임 루프 running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 패들 이동 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and paddle_x > 0: paddle_x -= 5 if keys[pygame.K_RIGHT] and paddle_x < width - paddle_width: paddle_x += 5 # 공 이동 ball_x += ball_dx ball_y += ball_dy # 벽 충돌 검사 if ball_x <= 0 or ball_x >= width: ball_dx *= -1 if ball_y <= 0: ball_dy *= -1 if ball_y >= height: running = False # 패들 충돌 검사 if ball_y + ball_radius >= paddle_y and ball_x >= paddle_x and ball_x <= paddle_x + paddle_width: ball_dy *= -1 # 블록 충돌 검사 for block in blocks[:]: if block.collidepoint(ball_x, ball_y): blocks.remove(block) ball_dy *= -1 break # 화면 그리기 screen.fill(BLACK) pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_width, paddle_height)) pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), ball_radius) for block in blocks: pygame.draw.rect(screen, RED, block) pygame.display.flip() clock.tick(60) pygame.quit()