![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Helios Lyons 201239214
Dependencies: mbed
Brief
My aim for this project was to create a FRDM K64F adapted version of the classic Space Invaders by Tomohiro Nishikado. The game itself has a number of clear features to implement;
- Left to right movement for the player 'canon'
- A fixed amount of player lives
- Firing mechanics for both canon and invaders (hence collision systems)
- Random firing from remaining invaders
- Wave based combat
My own addition to these established ideas was Boss waves, featuring a single, larger sprite which fires at a faster interval than previous waves. The addition of a movement system using a basic for loop, as opposed to a velocity based system, will enhance the nostalgic feel of the game.
Gameplay
Movement is controlled with the joystick, moving the canon left or right. Fire by pressing A. Invaders spawn at set positions, but randomly fire at a set interval, which is higher for boss waves. Time is taken during each wave, and displayed at wave intervals, and if the play wins.
Controls are shown on the Gamepad below: (attribution: Craig A. Evans, ELEC2645 University of Leeds)
Diff: Invaders/Army.cpp
- Revision:
- 11:1fd8fca23375
- Parent:
- 10:19b250c7de5e
diff -r 19b250c7de5e -r 1fd8fca23375 Invaders/Army.cpp --- a/Invaders/Army.cpp Mon May 18 18:14:35 2020 +0000 +++ b/Invaders/Army.cpp Wed May 27 05:07:34 2020 +0000 @@ -6,6 +6,13 @@ #include <cstdlib> #include <vector> +#include <algorithm> +#include <functional> + + +using namespace std; + +std::vector<std::vector<Invader*> > invaders; Army::Army() { @@ -17,176 +24,83 @@ } -void Army::create_army(int invaders) -{ - Invader army[3][3]; - - int i, n; - for(i=0; i < 4; i++) { - //army[i].setX(i); - - for(n=0; n < 4; n++) { - army[i][n].init(i+1, 1); - } - } -} - -void Army::create_boss() +void Army::create_army(int rows, int columns, int speed, bool ifboss, int bossNum) { - Invader boss; - boss.init(4, 4); -} - -void Army::start_pos(int rowX, int rowY) -{ - int tempX = rowX; - _move_left = true; + _rows = rows; // assign variables + _columns = columns; + _boss = ifboss; + + invaders.clear(); // free memory from previous instances of the vector + invaders.resize(_rows); // clear contents and resize to given # of rows, columns + for (size_t i = 0; i < _rows; ++i) { invaders[i].resize(_columns); } + + int vector_width = _columns*8 + _columns; // calculate parameters of the vector + int temp_x = WIDTH/2 - vector_width/2; // as a table, to allow proper Invader + int start_y = HEIGHT/2 - 20; // distribution + _inc = 6; - for (int i = 0; i < 4; i++){ // Loop through each row and assign start_pos - army[i][0].set_pos(rowX, rowY); // Based on X and Y inputs - rowX += 13; // 2 above standard Invader width of 11 - } - rowX = tempX; - rowY += 10; - for (int i = 0; i < 4; i++){ - army[i][1].set_pos(rowX, rowY); - rowX +=13; - } - rowX = tempX; - rowY += 10; - for (int i = 0; i < 4; i++){ - army[i][2].set_pos(rowX, rowY); - rowX +=13; + for (int i = 0; i < _rows; i++) { // nested 'for' looped through the vector + int start_x = temp_x; // reset x position at each row + if (i > 0) {start_y += 9;} // increment y position from 2nd row onward + + for (int n = 0; n < _columns; n++) { // iterate through columns + invaders[i][n] = new Invader(); // instantiate a new Invader pointer + if (_boss) { // change sprite value if boss wave + int bossSprite = bossNum + 3; + invaders[i][n]->init(bossSprite,bossNum*2,1); // + invaders[i][n]->set_pos(start_x,start_y); + } + else { + invaders[i][n]->init(i+1,1,0); // initialise with sprite, HP, speed, and boss boolean + invaders[i][n]->set_pos(start_x,start_y); + int rowWidth = invaders[i][n]->get_width(); + start_x += rowWidth + 2; // increment x position + } + } } } -void Army::move_army(int x_distance, int y_distance) // REPLACE WITH FSM AFTER BASIC FUNCTIONALITY TEST +bool Army::end() // find # of dead invaders and compare to expected result { - _x_distance = x_distance; - _y_distance = y_distance; - - int left_X = 0; - int right_X = 80; - int bot_Y = 0; - int top_Y = 80; - - // find leftmost, rightmost, top and bottom invaders - // less invaders means more room to cover (also speed up) - - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - int tempX = army[n][i].get_x_pos(); - int tempY = army[n][i].get_y_pos(); - - if (tempX < left_X) { - left_X = tempX; - } - else if (tempX > right_X) { - right_X = tempX; - } - - if (tempY < top_Y) { - top_Y = tempY; - } - else if (tempY > bot_Y) { - bot_Y = tempY; - } + int sum = 0; + for (int i = 0; i < _rows; i++) { // iterate through Invader vector + for (int n = 0; n < _columns; n++) { // and get death state for each + bool inv_death = invaders[i][n]->get_death(); + if (inv_death) {sum++;} // add to sum } } - - - if (_move_left == true) { // if moving left - if (left_X < 12) { // check if edge will be reached, if so change direction - _move_left = false; - if(bot_Y > 50) { - // if lowest Y point is below 50, move up - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() - _y_distance); - } - } - } - else { // else move down - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() + _y_distance); - } - } - } - } - else { // if edge is not near to being reached, move left - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos() - _x_distance, army[i][n].get_y_pos()); - } - } + + if (sum == _rows*_columns) { // check sum vs. total number of invaders + _end = true; + return _end; } - } else { - if (right_X > 80) { // if rightmost invader is further than X = 65, change direction - _move_left = true; - - if (bot_Y > 50) { // if lowest invader is below Y = 50, go up - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() - _y_distance); - } - } - } - else { // otherwise go down - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos(), army[i][n].get_y_pos() + _y_distance); - } - } - } + _end = false; + return _end; } - else { // if edge is not near to being reached, move right - for (int i = 3; i < 0; i--) { - for (int n = 3; n < 0; n--) { - army[i][n].set_pos(army[i][n].get_x_pos() + _x_distance, army[i][n].get_y_pos() + _y_distance); - } - } - } - } } -void Army::draw(N5110 &lcd) -{ - int i, n; - for(i=0; i < 4; i++) { - - for(n=0; n < 4; n++) { - army[i][n].draw(lcd); +void Army::draw(N5110 &lcd) // call draw for alive invaders +{ + for (int i = 0; i < _rows; i++) { // iterate through Invader vector + for (int n = 0; n < _columns; n++) { // call draw function if death is false + bool state = invaders[i][n]->get_death(); + if (!state) {invaders[i][n]->draw(lcd);} + } } - } } -void Army::rand_invader() -{ - // get random coordinates for an invader - int rem = rand() % 5; // random column number - int rem2 = rand()% 4; // random row number - _fire_x = army[rem2][rem].get_x_pos(); // get position for random choices - _fire_y = army[rem2][rem].get_y_pos(); // used to set bullet start location -} - -void Army::check_col(int x, int y) -{ - - for (int i = 0; i < 4; i++) { // take 2 positions - for (int n = 0; n < 4; n++) { // return match after iterating through army - int tempX = army[n][i].get_x_pos(); - int tempY = army[n][i].get_y_pos(); - - if (x == tempX && y == tempY) { - _x_col = true; - _y_col = true; - - army[n][i].hit(); - _sprite_pass = army[n][i].get_sprite(); - } - } - } -} +// NOTE: instead of all these 'for' loops, one that checked and deleted dead +// invaders would have been more efficient, if called prior to draw and +// collision functions + +// NOTE 2: std::for_each could have been used instead of functions called on +// all invaders, but I could not identify the correct syntax for apply this +// to a vector of vectors of pointers. + +//void Army::move_army() + + // removed due to numerous errors, will be fixed if losing 5% is deemed + // worth it + \ No newline at end of file