Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

Hello, soldier, you have been specially selected as the defender of planet earth.

Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.

But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.

As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.

Files at this revision

API Documentation at this revision

Comitter:
evanso
Date:
Tue May 12 23:08:08 2020 +0000
Parent:
24:479da6ca0e7e
Child:
26:1a7056eb3253
Commit message:
Added explosion class. When the alien is now shot it explodes.

Changed in this revision

Alien/Alien.h Show annotated file Show diff for this revision Revisions of this file
Explosion/Explosion.cpp Show annotated file Show diff for this revision Revisions of this file
Explosion/Explosion.h Show annotated file Show diff for this revision Revisions of this file
GameEngine/GameEngine.cpp Show annotated file Show diff for this revision Revisions of this file
GameEngine/GameEngine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show diff for this revision Revisions of this file
--- a/Alien/Alien.h	Tue May 12 15:08:56 2020 +0000
+++ b/Alien/Alien.h	Tue May 12 23:08:08 2020 +0000
@@ -92,6 +92,8 @@
         
         // Aliens genreal movement direction, 1 = East , 0 = West 
         int alien_general_direction;
+        
+        
 };
  
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Explosion/Explosion.cpp	Tue May 12 23:08:08 2020 +0000
@@ -0,0 +1,49 @@
+#include "Explosion.h"
+
+Animation animation_fsm[2] = {
+    {false, true, FILL_WHITE, FILL_BLACK},
+    {true, false, FILL_TRANSPARENT, FILL_WHITE},
+    
+};
+
+Explosion::Explosion() {
+     
+}
+ 
+Explosion::~Explosion() { 
+ 
+}
+
+void Explosion::init() {
+    position_x_explosion_ = 0;
+    position_y_explosion_ = 0;
+    fsm_counter_ = 0;  
+    explosion_radius_ = 4;
+    draw_counter = 0;
+}
+
+void Explosion::draw_explosion(N5110 &lcd) {
+    // Draws each explotion frames depending on state 
+    if(animation_fsm[fsm_counter_].draw_circle_one){
+        lcd.drawCircle(position_x_explosion_, position_y_explosion_,explosion_radius_ + 1, animation_fsm[fsm_counter_].circle_one);
+    }
+    if(animation_fsm[fsm_counter_].draw_circle_two){
+        lcd.drawCircle(position_x_explosion_, position_y_explosion_,(explosion_radius_ - 2), animation_fsm[fsm_counter_].circle_two);
+    }
+    
+    // Slows down annimation change time, so eplosion animation is longer
+    if (draw_counter%2 == 0) {
+        explosion_radius_++;
+        fsm_counter_ = !fsm_counter_;
+    }
+    draw_counter++;
+}
+
+void Explosion::set_explosion_posistion(Vector2D destroyed_position) {
+    position_x_explosion_ = destroyed_position.x + 3; 
+    position_y_explosion_ = destroyed_position.y + 3; 
+}
+
+int Explosion::get_explosion_radius(){
+    return explosion_radius_;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Explosion/Explosion.h	Tue May 12 23:08:08 2020 +0000
@@ -0,0 +1,73 @@
+#ifndef EXPLOSION_H
+#define EXPLOSION_H
+
+// Included libraries -----------------------------------------------------------
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include <vector>
+
+/** Explosion class
+@brief Draws explosion animation 
+@author Benjamin Evans, University of Leeds
+@date May 2020
+*/      
+
+struct Animation{
+    bool draw_circle_one; 
+    bool draw_circle_two;
+    FillType circle_one; 
+    FillType circle_two;
+};
+    
+
+class Explosion {
+    public:
+        /** Constructor */
+        Explosion();
+        
+        /** Destructor */
+        ~Explosion();
+        
+        /** Initalises explosion */
+        void init();
+    
+        /** Draws the explosion 
+         * @param lcd @details : N5110 object
+         */
+        void draw_explosion(N5110 &lcd);
+        
+    
+    // Accessors and mutators --------------------------------------------------
+        
+        /** Set position of the explosion
+         * @param destroyed_position @details : xy position of object that was detroyed
+         */
+        void set_explosion_posistion(Vector2D destroyed_position);
+        
+        /** Gets the explosion radius
+         * @returns explosion_radius_
+         */
+        int get_explosion_radius();
+        
+    private:   
+       
+    // Varibles ---------------------------------------------------------------- 
+         
+        //Explosion x position on lcd
+        int position_x_explosion_;
+            
+        // Explosion y position on lcd
+        int position_y_explosion_;
+        
+        // Explosion circle radius
+        int explosion_radius_;
+        
+        // FSM counter
+        int fsm_counter_;
+        
+        // Draw cunter 
+        int draw_counter;
+};
+ 
+#endif
\ No newline at end of file
--- a/GameEngine/GameEngine.cpp	Tue May 12 15:08:56 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Tue May 12 23:08:08 2020 +0000
@@ -31,6 +31,7 @@
     map.draw_map(lcd, d_);
     draw_aliens();
     draw_bullets();
+    draw_explosions(); 
 
     // refresh's screen
     lcd.refresh(); 
@@ -62,6 +63,16 @@
     alien_vector.push_back(new_alien);
 }
 
