In this simple game, you control a paddle and try to destroy bricks with a ball by deflecting the ball when it comes to you.
Click here for a demonstration.
To play the game, copy the code below and copy it into your python file and run the game.
Copy the code for FREE!:
import pygame import sys import random # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 600, 400 PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10 BALL_RADIUS = 10 BRICK_WIDTH, BRICK_HEIGHT = 50, 20 WHITE = (255, 255, 255) BLUE = (0, 0, 255) RED = (255, 0, 0) # Create the game window window = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Brick Breaker Game") # Create the paddle paddle = pygame.Rect(WIDTH // 2 - PADDLE_WIDTH // 2, HEIGHT - PADDLE_HEIGHT - 10, PADDLE_WIDTH, PADDLE_HEIGHT) # Create the ball ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS // 2, HEIGHT // 2 - BALL_RADIUS // 2, BALL_RADIUS, BALL_RADIUS) ball_speed = [5, -5] # Initial speed of the ball # Create the bricks def create_bricks(): bricks = [] for row in range(5): for col in range(WIDTH // BRICK_WIDTH): brick = pygame.Rect(col * BRICK_WIDTH, row * BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT) bricks.append(brick) return bricks bricks = create_bricks() # Score score = 0 # Function to reset the ball and bricks def reset_game(): global ball_speed, score, bricks ball_speed = [5, -5] score = 0 ball.x = WIDTH // 2 - BALL_RADIUS // 2 ball.y = HEIGHT // 2 - BALL_RADIUS // 2 bricks = create_bricks() # Game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Move the paddle keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and paddle.left > 0: paddle.x -= 5 if keys[pygame.K_RIGHT] and paddle.right < WIDTH: paddle.x += 5 # Move the ball ball.x += ball_speed[0] ball.y += ball_speed[1] # Ball collision with walls if ball.left <= 0 or ball.right >= WIDTH: ball_speed[0] = -ball_speed[0] if ball.top <= 0: ball_speed[1] = -ball_speed[1] # Ball collision with paddle if ball.colliderect(paddle) and ball_speed[1] > 0: ball_speed[1] = -ball_speed[1] # Ball misses the paddle (reset the game) if ball.top >= HEIGHT: reset_game() # Ball collision with bricks for brick in bricks: if ball.colliderect(brick): bricks.remove(brick) ball_speed[1] = -ball_speed[1] score += 1 # Reset all bricks if the player breaks them all if len(bricks) == 0: reset_game() # Draw everything window.fill(WHITE) pygame.draw.rect(window, BLUE, paddle) pygame.draw.ellipse(window, RED, ball) for brick in bricks: pygame.draw.rect(window, BLUE, brick) # Draw the score font = pygame.font.Font(None, 36) score_text = font.render("Score: " + str(score), True, BLUE) window.blit(score_text, (10, 10)) # Update the display pygame.display.flip() # Set the frames per second pygame.time.Clock().tick(60)