Starting from:

$35

Lab 6 Class Hierarchies and Pure Virtual Functions Solution

Part 1: (70 points) In this lab I would like for you to use a polymorphic screen manager for a very basic video game called “ World in the Balance. ” The year is 5242 and the information from Voyager spacecraft that was launch in 1977 has been discovered by the savage and alien Concal Empire. Their goal is to conquer the human race with their version of the ultimate weapon—the Death Star. Hey, it worked for Darth Vader! The only way to save planet earth and the human race is to harness the geothermal energy at the center of our planet and turn the whole planet into a maneuverable space ship so that we can find another star to orbit that is hidden from the Concal Empire. For your video game, I would like for you to make the first level only which is “Get through the Kuiper Belt.” In short, the player will need to navigate "spaceship earth" through a dense asteroid field. You will use the accelerometer in mbed kit to control the movements of the earth between asteroids. Your artist design team has decided to use a retro asteroid look for this level that is illustrated on the next page.




I have tried to simplify this game as much as possible so you can practice some of the core ideas discussed in class related to class hierarchies and polymorphism. These are some of the ideas that you will explore in this lab:




 
Class Hierarchy: You will make a hierarchy to implement your game’s polymorphic screen manager. Your derived classes will call pure virtual functions like draw() and update(). The class hierarchy is specified in the following pages.




 
Polymorphic Data Structures: You will create an array of ScreenObject * that will be used to manage the asteroids in the Kuiper belt that spaceship earth must navigate. The new idea is that this is an array of BASE pointers that will point to various DERIVED class objects.




In this game, you will start with 4 different asteroids that will come in from all directions on the LCD screen. Your spaceship earth will move like the sphere in Lab 2 with the accelerometer so that you can dodge the asteroids. Please note that as an asteroid goes off the screen you must have another asteroid enter the screen from a random location around the edge. Please watch the following video as a guide to design your own game. Please feel free to spruce it up beyond the basic required components.




https://youtu.be/I94lCZsMuvY






Requirements Discussion




 
Asteroid Design: I would like for you to have at least 4 asteroids flying through the screen at one time. Please match the below drawings the best you can. I have provided the first one for you in code on the next page. Also, if your earth collides with an asteroid, please create an appropriate explosion.
























 
Asteroid Movements: Use the rand() function to pick the initial locations around the edge of the screen (~126 x 126 pixels) to introduce a new asteroid. Once an asteroid leaves the screen, create a new asteroid to come in from a different location. You should also randomly assign the slope of the trajectory of each asteroid. To do this, randomly assign a deltaX and deltaY that you maintain in the AsteroidAbstract class that is used to update the asteroid's position. Please see the video for an example of this.




 
Begin main() Function: Here is some code that you might need to start the game and get the LCD screen working correctly.




