#!/usr/bin/env python3 import pygame import random import sys import os # Initialize Pygame pygame.init() # Game Settings WIDTH, HEIGHT = 800, 600 # Resized to 50% FPS = 60 PADDLE_WIDTH, PADDLE_HEIGHT = 100, 15 # Adjusted for smaller window BALL_RADIUS = 10 # Adjusted ball size BRICK_ROWS, BRICK_COLS = 7, 15 # 105 bricks BALL_SPEED = 5 PADDLE_SPEED = 7 STARTING_LIVES = 10 # Brick Settings BRICK_WIDTH = WIDTH // BRICK_COLS BRICK_HEIGHT = 35 # Adjusted height BRICK_Y_START = 100 # Adjusted for smaller screen # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Initialize Screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Brick Breaker") clock = pygame.time.Clock() # Load Banner Image try: banner_image = pygame.image.load("banner.png").convert_alpha() banner_image = pygame.transform.scale(banner_image, (WIDTH, 50)) # Scaled for smaller screen width except FileNotFoundError: print("Error: banner.png not found.") sys.exit() # Load All Brick Images brick_images = [] brick_image_directory = "bricks" for i in range(1, 106): try: image_path = os.path.join(brick_image_directory, f"brick_{i}.png") brick_image = pygame.image.load(image_path).convert_alpha() brick_image = pygame.transform.scale(brick_image, (BRICK_WIDTH, BRICK_HEIGHT)) brick_images.append(brick_image) except FileNotFoundError: print(f"Image brick_{i}.png not found in {brick_image_directory}.") sys.exit() # Classes class Paddle: def __init__(self): self.x = WIDTH // 2 - PADDLE_WIDTH // 2 self.y = HEIGHT - 40 self.width = PADDLE_WIDTH self.height = PADDLE_HEIGHT self.speed = PADDLE_SPEED self.rect = pygame.Rect(self.x, self.y, self.width, self.height) def move(self, dx): self.x += dx * self.speed self.x = max(0, min(self.x, WIDTH - self.width)) self.rect.x = self.x def draw(self, surface): pygame.draw.rect(surface, WHITE, self.rect) class Ball: def __init__(self, paddle): self.x = paddle.x + paddle.width // 2 self.y = paddle.y - BALL_RADIUS - 5 self.dx = 0 # Initially stationary self.dy = 0 # Ball starts stationary self.radius = BALL_RADIUS self.launched = False def launch(self): if not self.launched: self.dx = random.choice([-1, 1]) * BALL_SPEED self.dy = -BALL_SPEED # Shoot upward self.launched = True def move(self): if self.launched: self.x += self.dx self.y += self.dy # Wall Collision if self.x - self.radius <= 0 or self.x + self.radius >= WIDTH: self.dx = -self.dx if self.y - self.radius <= 0: self.dy = -self.dy def reset(self, paddle): self.x = paddle.x + paddle.width // 2 self.y = paddle.y - BALL_RADIUS - 5 self.dx = 0 self.dy = 0 self.launched = False def check_collision(self, paddle, bricks): # Paddle Collision if paddle.rect.collidepoint(self.x, self.y + self.radius): self.dy = -self.dy # Brick Collision hit_brick = None for brick in bricks: if brick.rect.collidepoint(self.x, self.y): self.dy = -self.dy hit_brick = brick break if hit_brick: bricks.remove(hit_brick) def draw(self, surface): pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), self.radius) class Brick: def __init__(self, x, y, image): self.x = x self.y = y self.width = BRICK_WIDTH self.height = BRICK_HEIGHT self.image = image self.rect = pygame.Rect(self.x, self.y, self.width, self.height) def draw(self, surface): surface.blit(self.image, (self.x, self.y)) # Helper Functions def create_bricks(): bricks = [] image_index = 0 for row in range(BRICK_ROWS): for col in range(BRICK_COLS): x = col * BRICK_WIDTH y = BRICK_Y_START + row * BRICK_HEIGHT bricks.append(Brick(x, y, brick_images[image_index])) image_index += 1 return bricks # Game Loop def main(): paddle = Paddle() ball = Ball(paddle) bricks = create_bricks() lives = STARTING_LIVES running = True game_over = False while running: clock.tick(FPS) # Event Handling for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: ball.launch() # Input keys = pygame.key.get_pressed() dx = 0 if keys[pygame.K_LEFT] or keys[pygame.K_a]: dx = -1 if keys[pygame.K_RIGHT] or keys[pygame.K_d]: dx = 1 # Update Game Objects if not game_over: paddle.move(dx) if not ball.launched: ball.reset(paddle) ball.move() ball.check_collision(paddle, bricks) # Ball falls below screen if ball.y - ball.radius > HEIGHT: lives -= 1 if lives > 0: ball.reset(paddle) else: game_over = True # Win Condition if not bricks: game_over = True # Draw Everything screen.fill(BLACK) banner_y = BRICK_Y_START - 60 screen.blit(banner_image, (0, banner_y)) paddle.draw(screen) ball.draw(screen) for brick in bricks: brick.draw(screen) # Display Lives font = pygame.font.SysFont("Arial", 18) lives_text = font.render(f"Lives: {lives}", True, RED) screen.blit(lives_text, (10, 10)) # Display Game Over or Victory if game_over: game_over_font = pygame.font.SysFont("Arial", 36) text = "YOU WIN!" if not bricks else "GAME OVER" text_surface = game_over_font.render(text, True, WHITE) text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2)) screen.blit(text_surface, text_rect) pygame.display.flip() # Run Game if __name__ == "__main__": main()