# 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; void Update() { x += dx; y += dy; if(x <= (left+1) || x >= (right-1)) dx = -dx; if(y <= (top+1) || y >= (bottom-1)) dy = -dy; } public: Ball() { x = 20; y = 10; dx = 1; dy = -1; } void Draw() { Display(x, y, ' '); Update(); Display(x, y, '*'); } }; void main() { Ball ball; DrawRectangle(); while(!_kbhit()) { ball.Draw(); Sleep(1000); } }