import pygame
import sys

pygame.init()

# 画面サイズ
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("ブロック崩し")

# 色定義
WHITE = (255, 255, 255)
RED = (255, 100, 100)
GREEN = (100, 255, 100)
BLUE = (100, 100, 255)
CYAN = (100, 255, 255)
BLACK = (0, 0, 0)

# フォント
font = pygame.font.SysFont(None, 36)

# ゲーム初期化関数
def init_game():
    global paddle, ball, ball_speed_x, ball_speed_y, blocks, scores, score, lives, game_over, game_clear
    paddle = pygame.Rect(270, 450, 100, 10)
    ball = pygame.Rect(320, 240, 10, 10)
    ball_speed_x = 3
    ball_speed_y = -3
    score = 0
    lives = 3
    game_over = False
    game_clear = False

    # ブロック設定(カラフル + スコア付き)
    blocks = []
    scores = []
    colors = [RED, GREEN, BLUE]
    point_values = [3, 2, 1]
    for row in range(3):
        for col in range(5):
            x = col * (100 + 10) + 35
            y = row * (30 + 10) + 40
            blocks.append(pygame.Rect(x, y, 100, 30))
            scores.append((colors[row], point_values[row])) # 色と点数

init_game()
clock = pygame.time.Clock()

# メッセージ表示関数
def draw_message(text, subtext=""):
    text_surf = font.render(text, True, CYAN)
    text_rect = text_surf.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 20))
    screen.blit(text_surf, text_rect)
    if subtext:
        sub_surf = font.render(subtext, True, CYAN)
        sub_rect = sub_surf.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 20))
        screen.blit(sub_surf, sub_rect)

# メインループ
running = True
while running:
    screen.fill(BLACK)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if (game_clear or game_over) and event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                init_game()

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        paddle.x -= 5
    if keys[pygame.K_RIGHT]:
        paddle.x += 5

    if not (game_clear or game_over):
        # ボール移動
        ball.x += ball_speed_x
        ball.y += ball_speed_y

        # 壁反射
        if ball.left <= 0 or ball.right >= WIDTH:
            ball_speed_x *= -1
        if ball.top <= 0:
            ball_speed_y *= -1

        # パドル反射
        if ball.colliderect(paddle):
            ball_speed_y *= -1

        # ブロック衝突
        hit_index = ball.collidelist(blocks)
        if hit_index != -1:
            del blocks[hit_index]
            color, point = scores.pop(hit_index)
            score += point
            ball_speed_y *= -1

        # ボール落下
        if ball.bottom >= HEIGHT:
            lives -= 1
            if lives > 0:
                ball.x, ball.y = 320, 240
                ball_speed_y = -3
            else:
                game_over = True

        # クリア判定
        if not blocks:
            game_clear = True

    # 描画
    pygame.draw.rect(screen, WHITE, paddle)
    pygame.draw.ellipse(screen, CYAN, ball)

    for i, block in enumerate(blocks):
        color, _ = scores[i]
        pygame.draw.rect(screen, color, block)

    score_text = font.render(f"Score: {score} Lives: {lives}", True, WHITE)
    screen.blit(score_text, (20, 10))

    if game_clear:
        draw_message("YOU WIN!", "Press R to Replay")
    elif game_over:
        draw_message("GAME OVER", "Press R to Retry")

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()