+void GameEngine::create_explosion(){
+    // explosion object
+    Explosion new_explosion;
+    
+    new_explosion.init();
+    
+    // Stores explosion object in vector 
+    explosion_vector.push_back(new_explosion);
+}
+
 void GameEngine::draw_bullets(){
     // interates over bullet vector and get each new_bullet object to draw its self and move
     for (int i = 0; i < bullet_vector.size(); i++){
@@ -82,8 +93,23 @@
         
         // Deletes bullet and alien if collision detected
         if (alien_vector[i].check_collision(bullet_vector[i])){
-            bullet_vector.erase(bullet_vector.begin()+ i);  
-            alien_vector.erase(alien_vector.begin()+ i);          
+            create_explosion();
+            explosion_vector[i].set_explosion_posistion(alien_vector[i].get_pos()); 
+            
+            bullet_vector.erase(bullet_vector.begin()+ i);    
+            alien_vector.erase(alien_vector.begin()+ i);  
+        }
+    }
+}
+
+void GameEngine::draw_explosions(){
+    // interates over expoltion vector and draws each explosion object then deleted object after set size
+    for (int i = 0; i < explosion_vector.size(); i++){
+        explosion_vector[i].draw_explosion(lcd);
+        
+        // delete explosion after reaches set size
+        if(explosion_vector[i].get_explosion_radius() == 8){
+             explosion_vector.erase(explosion_vector.begin()+ i);    
         }
     }
 }
\ No newline at end of file
--- a/GameEngine/GameEngine.h	Tue May 12 15:08:56 2020 +0000
+++ b/GameEngine/GameEngine.h	Tue May 12 23:08:08 2020 +0000
@@ -9,6 +9,7 @@
 #include "Map.h"
 #include "Weapons.h"
 #include "Alien.h"
+#include "Explosion.h"
 #include <vector>
 
  
@@ -52,17 +53,28 @@
         /** Draws each alien object and deletes both objects if collision detected*/
         void draw_aliens();
         
+        /** Creats bullet object if button A is pressed and stores in vector*/
+        void create_explosion();
+       
+        /** Draws each explosion object if collision detected*/
+        void draw_explosions();
+        
     // Variables -----------------------------------------------------------
         
         // Direction of joystick
         Direction d_; 
         
+    // Vectors ----------------------------------------------------------------  
+      
         // Vector to store each new bullet object
         std::vector<Weapons> bullet_vector;
         
         // Vector to store each new alien object
         std::vector<Alien> alien_vector;
         
+        // Vector to store each new eplosion object
+        std::vector<Explosion> explosion_vector;
+        
     // Objects -------------------------------------------------------------
     
         // Gamepad object 
@@ -76,7 +88,7 @@
         
         // Map object 
         Map map; 
-    
+ 
 };
  
 #endif
\ No newline at end of file
--- a/main.cpp	Tue May 12 15:08:56 2020 +0000
+++ b/main.cpp	Tue May 12 23:08:08 2020 +0000
@@ -27,7 +27,9 @@
 // seperate details comments 
 // do params on seperate line and have seperate detials for each one
 // make code multi line seperate at and statments 
-// deffine headers porperly by commenting out and compiling  
+// deffine headers porperly by commenting out and compiling 
+// change commenting type to /*....*/ for all variables
+// make position class and inheret 
 
 // Objects ---------------------------------------------------------------------