
In this article, we will build the famous Brick Breaker Game in C++. The goal of this game is to smash all the bricks on the screen while keeping the ball safe from falling down the boundary. This will be a console-based game written entirely in C++. So, even if have some familiarity with the language, you will be able to follow this article and build the game easily.
Features
- The player has to keep the ball safe by bouncing it back using the paddle.
- Five lives are given, after which the game is over.
- The player can restart the game.
Complete Code for Brick Breaker Game in C++
We will use Code::Blocks IDE to develop this program. Open the Code::Blocks IDE and create a new project with the name brickbreaker, now in the main.cpp file paste the following code and run it.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
//used to move the cursor to a specific position on the screen
void gotoxy(int x, int y){
COORD c; // a structure having two members which are the X and Y coordinates
c.X = x; // Locating its x coordinate
c.Y = y; // Locating its y coordinate
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); // Set the position for next thing to be printed
}
//used to hide the cursor
void hideCursor(){
CONSOLE_CURSOR_INFO cursor; // Create a CONSOLE_CURSOR_INFO object
cursor.dwSize = 100; // Assigning size of cursor
cursor.bVisible = false; // Making cursor invisible
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); // Set the cursor for next thing to be printed
}
//Some variables used in the game
int life;
int const screenHeight = 20;
int const screenWidth = 30;
int map[screenHeight][screenWidth];
char bricks[8][18] = {"##..##.##.######.",
"##..##.##...##...",
"##..##.##...##...",
"######.##...##...",
"##..##.##...##...",
"##..##.##...##...",
"##..##.##...##..."};
char temp[8][18] = {"##..##.##.######.",
"##..##.##...##...",
"##..##.##...##...",
"######.##...##...",
"##..##.##...##...",
"##..##.##...##...",
"##..##.##...##..."};
bool decre_life; //used to decrease the life of the player when the ball hits the bottom of the screen
class Paddle{
public:
int x;
int y;
int speed;
char dir;
int delay;
int count_delay;
void draw(){
for(int i = 0; i < 9; i++)
map[y][x+i] = 1; // 1 is the ASCII code the character to make the paddle
}
void move(){
if(count_delay == delay){ //used to make the paddle move slower
if(dir == 'L' && x-speed > 0){ // used to make the paddle move left
x -= speed;
}else if(dir == 'R' && x+speed < screenWidth-9){ //used to make the paddle move right
x += speed;
}
count_delay = 0;
}
count_delay++;
if(decre_life){
dir = 'S';
x = screenWidth/2-5;
y = screenHeight - screenHeight/7-1;
decre_life = false;
}
}
};
class Ball{
public:
int x;
int y;
int speed;
int dir;
void draw(){
map[y][x] = 5;
}
void move(){
if(dir == 0 && !collision(x-speed, y-speed)){ //used to make the ball move in the top left direction
x -= speed;
y -= speed;
}else if(dir == 1 && !collision(x+speed, y-speed)){ //top right direction
x += speed;
y -= speed;
}else if(dir == 2 && !collision(x-speed, y+speed)){ //bottom left direction
x -= speed;
y += speed;
}else if(dir == 3 && !collision(x+speed, y+speed)){ //bottom right direction
x += speed;
y += speed;
}
}
//used to check if the ball collides with the paddle or the bricks
bool collision(int fx, int fy){
if(map[fy][x] == 8){ //used to check if the ball collides with the paddle
decre_life = true;
x = screenWidth/2-1;
y = screenHeight - screenHeight/7-3;
dir = 4;
life--;
}
//used to check if the ball collides with the bricks
else if(map[fy][fx] != 0 || map[y][fx] != 0 || map[fy][x] != 0 || map[fy][fx] == 2 || map[y][fx] == 2 || map[fy][x] == 2){
if(map[fy][fx] == 2) bricks[fy-2][fx-6] = '.';
if(map[y][fx] == 2) bricks[y-2][fx-6] = '.';
if(map[fy][x] == 2) bricks[fy-2][x-6] = '.';
if(map[y][fx] != 0) bounce(fx,y);
else if(map[fy][x] != 0) bounce(x,fy);
else if(map[fy][fx] != 0) bounce(fx,fy);
return true;
}
return false;
}
//used to change the direction of the ball when it collides with the paddle or wall
void bounce(int fx, int fy){
if(dir == 0){
if(fx < x) dir = 1;
else if(fy < y) dir = 2;
else if(fx < x && fy < y) dir = 0;
}else if(dir == 1){
if(fx > x) dir = 0;
else if(fy < y) dir = 3;
else if(fx > x && fy < y) dir = 1;
}else if(dir == 2){
if(fx < x) dir = 3;
else if(fy > y) dir = 0;
else if(fx < x && fy > y) dir = 2;
}else if(dir == 3){
if(fx > x) dir = 2;
else if(fy > y) dir = 1;
else if(fx > x && fy > y) dir = 3;
}
}
};
//used to draw the bricks
void brick(){
for(int i = 0; i < 7; i++){
for(int j = 0; j < 17; j++){
if(bricks[i][j] == '#') map[i+2][j+6] = 2; // 2 is the ASCII code the character to make the bricks
}
}
}
Paddle paddle;
Ball ball;
void setup(){
srand(time(NULL));
decre_life = false;
life = 5;
paddle.x = screenWidth/2-5;
paddle.y = screenHeight - screenHeight/7-1;
paddle.speed = 1;
paddle.delay = 1;
ball.x = screenWidth/2;
ball.y = screenHeight - screenHeight/7-2;
ball.speed = 1;
ball.dir = rand()%4;
}
//used to make the wall
void wall(){
for(int i = 0; i < screenHeight; i++){
for(int j = 0; j < screenWidth; j++){
if(j == 0 || j == screenWidth-1) map[i][j] = 9; // 9 is the ASCII code the character to make the wall
else if(i == 0) map[i][j] = 7;
else if(i == screenHeight-1) map[i][j] = 8; // 8 is the ASCII code the character to make the bottom wall
else map[i][j] = 0;
}
}
}
void layout(){
wall();
paddle.draw();
ball.draw();
brick();
}
void display(){
gotoxy(2,1); cout << " LIFE: " << life;
for(int i = 0; i < screenHeight; i++){
for(int j = 0; j < screenWidth; j++){
gotoxy(j+2, i+3);
if(map[i][j] == 9) cout << char(219);
if(map[i][j] == 1) cout << char(219);
if(map[i][j] == 2) cout << char(233);
if(map[i][j] == 7) cout << char(219);
if(map[i][j] == 8) cout << char(240);
if(map[i][j] == 5) cout << char(254);
if(map[i][j] == 0) cout << char(32);
}
}
}
void input(){
if(kbhit()){
switch(getch()){
case 75:
paddle.dir = 'L';
break;
case 77:
paddle.dir = 'R';
break;
}
if(ball.dir == 4) ball.dir = rand()%2;
}
}
void movements(){
paddle.move();
ball.move();
}
void gameOver(){
system("cls");
cout << " GAMEOVER " << endl;
cout << " Do you want to play again?y/n" <<endl;
}
int main(){
system("color E4");
system("title Brick Breaker Game");
hideCursor();
setup();
while(true){
while(life > 0){
display();
layout();
input();
movements();
}
char ch;
gameOver();
cin >> ch;
if(ch == 'y' || ch == 'Y')
{
system("cls");
for(int i=0; i<8; i++){
for(int j=0; j<18; j++){
bricks[i][j] = temp[i][j];
}
}
life = 5;
}
else{
return 0;
}
}
}
Output for Brick Breaker Game in C++
Image Output:

