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.

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.12.00.png

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)

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.20.18.png

Revision:
11:1fd8fca23375
Parent:
10:19b250c7de5e
Child:
13:1472c1637bfc
--- a/Battle/Battle.cpp	Mon May 18 18:14:35 2020 +0000
+++ b/Battle/Battle.cpp	Wed May 27 05:07:34 2020 +0000
@@ -7,6 +7,12 @@
 #include "Gamepad.h"
 
 #include <vector>
+#include <algorithm>
+#include <memory>
+
+using namespace std;
+
+Ticker t;
  
 Battle::Battle()
 {
@@ -18,18 +24,17 @@
 
 }
  
-void Battle::init(int invaders, int speed, int interval)
+void Battle::init(int rows, int columns, int speed, int interval, bool boss, int bossNum)
 {
-    _invaders = invaders;
-    _speed = speed;
+    reset(); // reset initial variables
+    _speed = speed; // set value for accessible variables
     _interval = interval;
-    
-    _army.create_army();
-    //_army.start_pos(5,5);
-    //_canon.init(44);
-
-    //interval.attach(&Battle::player_fire, 8.0);
-
+    _boss = boss;
+    _rows = rows;
+    _columns = columns;
+     
+    _canon.init(HEIGHT - 3); // initialise player
+    _army.create_army(_rows,_columns,speed,_boss,bossNum); // initialise army
 }
 
 void Battle::read_input(Gamepad &pad)
@@ -38,153 +43,187 @@
     _mag = pad.get_mag();
 }
 
-void Battle::draw(N5110 &lcd)
+void Battle::draw(N5110 &lcd) // draw battle elements, and refresh display
 {
-    //lcd.drawLine(1,1,WIDTH/2,HEIGHT-1,2);
-    print_score(lcd); // score
-    
-    _canon.draw(lcd); // canon
-    _army.draw(lcd); // invaders
+    _canon.draw(lcd); // draw canon
+    _army.draw(lcd); // draw invaders    
     
-    vector<Bullet>::iterator it1;
-    int i;
-    vector<Bullet>::iterator it2;
-    int n;
-    
-    for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) {
-        invBomb[i].draw(lcd);
-        }
-    for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) {
-        playBul[i].draw(lcd);
-        }
-    print_score(lcd);
+    for(int i = 0; i < Bombs.size(); i++) { Bombs[i]->draw(lcd); } // draw bombs
+    for(int n = 0; n < Bullets.size(); n++) { Bullets[n]->draw(lcd); } // draw bullets
+    lcd.refresh();
 }
 
-void Battle::update(Gamepad &pad)
+void Battle::clock(Gamepad &pad) // call invader fire at an interval of 2
 {
-    // important to update paddles and ball before checking collisions so can
-    // correct for it before updating the display
+    t.attach(Callback<void()>(this, &Battle::invader_fire), _interval);
+}
+
+void Battle::update(Gamepad &pad) // update canon position and firing, 
+{                                 // and all projectile positions and collisions
+    
+    //_army.move_army();
+    canon_fire(pad);
     _canon.update(_d,_mag);
     
-    invader_fire();
-    canon_fire(pad);
-    
-    int i, n;
-    vector<Bullet>::iterator it1;
-    vector<Bullet>::iterator it2;
-    
-    for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) {
-        invBomb[i].update();
-        }
-    for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) {
-        playBul[i].update();
-        }
+    for(int i = 0; i < Bombs.size(); i++) { Bombs[i]->update(); } 
+    for(int n = 0; n < Bullets.size(); n++) { Bullets[n]->update(); }
 
-    bullet_collisions();
-    check_invader_hit(pad);
-    check_canon_hit(pad);
+    projectile_edge();
+    bullet_collision(pad);
+    bomb_collision(pad);
 }
 
