Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / Win32

Pong in a Win32 Console

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
15 Jul 2010CPOL2 min read 44.9K   1.9K   20  
Pong in a Win32 Console

// *********************************************************************
// *     This software is made available only to individuals and only  *
// *     for educational purposes. Any and all commercial use is       *
// *     stricly prohibited.                                           *
// *********************************************************************
//**********************************************************************
//* Disclaimer: Any borrowed code used in this                         *
//*             program is the property of the                         *
//*             code originator. credit to them.                       *
//*                                                                    *
//*                                                                    *
//*   Unfinished                                                       *
//*   WARNING:                                                         *
 //*                                                                    *
//*                                                                    *
//**********************************************************************



#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <iostream.h>
#include <math.h>
#include <fstream.h>

#define Black 0
#define Blue 1
#define Green 2
#define Cyan 3
#define Red 4
#define Magenta 5
#define Yellow 6
#define White 7
#define Gray 8
#define LightBlue 9
#define LightGreen 10
#define LightCyan 11
#define LightRed 12
#define LightMagenta 13
#define LightYellow 14
#define BrightWhite 15



		int temp,kbChar;
int _key=0;

		/*
		void getKey();
		void gotoxy(int x, int y);
		void setcolor(WORD color);
		void textColor(unsigned char fColor,unsigned char bColor);
		void clrscr();
		void delay(int milliseconds);
		void txtPlot(int x, int y,int color);
		void txtLine( int xx1, int yy1, int xx2, int yy2 ,int color);


		*/
		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************

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

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


		//
		//     colors:
		//     0 = Black
		//     1 = Blue
		//     2 = Green
		//     3 = Cyan
		//     4 = Red
		//     5 = Magenta
		//     6 = Yellow
		//     7 = LightGray
		//     8 = DarkGray
		//     9 = LightBlue
		//     10 = LightGreen
		//     11 = LightCyan
		//     12 = LightRed
		//     13 = LightMagenta
		//     14 = LightYellow
		//     15 = White


		//

		void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
		{
		   int color=16*BackGroundColor+ForeGroundColor;
		   setcolor(color);
}



		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************

		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;
		}
		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************

		void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[])
		{  unsigned i,j,m;
			{

			   m=(sx-x);                       //differential
			   j=m/8;                          //adjust
			   j=j-1;                          //more adjustment
			   gotoxy(x,y);cprintf("�");       //Top left corner of box
			   gotoxy(sx,y);cprintf("�");      //Top right corner of box
			   gotoxy(x,sy);cprintf("�");      //Bottom left corner of box
			   gotoxy(sx,sy);cprintf("�");     //Bottom right corner of box

			   for (i=x+1;i<sx;i++)
			   {
				  gotoxy(i,y);cprintf("�");     // Top horizontol line
				  gotoxy(i,sy);cprintf("�");    // Bottom Horizontal line
			   }

			   for (i=y+1;i<sy;i++)
			   {
				  gotoxy(x,i);cprintf("�");     //Left Vertical line
				  gotoxy(sx,i);cprintf("�");    //Right Vertical Line
			   }

				  gotoxy(x+j,y);cprintf(text_); //put Title
				  gotoxy(1,24);
			}
		}
		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************

		void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol)
		{
			int x,y;
			setcolor(bkcol);                       //Set to color bkcol

			for (y=y1;y<y2;y++)                    //Fill Y Region Loop
			{
				for (x=x1;x<x2;x++)               //Fill X region Loop
				{
				  gotoxy(x,y);cprintf(" ");       //Draw Solid space
				}
			}
		}
		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************

		void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,
				 unsigned char col, unsigned char col2,unsigned char bkcol,char text_[])
		{
			clrbox(x,y,sx,sy,bkcol);
			box(x,y,sx,sy,col,col2,text_);
		}

		//*****************************************************************************
		//*                                                                           *
		//*****************************************************************************


		void txtPlot( unsigned char x, unsigned char y, unsigned char Color)
		{
				setcolor(Color);
				gotoxy(x,y);cprintf("�");
		}


	//**********************************************
	//**                                          **
	//**********************************************




		 void  txtSwap(int first, int second )
		{
		  int  temp ;


		    temp   = first;
		    first  = second;
		    second = temp;
		  }




     void txtCircle(int X, int Y, int rad, int col)
	 {
       float deg = 0;
       do {
         X = (int) (rad * cos(deg));
         Y = (int) (rad * sin(deg));
         txtPlot (40+X, 12+Y,  col);
         deg +=  0.005;
       }
       while (deg <= 6.4);
     }


	//**********************************************
	//**                                          **
	//**********************************************



    void txtLine(int x0, int y0, int x1, int y1, int color)
    {
        int pix = color;
        int dy = y1 - y0;
        int dx = x1 - x0;
        int stepx, stepy;

        if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
        if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
        dy <<= 1;                                                  // dy is now 2*dy
        dx <<= 1;                                                  // dx is now 2*dx

      txtPlot( x0, y0,pix);
        if (dx > dy) {
            int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
            while (x0 != x1) {
                if (fraction >= 0) {
                    y0 += stepy;
                    fraction -= dx;                                // same as fraction -= 2*dx
                }
                x0 += stepx;
                fraction += dy;                                    // same as fraction -= 2*dy
                txtPlot( x0, y0,pix);
            }
        } else {
            int fraction = dx - (dy >> 1);
            while (y0 != y1) {
                if (fraction >= 0) {
                    x0 += stepx;
                    fraction -= dy;
                }
                y0 += stepy;
                fraction += dx;
                txtPlot( x0, y0,pix);
            }
        }
    }


	//**********************************************
	//**                                          **
	//**********************************************




		void delay(unsigned int milliseconds)
		{
			clock_t ticks1, ticks2;
			unsigned int tic1=0,tic2=0,tick=0;

			ticks1=clock();
			while(tick<milliseconds)
			{
				ticks2=clock();
				tic1=ticks2/CLOCKS_PER_SEC-ticks1;
				tic2=ticks1/CLOCKS_PER_SEC;
				tick=ticks2-ticks1;
			}
			ticks2=clock();
		}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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