In: Computer Science
C++
Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle for the tenth time.
/* This program uses to that two rectangular shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and move toward the edge of the screen.
*/
//Header file section
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
using namespace std;
/*A global constant can be included in more than
one cpp file. This is the handle to the output console.*/
const HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
/* A shape has a direction and is able to move in that direction.
The move is a virtual member function.*/
class Shape
{
public:
virtual void setDirection(int drow, int dcol)
{
dRow = drow;
dCol = dcol;
}
void getDirection(int &drow, int &dcol)
{
drow = dRow;
dcol = dCol;
}
virtual void move() = 0;
private:
int dRow, dCol; // Direction of motion
};
/* A SimpleShape is drawn at a given position in a
specified color.*/
class SimpleShape : public Shape
{
public:
virtual void draw() = 0;
void getPosition(int &row, int &col)
{
row = rowPos;
col = colPos;
}
void setPosition(int row, int col)
{
rowPos = row;
colPos = col;
}
void setColor(int c)
{
color = c;
}
int getColor()
{
return color;
}
virtual void move();
private:
int color;
int rowPos, colPos;
};
//A Box is a rectangular type of shape
class Box : public SimpleShape
{
public:
virtual void draw();
Box(int rowPos, int colPos, int width, int height);
private:
int width, height;
};
/* Moves a simple shape one step by erasing the shape at its
current position, changing its position, and then redrawing the
shape at its new position */
void SimpleShape::move()
{
int dRow, dCol; //Direction of motion
int savedColor = color;
color = 0; //Drawing in color 0 erases the shape
draw();
/* Compute the new position for the shape by adding a
step in the proper direction to the current
position. */
getDirection(dRow, dCol);
rowPos += dRow;
colPos += dCol;
/* Draw shape at its new position in its specified
color*/
color = savedColor;
draw();
}
/* Constructor sets the color, position, and
dimensions for a box shape, and draws the
box at its initial position */
Box::Box(int rowPos, int colPos, int width, int height)
{
setColor(14);
setPosition(rowPos, colPos);
this->width = width;
this->height = height;
draw();
}
//Draws a box shape
void Box::draw()
{
int rowPos, colPos;
COORD pos;
//Sets the color attribute for the box
SetConsoleTextAttribute(outHandle, getColor());
getPosition(rowPos, colPos);
pos.X = colPos;
pos.Y = rowPos;
//Draws the lines that make up the box.
for(int r = 0; r < height; r++)
{
SetConsoleCursorPosition(outHandle, pos);
for( int c = 0; c < width; c++)
cout<< "*";
cout<<endl;
pos.Y++;
}
//Restore normal text attribute
SetConsoleTextAttribute(outHandle, 7);
}
/* This program creates two rectangle shapes and to do
graphics animation */
int main()
{
//Creates two boxes
Box box1(5, 0, 10, 5);
Box box2(5, 70, 10, 5);
int met = 0; //number of meets at the middle
int d1 = -1, d2 = 1; //direction of motion for boxes
//Draws two boxes
box1.draw();
box2.draw();
do
{
d1 = -d1; d2 = -d2;
box1.setDirection(0, d1);
box2.setDirection(0, d2);
for( int k = 0; k < 30; k++)
{
Sleep(100);
box1.move();
box2.move();
}
met++;
COORD pos;
pos.X = 5;
pos.Y = 15;
SetConsoleCursorPosition(outHandle, pos);
cout<<"Number of meetings at the middle: "
<<(met+1)/2<<endl;
}while(met < 19);
return 0;
}