import pygame
import random
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()