Introduction
In this article, we will develop a Brick Breaker game in Python. We will use Pygame to develop this Brick Breaker game. Pygame is a set of Python modules used for writing video games. It includes computer graphics and sound libraries designed to be used with Python to develop games. Let’s get started!
Code for Brick Breaker Game in Python
Create a Python file and save it as brickbreaker.py and paste the following code into it. Save the file and run it.
import pygame #Start the game pygame.init() size = (600, 600) screen = pygame.display.set_mode(size) pygame.display.set_caption("Brick Breaker Game") floor = pygame.Rect(100, 550, 200, 10) ball = pygame.Rect(50, 250, 10, 10) score = 0 move = [1, 1] continueGame = True GREEN = (28, 252, 106) WHITE = (255, 255, 255) BLACK = (0,0,0) PINK = (252, 3, 152) ORANGE= (252, 170, 28) RED = (255, 0, 0) # bricks b1 = [pygame.Rect(1 + i * 100, 60, 98, 38) for i in range(6)] b2 = [pygame.Rect(1 + i * 100, 100, 98, 38) for i in range(6)] b3 = [pygame.Rect(1 + i * 100, 140, 98, 38) for i in range(6)] # Draw bricks on screen def draw_brick(bricks): for i in bricks: pygame.draw.rect(screen, ORANGE, i) while continueGame: for event in pygame.event.get(): if event.type == pygame.QUIT: continueGame = False screen.fill(BLACK) pygame.draw.rect(screen, PINK, floor) font = pygame.font.Font(None, 34) text = font.render("CURRENT SCORE: " + str(score), 1, WHITE) screen.blit(text, (180, 10)) # floor move if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: if floor.x < 540: floor.x = floor.x + 3 if event.key == pygame.K_LEFT: if floor.x > 0: floor.x = floor.x - 3 # Bricks draw_brick(b1) draw_brick(b2) draw_brick(b3) # Ball ball.x = ball.x + move[0] ball.y = ball.y + move[1] if ball.x > 590 or ball.x < 0: move[0] = -move[0] if ball.y <= 3: move[1] = -move[1] if floor.collidepoint(ball.x, ball.y): move[1] = -move[1] if ball.y >= 590: font = pygame.font.Font(None, 74) text = font.render("Game Over!", 1, RED) screen.blit(text, (150, 300)) font = pygame.font.Font(None, 50) text = font.render("YOUR FINAL SCORE: " + str(score), 1, GREEN) screen.blit(text, (100, 350)) pygame.display.flip() pygame.time.wait(5000) break; pygame.draw.rect(screen, WHITE, ball) for i in b1: if i.collidepoint(ball.x, ball.y): b1.remove(i) move[0] = -move[0] move[1] = -move[1] score = score + 1 for i in b2: if i.collidepoint(ball.x, ball.y): b2.remove(i) move[0] = -move[0] move[1] = -move[1] score = score + 1 for i in b3: if i.collidepoint(ball.x, ball.y): b3.remove(i) move[0] = -move[0] move[1] = -move[1] score = score + 1 if score == 18: font = pygame.font.Font(None, 74) text = font.render("YOU WON THE GAME", 1, GREEN) screen.blit(text, (150, 350)) pygame.display.flip() pygame.time.wait(3000) break; pygame.time.wait(1) pygame.display.flip() #End the game pygame.quit()
Output for Brick Breaker Game in Python:
Image output:
Video output:
Thank you for visiting our website.
Also Read:
- Create your own ChatGPT with Python
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter
- Scientific Calculator in Python using Tkinter
- GUI Chat Application in Python Tkinter