Video Output:
Conclusion
This brings us to the end of this article on the Brick Breaker game source code in C++. Go ahead and customize the code according to your need and have fun with it. You can also try to add more features like win count or the number of rounds the player wants to play.
Thank you for visiting our website.
Also Read:
- You Can Now Run AI Fully Offline on Your Phone — Google’s Gemma 4 Just Changed Everything
- I Built a 24×7 AI Blogging System for WordPress Using Python (Free) — Full Code Inside
- This Reddit User “Hacked” AI With Simple Tricks… And The Results Are Insane
- One “rm -rf” Command Almost Wiped Out $100 Million Worth of Toy Story 2
- How to Make Money with ChatGPT in 2026: A Real Guide That Still Works
- PicoClaw vs OpenClaw: The Tiny AI That Could Replace Powerful AI Agents
- Oracle Layoffs 2026: People Woke Up to an Email… and Lost Their Jobs Instantly
- X’s New Video Update Is Breaking a Basic Feature — And Users Are Not Happy
- The Most Shocking Military Tech Yet: Robot Soldiers That Could Change Warfare Forever
- Sora Shutdown: The Reality Check That Shook AI Video — And What Comes Next
- Aya Expanse supports multiple languages for diverse global applications
- Alibaba releases Page Agent on GitHub for public access
- Google Sheets Gemini reaches new levels of performance and accuracy
- Artificial intelligence boosts cardiac care in rural Australian communities
- NVIDIA GTC 2026 Offers Insights into Future Artificial Intelligence Developments
- Google DeepMind Updates Satellite Embedding Dataset with 2025 Data
- Enhancing hierarchical instruction in advanced large language models
- Meta supports community development near its data centers through grants
- Exploring the world of underwater robotics through coding techniques
- ABB Robotics partners with NVIDIA for large scale physical AI solutions
- Why All AI Models Are Slowly Becoming the Same Model
- Aam Aadmi vs Corrupt System: How ChatGPT Helped One Guy Expose Govt Fraud, The Story: “Ravi and The Missing Light Pole”
- ChatGPT Asked a person to commit suicide to solve the problem
- Viral Moment: China’s AgiBot X2 Makes History With World’s First Webster Backflip
- Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human Extinction
- What Is Albania’s World-First AI-Generated Minister and How Does It Work?
- Does ChatGPT believe in God? ChatGPT’s Personal Opinion
- ChatGPT vs Human: The Breath-Holding Chat That Ends in “System Failure”
- What Is Vibe Coding? The Future of No-Code Programming and Its Impact on Software Developers
- Struggling to Generate Ghibli-Style AI Images? Here’s the Real Working Tool That Others Won’t Tell You About!