-void Battle::invader_fire()
-{
-    _army.rand_invader();
+void Battle::invader_fire() // pseudo-randomly select the position of an alive invader
+{                           // and drop a bomb
+    std::vector<int> fire_pos_y;
+    std::vector<int> fire_pos_x;
     
-    int x1 = _army._fire_x;
-    int y1 = _army._fire_y;
+    for (int i = 0; i < _rows; i++) { // iterate through invaders
+            for (int n = 0; n < _columns; n++) {
+                int state = invaders[i][n]->get_death(); 
+                if (!state) { // add alive invader position to X and Y vectors
+                    Vector2D fire_pos = invaders[i][n]->get_pos();
+                    fire_pos_y.push_back(fire_pos.y);
+                    fire_pos_x.push_back(fire_pos.x);
+                    }
+                }
+            }
+    // select random X and Y positions from the vectors        
+    int rand1 = rand() % fire_pos_y.size();
+    int rand2 = rand() % fire_pos_x.size();
     
-    Bullet bomb; // instantiate new bullet object
-    bomb.init(false, x1, y1); // initialise
+    Vector2D fire_pos;
+    fire_pos.y = fire_pos_y[rand1];
+    fire_pos.x = fire_pos_x[rand2];
     
-    invBomb.push_back(bomb); // add to invader bomb vector
+    // instantiate a new Bullet pointer, and add it to the Bomb vector
+    Bombs.push_back(new Bullet()); // initialise the Bullet with owner, position, speed
+    Bombs.back()->init(false, fire_pos.x + 4, fire_pos.y + 2, 3);
 }
 
-void Battle::canon_fire(Gamepad &pad)
-{
+void Battle::canon_fire(Gamepad &pad) // fire when A is pressed, and add a new
+{                                     // projectile to the player bullet vector
     bool A = pad.A_pressed();
     if (A) {
-            // 
+            Vector2D canon_pos = _canon.get_pos();
+            int x1 = canon_pos.x + 2;
+            int y1 = canon_pos.y + 1;
             
-            Vector2D canon_pos = _canon.get_pos();
-            int x1 = canon_pos.x;
-            int y1 = canon_pos.y;
+            Bullets.push_back(new Bullet());
+            Bullets.back()->init(true, x1, y1, 3);
             
-            Bullet shot;
-            shot.init(true, x1, y1);
-            playBul.push_back(shot);
+            printf("Player fired");
         }
 }
 
-void Battle::check_invader_hit(Gamepad &pad)
+void Battle::bullet_collision(Gamepad &pad) // bullet collision algorithm
 {
-    vector<Bullet>::iterator it1; // Iterate through canon bullet vector
-    Vector2D playBulPos; // Use collision function from army to iterate through invaders
-    int i; // Match deletes bullet, removes invader 
-
-    for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++i) {
+    Vector2D BulPos;
+    
+    for(int z = 0; z < Bullets.size(); z++) { // iterate through bullets
+        BulPos = Bullets[z]->get_pos(); // fetch each bullets position
+        Rectangle bulletRect(BulPos.x, BulPos.y, 1, 2); // apply to rectangle
+            
+        for (int i = 0; i < _rows; i++) { // iterate through invaders
+            for (int n = 0; n < _columns; n++) {
+          
+                bool state = invaders[i][n]->get_death();
         
-        playBulPos = playBul[i].get_pos();
-        int playBulX = playBulPos.x;
-        int playBulY = playBulPos.y;
-        
-        _army.check_col(playBulX, playBulY);
-        _canon.kill(_army._sprite_pass);
-        playBul.erase(playBul.begin() + i - 1);
-
+                if (!state) { // only test for collision if invader is alive
+                    Vector2D invPos = invaders[i][n]->get_pos(); // fetch parameters
+                    int invWidth = invaders[i][n]->get_width();
+                    int invHeight = invaders[i][n]->get_height();    
+                    Rectangle invaderRect(invPos.x, invPos.y, invWidth, invHeight); // apply to rectangle 
+                            
+                    if(colTest(invaderRect, bulletRect)){ // compare invader vs. bullet rectangles
+                        
+                        invaders[i][n]->hit(); // if collided, decrease health
+                        //int sprite = invaders[i][n]->get_sprite(); // add to kill count
+                        //_canon.kill(sprite);
+                        pad.tone(750.0,0.1); // play a tone
+                        Bullets.erase(Bullets.begin() + z - 1); // and delete bullet
+                        }
+                }
+            }
+        }
     }
 }
 
-void Battle::check_canon_hit(Gamepad &pad)
-{
-    vector<Bullet>::iterator it2; // Iterate through invader bomb vector
-    Vector2D invBombPos; // If positions are equal, delete bullet and remove life
+void Battle::bomb_collision(Gamepad &pad) // check if bombs have collided with the canon
+{                                         // if so, remove life, play tone, 
+    Vector2D BombPos;                     // and delete from vector
     Vector2D canonPos;
     
-    int i;
-    
-    for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) {
+    for (int i = 0; i < Bombs.size(); i++) { // check 
+        BombPos = Bombs[i]->get_pos();
+        
+        canonPos = _canon.get_pos();
+        int canonWidth = _canon.get_width();
+        int canonHeight = _canon.get_height();
         
-        invBombPos = invBomb[i].get_pos();
-        canonPos = _canon.get_pos();
+        Rectangle canonRect(canonPos.x, canonPos.y, canonWidth, canonHeight);
+        Rectangle bombRect(BombPos.x, BombPos.y, 2, 2);
         
-        if (invBombPos.x == canonPos.x && invBombPos.y == canonPos.y) {
-            invBomb.erase(invBomb.begin() + 1 - 1);
+        if (colTest(canonRect,bombRect)) {
             _canon.remove_life();
+            pad.tone(650.0,0.1);
+            Bombs.erase(Bombs.begin() + i - 1);
             }
         } 
 }
 
-void Battle::bullet_collisions()
-{
-    vector<Bullet>::iterator it1;
-    vector<Bullet>::iterator it2;
+void Battle::projectile_edge() // if projectiles are beyond screen limits
+{                               // remove from the vector to free memory
+    Vector2D BombEdge;
+    Vector2D BullEdge;
     
-    Vector2D invBombCol;
-    Vector2D playBulCol;
-    int i;
-    
-    for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) {
-        invBombCol = invBomb[i].get_pos();
-        
-        if (invBombCol.y >= 48){
-            invBomb.erase(invBomb.begin() + i - 1);
+    for(int i = 0; i < Bombs.size(); i++) {
+        BombEdge = Bombs[i]->get_pos();
+        if (BombEdge.y >= HEIGHT){
+            Bombs.erase(Bombs.begin() + i - 1);
         }
     }
-    
-    int n;
-    
-    for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++n) {
-        playBulCol = playBul[n].get_pos();
-        
-        if (playBulCol.x >= 0){
-            playBul.erase(playBul.begin() + n - 1);
+
+    for(int n = 0; n < Bullets.size(); n++) {
+        BullEdge = Bullets[n]->get_pos();
+        if (BullEdge.y <= 0){
+            Bullets.erase(Bullets.begin() + n - 1);
         }
     }
 }
 
-void Battle::print_score(N5110 &lcd)
+bool Battle::end() // if all the invaders are dead, returns true
 {
-    int total_score = _canon.get_score();
-    char buffer[14];
-    sprintf(buffer,"%2d",total_score);
-    lcd.printString(buffer,42,1);
+    if (_army.end()) { // saves main from interacting with army.h
+        return _end = true;
+        }
+    else { return _end = false; }
+}
+
+void Battle::reset() // free memory from the vector pointers for projectiles
+{   
+    Bullets.clear();
+    Bombs.clear();
+    
+    _end = false; // reset _end and _dead conditions
+    _dead = false;
+}
+
+int Battle::life() // check next comment
+{
+    int lives = _canon.get_life();
+    return lives;
+}
+
+void Battle::reset_life() // pulls from canon.h method so that main only
+{                         // has to interact with battle.h
+    _canon.reset_life();
+}
+
+bool Battle::colTest(Rectangle r1, Rectangle r2) // check if 2 rectangles 
+{                                                // are overlapping
+    return ((r1.x + r1.width > r2.x) // code taken from Mozilla:
+        &&  (r1.x < r2.x + r2.width) // https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection
+        &&  (r1.y + r2.height > r2.y) // for 2D game collisions
+        &&  (r1.y < r2.y + r2.height));
 }
\ No newline at end of file