In this game, you control a paddle and bounce around a ball to break bricks. Some bricks are harder to break than others and give more points, you have 3 lives.

View an example here: Brick Breaker!

You control the paddle with arrow keys, in order to play the game, copy and paste the code into your python file and run the code. Enjoy!

 

Copy and paste the code for FREE!:

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up the screen
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Brick Breaker")

# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# Set up the paddle
paddle_width, paddle_height = 100, 20
paddle_x = WIDTH // 2 - paddle_width // 2
paddle_y = HEIGHT - 50
paddle_speed = 7

# Set up the ball
ball_radius = 10
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_dx = 5 * random.choice((1, -1))
ball_dy = -5
ball_speed = 7

# Set up the bricks
brick_width, brick_height = 80, 30
num_bricks_rows = 5
num_bricks_cols = 10
bricks = {}
for i in range(num_bricks_rows):
    for j in range(num_bricks_cols):
        brick_x = j * (brick_width + 5) + 30
        brick_y = i * (brick_height + 5) + 30
        brick_rect = pygame.Rect(brick_x, brick_y, brick_width, brick_height)
        if random.random() < 0.05:
            bricks[(brick_x, brick_y)] = "multi-hit"
        elif random.random() < 0.05:
            bricks[(brick_x, brick_y)] = "extra-points"
        else:
            bricks[(brick_x, brick_y)] = None

# Set up the power-ups
power_ups = []

# Set up the game variables
score = 0
lives = 3
game_over = False

# Set up the game font
font = pygame.font.Font(None, 36)

# Function to display text on screen
def draw_text(text, color, x, y):
    text_surface = font.render(text, True, color)
    text_rect = text_surface.get_rect()
    text_rect.center = (x, y)
    screen.blit(text_surface, text_rect)

# Function to create a new power-up
def create_power_up(x, y):
    power_up_rect = pygame.Rect(x, y, 20, 20)
    power_up_rect.type = random.choice(("paddle_size", "extra_ball"))
    power_ups.append(power_up_rect)

# Main game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    if not game_over:
        # Move the paddle
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and paddle_x > 0:
            paddle_x -= paddle_speed
        if keys[pygame.K_RIGHT] and paddle_x < WIDTH - paddle_width:
            paddle_x += paddle_speed

        # Move the ball
        ball_x += ball_dx
        ball_y += ball_dy

        # Ball collision with walls
        if ball_x - ball_radius < 0 or ball_x + ball_radius > WIDTH:
            ball_dx *= -1
        if ball_y - ball_radius < 0:
            ball_dy *= -1

        # Ball collision with paddle
        if ball_y + ball_radius > paddle_y and paddle_x < ball_x < paddle_x + paddle_width:
            ball_dy *= -1

        # Ball collision with bricks
        for brick_pos, special in list(bricks.items()):
            brick_rect = pygame.Rect(brick_pos[0], brick_pos[1], brick_width, brick_height)
            if brick_rect.colliderect(pygame.Rect(ball_x - ball_radius, ball_y - ball_radius, ball_radius * 2, ball_radius * 2)):
                if special == "multi-hit":
                    bricks[brick_pos] = "hit"
                elif special == "extra-points":
                    score += 50
                else:
                    del bricks[brick_pos]
                    score += 10
                ball_dy *= -1

        # Ball collision with power-ups
        for power_up in power_ups:
            if power_up.colliderect(pygame.Rect(ball_x - ball_radius, ball_y - ball_radius, ball_radius * 2, ball_radius * 2)):
                if power_up.type == "paddle_size":
                    paddle_width += 20
                elif power_up.type == "extra_ball":
                    extra_ball = True
                power_ups.remove(power_up)

        # Check for game over
        if ball_y > HEIGHT:
            lives -= 1
            if lives <= 0:
                game_over = True
            else:
                ball_x = WIDTH // 2
                ball_y = HEIGHT // 2
                ball_dx = 5 * random.choice((1, -1))
                ball_dy = -5

    # Clear the screen
    screen.fill(BLACK)

    # Draw the paddle
    pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_width, paddle_height))

    # Draw the ball
    pygame.draw.circle(screen, WHITE, (ball_x, ball_y), ball_radius)

    # Draw the bricks
    for brick_pos, special in bricks.items():
        if special == "multi-hit":
            pygame.draw.rect(screen, RED, pygame.Rect(brick_pos[0], brick_pos[1], brick_width, brick_height))
        elif special == "extra-points":
            pygame.draw.rect(screen, YELLOW, pygame.Rect(brick_pos[0], brick_pos[1], brick_width, brick_height))
        else:
            pygame.draw.rect(screen, random.choice((RED, GREEN, BLUE)), pygame.Rect(brick_pos[0], brick_pos[1], brick_width, brick_height))

    # Draw the power-ups
    for power_up in power_ups:
        pygame.draw.rect(screen, random.choice((RED, GREEN, BLUE, YELLOW)), power_up)

    # Draw the score and lives
    draw_text(f"Score: {score}", WHITE, 70, 30)
    draw_text(f"Lives: {lives}", WHITE, WIDTH - 70, 30)

    # Check for win
    if len(bricks) == 0:
        draw_text("You Win!", WHITE, WIDTH // 2, HEIGHT // 2)
        game_over = True

    # Check for game over
    if game_over:
        draw_text("Game Over", WHITE, WIDTH // 2, HEIGHT // 2)

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    pygame.time.Clock().tick(60)

Leave a Reply

Your email address will not be published. Required fields are marked *