creating a small game to help reinforce the topics we’re covering rules of the game will be to dodge projectiles and accrue point.C++ Programming

Objective

You’ll be creating a small game to help reinforce the topics we’re covering this week.  The rules of the game will be to dodge projectiles and accrue points as you do that.  You’ll be given a loose description of the features and have to decide how best to implement the solution.  For this lab, you’ll be doing some back-end setup.

Concepts we will be using / reinforcing in this lab:

inheritance

virtual functions

vector class

dynamic_cast

new and delete

Things to Avoid

Make sure you do not just write all of your logic in main.  Main should basically just have an instance of the Game class inside it and nothing more.  If your logic mostly exists in main, that will qualify for an immediate zero.  So please.  Don’t do that.

 

Remember to avoid globals.

 

Console.h/cpp

Before we start, let’s talk about the Console.h and Console.cpp.  First, copy over these 2 files from the Lecture 4 code folder to your project folder. Then you can include these files to get some nice extra functionality to your program, mainly an easier way of setting your position, and the ability to add color.  Once you include this file, make sure you are either scoping it in using the System namespace, or you add the using namespace System code where appropriate.  As these functions are all static, you’ll need to call them by scoping them in from the Console class.  So for example, Console::ForegroundColor();

 

ForegroundColor() – Use this to change your text color.  You’ll pass in an enum (included in the file) to change the colors.

 

BackgroundColor() – Use this to change the color of the test background.  As above, you’ll pass in a color enum.

 

ResetColor() – This function will reset the colors on the screen.  If you make changes to foreground and/or background, make sure you call this after you’re done displaying, or EVERYTHING will be these colors.

 

WindowWidth()/WindowHeight() – You can use these functions in order to get the max range of your window.

 

Clear() – Clears the screen of all text.

 

SetCursorPosition() – Probably the most helpful function, you’ll pass in an X and a Y position, and you’ll immediately begin displaying from there (as in, your cout << “Whatever” will print from that position).  It’s important to note that when you do a newline, you’ll move down to the next Y position, but the X will reset to the left edge of the screen.

 

Program Structure - Base

First, we’re going to set up our base class.  This is going to be the class that all of our functionality derives from.  It’s the class that we’ll want to put all of our common components in, meaning anything that our other classes (player, block, or additional items you want to add) will share.  X and Y position are great candidates for that, as anything you make is going to need a position on the screen. 

I’ll refer to this class as Base from here on out, feel free to call it whatever you choose.  Give it an X and Y int, and make sure you add a char for a picture/symbol, something that will display to the screen.  If you’re feeling good with the Console.h/cpp, feel free to add a ConsoleColor data member for the foreground and background, too.

We’re also going to need some extra methods aside from any accessors and mutators you have.  An Update() will be used to control any user input, as well as any sort of AI algorithms you decide to add later.  A Render() method will be used to display anything to the screen.  I recommend putting the invoking object const on Render, as that function should ONLY be used to display, not make any sort of changes to the classes.

The Render() class should display your object at the X and Y location of the data members, and also update any sort of foreground and background colors.  If you would like to add some input functionality to your Update(), look in to the GetAsyncKeyState method.

Program Structure – Game

Now, add another class called Game, or Play, or something along those lines.  This class will be used to manage the objects, and allow things to run in real time.  Add a bool that will allow the user to quit out (I usually go with “play”), and the Update() and Render() functions.  After these have been set up, you’ll need to make a play loop.  It should look something like this:

 

for(; play; )

{

   Update();

   Render();

}

 

This is the loop that is going to allow you to do real time updates.  As long as play is true, you’ll forever Update() and Render() your objects.   You do NOT need to do this with a for loop, a while works just as well.  Even a do-while, if you’re feeling like it.

 

Testing Things – Game

In the game class, make an array of Base class pointers.  I’ll let you decide if it should be a dynamic or static array.  In the Game classes constructor, allocate at least two of the objects, and then loop through them in the Game class Update and Render functions, calling the objects’ Update and Render functions.  With any luck, you should see your objects showing up on the screen, albeit with a bit of flicker.  If you’ve put any sort of input code using GetAsyncKeyState, you’ll be treated to a bad-looking game of snake.

 

You can make the flicker go away by adding the following code to your Game class Render function:

 

Console::Lock(true);

Console::Clear();

//Display objects here

 

Console::Lock(false);

Sleep(10);

 

What this does is prevent the console from drawing to the screen until your entire image is ready.  The system(“cls”) will also eliminate any trails you have from your objects.  So no more snake ripoff

 

Extra

If you decide you want to get a little fancy with your symbol, you may want to add a width and height to your base class.  In fact, for anything that’s going to be bigger than a single character, you’ll definitely want to add these in.  I recommend putting in something to calculate the width and height in your SetPicture() modifier. 

In addition, if you decide to have bigger pictures, you’ll need to change how you’re displaying things.  If your picture looks something like this, for example:

 

XXX

XXX

 

You’ll need to account for that newline.   Remember, that in the array, the characters actually look like this:  XXXnXXX

Width and Height will also need to be calculated a bit differently.  If you can get this up and running, though, it gives you much greater flexibility with your images for the objects.

Attachments:

Instructions Files

C++ Programming Experts

expert
Eric Bell
C++ Programming

88 Answers

expert
Dinh Hong P.
C++ Programming

20 Answers

View More Experts
Disclaimer

The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.

Get Free Quote!

259 Experts Online