Question

In: Computer Science

C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...

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.

Solutions

Expert Solution

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

}


Related Solutions

C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a C program that creates a database of plant height samples. In this scenario a...
Write a C program that creates a database of plant height samples. In this scenario a researcher enters a name of a group/type of plants, and heights for each plant in the group/type Use input will be stored in a database file, which should be a file called database.csv and should be in the following formatPlant1, number_of_samples, sample1, sample2, sample3 ...Plant2, number_of_samples, sample1, sample2, sample3 ...Plant3, number_of_samples, sample1, sample2, sample3 ...Lines starts with the name of a plant type, then...
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT