Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++

Breakout Game in a Win32 Console

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
5 Jun 2010CPOL3 min read 56.3K   2K   25   9
This article describes how to create a Breakout game in a Win32 console with Visual C++ 6.0
Image 1

Introduction

Designing apps in a Win32 Console environment is quite an adventure. There exist little or no game libraries. This simple app demonstrates how easy it is to create a simple breakout game in a Win32 Console with Visual C++ 6.0. But before we begin, let's talk about a header file called CTextMode.h.

The C Header File CTextMode.h contains the functions that are of outmost importance for the game to be able to position the cursor and text characters in the correct x and y location. Also included in the file CTextMode.h are the functions to draw simple dialogs, draw lines, plot points and of course set a text character's color.

Functions in CTextMode.h:

delay()         delays x milliseconds
gotoxy()        Goes to position on 80x25 console screen
setcolor()      Changes text and background color
clrbox()        draws colored box without frames
box()           draws framed box
gotoxy()        only works with printf(), cprintf() and not cout

The gotoxy(int x, int y) Method

The gotoxy(int x, int y) method is called upon to set the cursor at a specific x and y location.

C++
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	return;
}

The setcolor(WORD color) Method

The setcolor(WORD color) method is called upon to set the color of text characters.

C++
void setcolor(WORD color)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
	return;
}

The clrscr() Method

The clrscr() method is called upon to clear the screen or to fill the screen with specific color.

C++
void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), 
		dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
		dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
	return;
}  

The Game

Our class CBrickGame inherits function methods from the class CTextMode so we need to include this syntax:

C++
CBrickGame::CBrickGame( int i) : CTextMode()

Now after the inheritance, the function methods in CTextMode.h have become "one" with the function methods in our class CBrickGame. We can now treat those methods as if they were native to CBrickGame.
We have also placed function calls we want to be initialized at start up in the constructor so that when our class is being instantiated, those function methods get called and executed.

The project files are:

CBrickGame_main.cpp The main file
CBrickGame.cppThe class definition and method body file
CBrickGame.hThe class declaration file
CTextMode.hThe Console textmode graphics class

The CBrickGame::putPad(void) Method

The game code itself is quite simple. The user controls the pad by pressing down the left or right key on the keyboard. A function call is made to kbhit().

C++
if (kbhit())
{
	ch=getch();
	getBall();

	switch(ch)
	{
	    case LEFT:
			padX=padX-PADSTEP;
			if (padX<4) padX=4;
	    break;

	    case RIGHT:
			padX=padX+PADSTEP;
			if (padX>52) padX=52;
	    break;
         }
} 

After the user makes an input, the game will update the pad's position by making a function call to CBrickGame::putPad().

C++
void CBrickGame::putPad(void)
{
	int i;
	setcolor(111);

	for (i=0;i<6;i++)
	{
		gotoxy(padX+i,padY);cprintf("Ü");
	}
} 

The CBrickGame::moveBall(void) Method

The CBrickGame::moveBall(void) method is called upon to move the ball. Then the position of the ball is compared to the location of the bricks, when the locations match, i.e., when the ball hits bricks or hits the pad, the states of these objects are altered.

C++
void CBrickGame::moveBall(void)
{
	ballX=ballX+dirX;
	if (ballX<4 || ballX >55) dirX=-dirX;

	ballY=ballY+dirY;
	if (ballY<4 ) dirY=-dirY;
} 

The CBrickGame::setupBricks(void) Method

The CBrickGame::setupBricks(void) method is called upon to reset the brick hit flag and to put up a fresh batch of unhit bricks.

C++
void CBrickGame::setupBricks(void)
{
 for (i=0;i<=MAXBRICKS;i++) bricks[i]=true;
 setcolor(15);
 for (i=1;i<=MAXBRICKS;i++)
 {
     if (i>0 && i<=6){gotoxy(8*i,6);cprintf("ÛÛÛÛÛÛÛ");}
     if (i>6 && i<=12){gotoxy(8*(i-6),8);cprintf("ÛÛÛÛÛÛÛ");}
     if (i>12 && i<=18){gotoxy(8*(i-12),10);cprintf("ÛÛÛÛÛÛÛ");}
     if (i>18 && i<=24){gotoxy(8*(i-18),12);cprintf("ÛÛÛÛÛÛÛ");}
 }
}

The CBrickGame::checkBricks(void) Method

The CBrickGame::checkBricks(void) method is called upon to check if a specific brick has been hit. If the brick was hit, then its flag is set to bricks[i]=false and is cleared from the ball's path.

C++
void CBrickGame::checkBricks(void)
{
	for (i=1;i<=6;i++)
	{
	   if (ballX>(8*i) && ballX<8*(i+1))
	   {
		   if (ballY==6 && bricks[i]==true)
		   {
		     bricks[i]=false;
		     gotoxy(8*i,6);cprintf("       ");
			 processHits();
		   }

		   if (ballY==8 && bricks[i+6]==true)
		   {
		     bricks[i+6]=false;
		     gotoxy(8*i,8);cprintf("       ");
			 processHits();		   }

		   if (ballY==10 && bricks[i+12]==true)
		   {
		     bricks[i+12]=false;
		     gotoxy(8*i,10);cprintf("       ");
			 processHits();		   }

		   if (ballY==12 && bricks[i+18]==true)
		   {
		     bricks[i+18]=false;
		     gotoxy(8*i,12);cprintf("       ");
			 processHits();		   }
	   }
	}
}

The CBrickGame::gameMain(void) Method is our Message Pump

Keyboard input is the games' only means of control. In here, key codes are processed and translated into instructions.

C++
void CBrickGame::gameMain(void)
{
  if (kbhit())
  {
	ch=getch();
	op=toupper((char)ch);
	getPad();
	getBall();

	switch (ch)
	{
	    case LEFT:
			padX=padX-PADSTEP;
			//ballX=padX;
			if (padX<4) padX=4;
		break;

		case RIGHT:
			padX=padX+PADSTEP;
 			//ballX=padX;
			if (padX>52) padX=52;
		break;

		case UP:
		break;

		case DOWN:
		break;

		case ENTER:
		break;

		case SPACE:
		break;

		case F1:
			help();
			resumeGame();
		break;

		case TAB:
			autoplay=-autoplay;
		break;
	}
  }
}

The Main File

Finally the file CBrickGame_main.cpp is where we create an instance for our class and run our application.

C++
//CBrickGame.cpp

#include "CBrickGame.h"

int main()
{
  CBrickGame *brickGame = new CBrickGame(1);

  return 0;
}

And that is how easy it is to create a simple breakout game in a Win32 Console.

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
General[My vote of 1] 30 years too late Pin
Johnny J.3-Jun-10 21:26
professionalJohnny J.3-Jun-10 21:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.