int main() {




uLCD.baudrate(300000);




wait(0.2);




srand(time(0)); // do this srand call here ONLY... no where else in the code! ScreenObject * ActiveAsteroids[NUM_ASTEROIDS]; SpaceShipEarth ship;




//more code after this!




 
Game Timer: To complete the game, the user must dodge asteroids for 30 seconds. Please use the time function in the ctime library to keep track of the elapsed time. If the player can successfully dodge the asteroids for the entire time, then he or she has cleared this level. Yea! Otherwise, the earth explodes and the game ends. If the player survives the entire time, then clear the screen and print a congratulation to the user.




I would like for you to have a TIMER BAR on one side of the screen that gets shorter as time progresses. In the video, which is hard to see, there is a blue bar at the top of the screen that shortens as time progresses.




time_t startTime; startTime = time(0);

time_t timeElapsed = time(0)-startTime;







 
Graphic Sprites: In this lab, I would like for you to use the idea of a graphic "sprite." I have defined the sprite for ONE of your asterioids and a spaceshipEarth as seen below. You will use the mbed member function for the uLCD display called BLIT to display the sprite to the screen. Here are some code snippets to help you. You may define your sprites in the global namespace as seen below in order to maximize performance.




#define ASTEROID_HEIGHT 12




#define ASTEROID_WIDTH 15

#define SPRITE_MAX 15




#define EARTH_WIDTH 10




#define EARTH_HEIGHT 10




#define EXPLOSION1_WIDTH 20




#define SCREEN_MAX 125




#define SCREEN_MIN 1




#define NUM_ASTEROIDS 4




#define Q 0x808000 //OLIVE




#define I 0x008000 //GREEN




#define S 0xC0C0C0 //SILVER




#define C 0x17202A //UFO GLASS




#define D 0x797D7F //DARK GREY




#define L 0x00FF00 //LIME




#define P 0xFF00FF //PINK




#define R 0xF1C40F //YELLOW

#define O 0xF39C12 //ORANGE

#define G 0xAAB7B8 //GREY




#define _ 0x000000 //BLACK




#define X 0xFFFFFF //WHITE




#define B 0x0000FF //BLUE




#define r 0xFF0000 //RED




int asteroid_sprite_1[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {




_,_,_,_,X,X,X,X,X,X,X,X,_,_,_,




_,_,_,X,_,_,_,_,_,_,_,_,X,_,_,




_,_,X,_,_,_,_,_,_,_,_,_,X,_,_,




_,X,_,_,_,_,_,_,_,_,_,_,X,_,_,




X,X,X,X,_,_,_,_,_,_,_,_,_,X,_,




_,_,_,X,_,_,_,_,_,_,_,_,_,X,_,




_,_,X,_,_,_,_,_,_,_,_,_,_,X,_,




_,X,_,_,_,_,_,X,_,_,_,_,_,X,_,




X,_,_,_,_,_,X,X,_,_,_,_,X,_,_,




_,X,_,_,_,X,_,X,_,_,_,_,X,_,_,




_,_,X,_,X,_,_,X,_,_,_,X,_,_,_,




_,_,_,X,_,_,_,X,X,X,X,_,_,_,_




};







int spaceship_earth1[EARTH_WIDTH *EARTH_HEIGHT] = {




_,_,S,S,S,S,S,S,_,_,




_,S,I,I,I,I,I,I,S,_,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,I,S,




S,I,I,I,I,I,I,I,S,_,




_,S,S,S,S,S,S,S,_,_,




};




To send this sprite image quickly to the LCD display, you can use the following command. The x_UPPERLEFT and y_UPPERLEFT specify the coordinates of the upper left corner of the sprite.




uLCD.BLIT(x_UPPERLEFT, y_UPPERLEFT, ASTEROID_WIDTH, ASTEROID_HEIGHT, asteroid_sprite_1);







 
Class Hierarchy: I would like you to implement the following class hierarchy as a part of this lab. Make sure you use pure virtual functions where appropriate.















pure virtual functions


ScreenObject




draw()










update()


int y_pos;
































AbstractAsteroid






SpaceShipEarth
int deltaX;






draw()
int deltaY;






update()





















ConcreteAsteroid1


ConcreteAsteroid2


ConcreteAsteroid3


ConcreteAsteroid4
draw()


draw()


draw()


draw()
update()


update()


update()


update()

















 
Collision Detection: You will need to determine when the asteroid and the spaceship overlap. To determine overlap please assume that the rectangular sprite determines overlap boundaries. To detect a collision, you should be able to check if each of the corners of one object are in the interior of a second object to detect a collision. In addition, I would like for you to create a polymorphic global function that determines if two objects overlap. The function signature might look something like the following.




bool overlap(ScreenObject & objectA, ScreenObject & objectB)




{

//you put stuff in here!

}




4The key idea is that you will pass the objects as references to a ScreenObject as seen above.




This function will return true if the two objects overlap and false otherwise.




Part 2: (20 points)




I would like for you to use interrupts in this lab to monitor a push button for a single "bomb" that can be used ONCE to destroy all the asteroids on the screen. After they are destroyed, more asteroids must come in from the edges so that the game continues. Please review the code that was used in the 3-button keyboard in an earlier lab. Please remember to not have too many operations inside your interrupt callback function or this will cause unexpected behavior in your program. Your call back function might look something like.




void pb1_hit_callback (void)




{




if (!BombUsed )




{




ExplodeAllAsteroids = true;

BombUsed = true;

}




}







Part 3: (10 points)




Use the speaker and add some sound effects for your game. At the very least make a good explosion noise when the earth collides with an asteroid!




Turn in instructions




 
Demo your lab to the TA during their lab hours. Print a copy of the checkoff sheet in Appendix A for the TA to sign. Make sure that you show them your code during the check-off so that they can see your class hierarchy. Also show them your polymorphic data structure that you used to store different asteroid objects.`




 
Post your source code AND the signed check off sheet on the canvas as apart of the assignment. The TAs or the professor will need to look at your code for reference. You can put all of you code in one file if you choose to do so.

















































5
APPENDIX A




Grade Sheet Checkoff




Name of Student: _________________________________________ Name of TA: ____________________________________




Completed Part 1 Requirements:




Show the TA your class hierarchy (30%) _______________________




Show the TA correct implementation (40%) ____________________




Completed Part 2 Requirements (20%): _______________________________




Completed Part 3 Requirements (10%): _______________________________




Additional Comments: ______________________________________________________




Additional points can be deducted at the discretion of the TA according to the following criteria.

















Element


Percentage Details








Deduction

















Used a class hierarchy improperly.




Did not use Self- Documenting Coding Styles




30% This is a requirement for the program.







5%-15% This can include incorrect indentation, using unclear variable names, unclear comments, or compiling with warnings. (See next page)






TURN IN EARLY POLICY


























Element


Percentage
Details








Extra Credit






















Turn In Nov 28 or
4%




You must have complete check off with TA




before


























Turn In Nov 29
3%




You must have complete check off with TA


















Turn In Nov 30
2%




You must have complete check off with TA


















Turn In Dec 3
1%




You must have complete check off with TA
















LATE POLICY




























Element


Percentage


Details








Deduction






















Each Additional Day
20% per day


The weekend counts as one day. (i.e. turning in on Monday














results in a 20% point reduction)
































6

More products