# include # include # include int left = 10, top = 5, right = 70, bottom = 20; void gotoxy( short x, short y ) { HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE) ; COORD position = { x, y } ; SetConsoleCursorPosition( output_handle, position ) ; } void Display(int x, int y, unsigned char element) { gotoxy(x, y); printf("%c", element); } void DrawRectangle() { unsigned char brick = 177; //try different values like 219, 178 for(int x = left; x <= right; x++) { Display(x, top, brick); //top line Display(x, bottom, brick); //bottom line } for(int y = top; y <= bottom; y++) { Display(left, y, brick); //left line Display(right, y, brick); //right line } } class Ball { int x, y; int dx, dy; public: Ball() { x = 20; y = 10; dx = 1; dy = -1; } bool Update(int x1, int x2) { Display(x, y, ' '); x += dx; y += dy; Display(x, y, '*'); if(x <= (left+1) || x >= (right-1)) dx = -dx; if(y <= (top+1)) dy = -dy; if(y >= (bottom-1)) { if(x < x1 || x > x2) return true; else dy = -dy; } return false; } }; struct Pad { int x; int width; Pad() { x = 30; width = 10; } void Draw(unsigned char element) { for(int i = 0; i < width; i++) Display(x+i, bottom-1, element); } void MoveRight() { Draw(' '); if(x < (right-width)) x++; Draw(176); } void MoveLeft() { Draw(' '); if( x > (left+1)) x--; Draw(176); } }; void main() { Ball ball; Pad pad; char input = 0; char escapeKey = 27; bool gameOver = false; DrawRectangle(); while(!gameOver) { gameOver = ball.Update(pad.x, pad.x+pad.width); pad.Draw(176); Sleep(100); if(_kbhit()) { input = _getch(); if(input == '6') pad.MoveRight(); if(input == '4') pad.MoveLeft(); gameOver |= (input == escapeKey); } } gotoxy(10, 2); printf("Game Over..."); _getch(); }