编辑代码

import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WIDTH = 800
HEIGHT = 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("打砖块")

# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0)]

# 挡板设置
paddle_width = 100
paddle_height = 20
paddle_speed = 10

# 小球设置
ball_radius = 10
ball_speed_x = 5
ball_speed_y = -5

# 砖块设置
brick_width = 75
brick_height = 30
brick_rows = 5
brick_cols = 10

# 得分
score = 0

class Paddle:
    def __init__(self):
        self.x = WIDTH // 2 - paddle_width // 2
        self.y = HEIGHT - 50
        self.speed = paddle_speed
        
    def draw(self):
        pygame.draw.rect(win, BLUE, (self.x, self.y, paddle_width, paddle_height))
        
    def move(self, direction):
        if direction == "left" and self.x > 0:
            self.x -= self.speed
        elif direction == "right" and self.x < WIDTH - paddle_width:
            self.x += self.speed

class Ball:
    def __init__(self):
        self.reset()
        
    def reset(self):
        self.x = WIDTH // 2
        self.y = HEIGHT // 2
        self.speed_x = ball_speed_x * random.choice([-1, 1])
        self.speed_y = ball_speed_y
        
    def move(self):
        self.x += self.speed_x
        self.y += self.speed_y
        
        # 碰撞墙壁
        if self.x <= 0 or self.x >= WIDTH:
            self.speed_x *= -1
        if self.y <= 0:
            self.speed_y *= -1
            
    def draw(self):
        pygame.draw.circle(win, RED, (self.x, self.y), ball_radius)

class Brick:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.active = True
        
    def draw(self):
        if self.active:
            pygame.draw.rect(win, self.color, (self.x, self.y, brick_width, brick_height))

# 初始化游戏对象
paddle = Paddle()
ball = Ball()

# 创建砖块
bricks = []
for row in range(brick_rows):
    for col in range(brick_cols):
        brick_x = col * (brick_width + 5) + 30
        brick_y = row * (brick_height + 5) + 50
        color = random.choice(COLORS)
        bricks.append(Brick(brick_x, brick_y, color))

# 游戏循环
clock = pygame.time.Clock()
running = True
game_over = False

while running:
    win.fill((0, 0, 0))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        paddle.move("left")
    if keys[pygame.K_RIGHT]:
        paddle.move("right")
        
    # 小球移动
    if not game_over:
        ball.move()
        
    # 检测小球与挡板碰撞
    if (ball.y + ball_radius >= paddle.y and 
        paddle.x <= ball.x <= paddle.x + paddle_width):
        ball.speed_y *= -1
        # 根据碰撞位置调整水平速度
        offset = (ball.x - paddle.x) / paddle_width - 0.5
        ball.speed_x = offset * 10
        
    # 检测小球与砖块碰撞
    for brick in bricks:
        if brick.active and (
            brick.x <= ball.x <= brick.x + brick_width and
            brick.y <= ball.y <= brick.y + brick_height
        ):
            brick.active = False
            ball.speed_y *= -1
            score += 10
            break
            
    # 检测游戏失败
    if ball.y + ball_radius >= HEIGHT:
        game_over = True
        text = pygame.font.SysFont(None, 100).render("Game Over!", True, WHITE)
        win.blit(text, (WIDTH//2 - 150, HEIGHT//2 - 50))
        
    # 检测游戏胜利
    if all(not brick.active for brick in bricks):
        game_over = True
        text = pygame.font.SysFont(None, 100).render("You Win!", True, WHITE)
        win.blit(text, (WIDTH//2 - 150, HEIGHT//2 - 50))
        
    # 绘制游戏元素
    paddle.draw()
    ball.draw()
    for brick in bricks:
        brick.draw()
        
    # 显示得分
    font = pygame.font.SysFont(None, 36)
    score_text = font.render(f"Score: {score}", True, WHITE)
    win.blit(score_text, (10, 10))
    
    pygame.display.update()
    clock.tick(60)

pygame.quit()