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:
Fri May 15 14:19:54 2020 +0000
Parent:
34:85ccc16f24d2
Child:
36:27aa597db3d2
Commit message:
Moved check_alien_collision and check_collision into sperate parent classes as code was being written twice.

Changed in this revision

Alien/Alien.cpp Show annotated file Show diff for this revision Revisions of this file
Alien/Alien.h Show annotated file Show diff for this revision Revisions of this file
CheckAlienCollision/CheckAlienCollision.cpp Show annotated file Show diff for this revision Revisions of this file
CheckAlienCollision/CheckAlienCollision.h Show annotated file Show diff for this revision Revisions of this file
CheckCollisions/CheckCollision.cpp Show annotated file Show diff for this revision Revisions of this file
CheckCollisions/CheckCollision.h Show annotated file Show diff for this revision Revisions of this file
People/People.cpp Show annotated file Show diff for this revision Revisions of this file
People/People.h Show annotated file Show diff for this revision Revisions of this file
RandomMovement/RandomMovement.h Show annotated file Show diff for this revision Revisions of this file
Spaceship/Spaceship.cpp Show annotated file Show diff for this revision Revisions of this file
Spaceship/Spaceship.h Show annotated file Show diff for this revision Revisions of this file
--- a/Alien/Alien.cpp	Fri May 15 12:05:33 2020 +0000
+++ b/Alien/Alien.cpp	Fri May 15 14:19:54 2020 +0000
@@ -18,6 +18,8 @@
 }
 
 void Alien::init(Gamepad &pad, int position_x_start, int position_y_start) {
+    sprite_x_length = 7;
+    sprite_y_length = 6;
     position_x_ = position_x_start;
     position_y_ = position_y_start;
     alien_move_counter_ = 0;
@@ -114,41 +116,7 @@
     }
 }
 
-bool Alien::check_collision(Weapons bullet) {
-    bool collision = false; 
-    //printf ("Collision 1 = %d\n", collision);
-    Vector2D bullet_pos = bullet.get_pos();
-    // checks collision if bullet is going in east direction 
-    if(bullet.get_direction()){
-        //int bullet_pos_twox = bullet_pos.x;
-        //int bullet_pos_twoy = bullet_pos.y;
-        //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox);
-        //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy);
-        
-        if((bullet_pos.x + 1) >= position_x_ && 
-        (bullet_pos.x + 1) <= (position_x_ + 6) && 
-        position_y_ <= bullet_pos.y && 
-        bullet_pos.y <= (position_y_ + 7)){
-            collision = true;
-        }
-    //printf ("Collision 2 = %d\n", collision);  
-      
-    // checks collision if bullet is going in west direction 
-    }else if(!bullet.get_direction()){
-        
-        if(bullet_pos.x <= (position_x_ + 6) && 
-        bullet_pos.x >= position_x_ && 
-        position_y_ <= bullet_pos.y && 
-        bullet_pos.y <= (position_y_ + 7)){
-            collision = true;
-        }
-        
-     //printf ("Collision 3 = %d\n", collision);   
-    }
-    
-    //printf ("Collision 4 = %d\n", collision);   
-    return collision;
-}
+
 
 
 int Alien::get_alien_fire_counter(){
--- a/Alien/Alien.h	Fri May 15 12:05:33 2020 +0000
+++ b/Alien/Alien.h	Fri May 15 14:19:54 2020 +0000
@@ -7,13 +7,14 @@
 #include "Gamepad.h"
 #include "Weapons.h"
 #include "RandomMovement.h"
+#include "CheckCollision.h"
 
 /** Alien class
  * @brief Draws and moves aliens 
  * @author Benjamin Evans, University of Leeds
  * @date May 2020
  */ 
-class Alien: public RandomMovement {
+class Alien: public RandomMovement, public CheckCollision {
     public:
         /** Constructor */
         Alien();
@@ -36,11 +37,6 @@
          */
         void draw_alien(N5110 &lcd, Vector2D spaceship_pos, Direction d_, 
         int map_length_, int position_x_map_, bool alien_collision);
-        
-        /** Checks if bullet collides with a alien 
-         * @param bullet @details Weapons object
-         */
-        bool check_collision(Weapons bullet);
     
     // Accessors and mutators --------------------------------------------------
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckAlienCollision/CheckAlienCollision.cpp	Fri May 15 14:19:54 2020 +0000
@@ -0,0 +1,40 @@
+#include "CheckAlienCollision.h"
+
+bool CheckAlienCollision::check_alien_collision(Alien alien) {
+    bool collision = false; 
+    Vector2D alien_pos = alien.get_pos();
+    
+    // checks collision of top left corner of alien with people     
+    if(alien_pos.x >= position_x_ &&
+    alien_pos.x <= (position_x_ + sprite_x_length) && 
+    position_y_ <= alien_pos.y && 
+    alien_pos.y <= (position_y_ + sprite_y_length)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of top right corner of alien with people   
+    }else if((alien_pos.x + 7) >= position_x_ &&
+    (alien_pos.x + 7) <= (position_x_ + sprite_x_length) && 
+    position_y_ <= alien_pos.y && 
+    alien_pos.y <= (position_y_ + sprite_y_length)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of bottom left corner of alien with people   
+    }else if(alien_pos.x >= position_x_ &&
+    alien_pos.x <= (position_x_ + sprite_x_length) && 
+    position_y_ <= (alien_pos.y + 6)&& 
+   (alien_pos.y + 6) <= (position_y_ + sprite_y_length)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of bottom right corner of alien with people    
+    }else if((alien_pos.x + 7) >= position_x_ &&
+    (alien_pos.x + 7) <= (position_x_ + sprite_x_length) && 
+    position_y_ <= (alien_pos.y + 6) && 
+    (alien_pos.y + 6) <= (position_y_ + sprite_y_length)){
+        collision = true;
+        alien_collision_flag = true;
+    } 
+    return collision;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckAlienCollision/CheckAlienCollision.h	Fri May 15 14:19:54 2020 +0000
@@ -0,0 +1,37 @@
+#ifndef CHECKALIENCOLLISION_H
+#define CHECKALIENCOLLISION_H
+
+// Included libraries ----------------------------------------------------------
+#include "mbed.h"
+#include "Position.h"
+#include "Alien.h"
+
+/**  CheckAlienCollision class
+* @brief Check Alien Collision perant class
+* @author Benjamin Evans, University of Leeds
+* @date May 2020
+*/         
+class CheckAlienCollision: virtual public Position{    
+    public: 
+    // Function prototypes -----------------------------------------------------
+        
+         /** Checks if alien collides with a sprite
+         * @param alien @details Alien object
+         */
+        bool check_alien_collision(Alien alien);
+        
+    protected:    
+    // Variables ---------------------------------------------------------------
+        
+        /** Length of sprite in x direction */
+        int sprite_x_length; 
+        
+        /** Length of sprite in y direction */
+        int sprite_y_length; 
+        
+        /** Flag if there is is a alien collision*/
+        bool alien_collision_flag;
+        
+};
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckCollisions/CheckCollision.cpp	Fri May 15 14:19:54 2020 +0000
@@ -0,0 +1,38 @@
+#include "CheckCollision.h"
+
+bool CheckCollision::check_collision(Weapons bullet) {
+    bool collision = false; 
+    //printf ("Collision 1 = %d\n", collision);
+    Vector2D bullet_pos = bullet.get_pos();
+    // checks collision if bullet is going in east direction 
+    if(bullet.get_direction()){
+        //int bullet_pos_twox = bullet_pos.x;
+        //int bullet_pos_twoy = bullet_pos.y;
+        //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox);
+        //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy);
+        
+        if((bullet_pos.x + 1) >= position_x_ && 
+        (bullet_pos.x + 1) <= (position_x_ + sprite_x_length ) && 
+        position_y_ <= bullet_pos.y && 
+        bullet_pos.y <= (position_y_ + sprite_y_length )){
+            collision = true;
+        }
+    //printf ("Collision 2 = %d\n", collision);  
+      
+    // checks collision if bullet is going in west direction 
+    }else if(!bullet.get_direction()){
+        
+        if(bullet_pos.x <= (position_x_ + sprite_x_length ) && 
+        bullet_pos.x >= position_x_ && 
+        position_y_ <= bullet_pos.y && 
+        bullet_pos.y <= (position_y_ + sprite_y_length )){
+            collision = true;
+        }
+        
+     //printf ("Collision 3 = %d\n", collision);   
+    }
+    
+    //printf ("Collision 4 = %d\n", collision);   
+    return collision;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CheckCollisions/CheckCollision.h	Fri May 15 14:19:54 2020 +0000
@@ -0,0 +1,34 @@
+#ifndef CHECKCOLLISION_H
+#define CHECKCOLLISION_H
+
+// Included libraries ----------------------------------------------------------
+#include "mbed.h"
+#include "Position.h"
+#include "Weapons.h"
+
+/** CheckCollision class
+* @brief Check Collision perant class
+* @author Benjamin Evans, University of Leeds
+* @date May 2020
+*/         
+class CheckCollision: virtual public Position{    
+    public: 
+    // Function prototypes -----------------------------------------------------
+    
+        /** Checks if bullet collides with a sprite
+         * @param bullet @details Weapons object
+         */
+        bool check_collision(Weapons bullet); 
+        
+    protected:    
+    // Variables ---------------------------------------------------------------
+        
+        /** Length of sprite in x direction */
+        int sprite_x_length; 
+        
+        /** Length of sprite in y direction */
+        int sprite_y_length; 
+        
+};
+ 
+#endif
\ No newline at end of file
--- a/People/People.cpp	Fri May 15 12:05:33 2020 +0000
+++ b/People/People.cpp	Fri May 15 14:19:54 2020 +0000
@@ -17,6 +17,8 @@
 }
 
 void People::init(Gamepad &pad, int position_x_start) {
+    sprite_x_length = 4;
+    sprite_y_length = 5;
     position_x_ = position_x_start;
     position_y_ = 43;
     alien_collision_flag = false;
@@ -56,45 +58,6 @@
     }   
 }   
 
-bool People::check_alien_collision(Alien alien) {
-    bool collision = false; 
-    Vector2D alien_pos = alien.get_pos();
-    
-    // checks collision of top left corner of alien with people     
-    if(alien_pos.x >= position_x_ &&
-    alien_pos.x <= (position_x_ + 4) && 
-    position_y_ <= alien_pos.y && 
-    alien_pos.y <= (position_y_ + 5)){
-        collision = true;
-        alien_collision_flag = true;
-        
-    // checks collision of top right corner of alien with people   
-    }else if((alien_pos.x + 7) >= position_x_ &&
-    (alien_pos.x + 7) <= (position_x_ + 4) && 
-    position_y_ <= alien_pos.y && 
-    alien_pos.y <= (position_y_ + 5)){
-        collision = true;
-        alien_collision_flag = true;
-        
-    // checks collision of bottom left corner of alien with people   
-    }else if(alien_pos.x >= position_x_ &&
-    alien_pos.x <= (position_x_ + 4) && 
-    position_y_ <= (alien_pos.y + 6)&& 
-   (alien_pos.y + 6) <= (position_y_ + 5)){
-        collision = true;
-        alien_collision_flag = true;
-        
-    // checks collision of bottom right corner of alien with people    
-    }else if((alien_pos.x + 7) >= position_x_ &&
-    (alien_pos.x + 7) <= (position_x_ + 4) && 
-    position_y_ <= (alien_pos.y + 6) && 
-    (alien_pos.y + 6) <= (position_y_ + 5)){
-        collision = true;
-        alien_collision_flag = true;
-    } 
-    return collision;
-}
-
 bool People::get_alien_collision_flag(){
     return alien_collision_flag;
 }
\ No newline at end of file
--- a/People/People.h	Fri May 15 12:05:33 2020 +0000
+++ b/People/People.h	Fri May 15 14:19:54 2020 +0000
@@ -7,14 +7,14 @@
 #include "Gamepad.h"
 #include "RandomMovement.h"
 #include "Alien.h"
-
+#include "CheckAlienCollision.h"
 
 /** People class
  * @brief Draws people and moves them if collected by aliens
  * @author Benjamin Evans, University of Leeds
  * @date May 2020
  */ 
-class People: public RandomMovement {
+class People: public RandomMovement, public CheckAlienCollision {
     public:
         /** Constructor */
         People();
@@ -32,11 +32,6 @@
          */
         void draw_people(N5110 &lcd, Direction d_, int map_length_, 
         int position_x_map_);
-        
-        /** Checks if alien collides with a people
-         * @param alien @details Alien object
-         */
-        bool check_alien_collision(Alien alien);
     
     // Accessors and mutators --------------------------------------------------
         
@@ -57,9 +52,6 @@
         void off_screen_x_y_checker(int map_length_, int position_x_map_);
         
     // Variables ---------------------------------------------------------------
-         
-        /** Flag if there is is a alien collision*/
-        bool alien_collision_flag;
         
         /** People movement counter */
         int people_move_counter_; 
--- a/RandomMovement/RandomMovement.h	Fri May 15 12:05:33 2020 +0000
+++ b/RandomMovement/RandomMovement.h	Fri May 15 14:19:54 2020 +0000
@@ -10,7 +10,7 @@
 * @author Benjamin Evans, University of Leeds
 * @date May 2020
 */         
-class RandomMovement: public Position{    
+class RandomMovement: virtual public Position{    
     protected: 
     // Function prototypes -----------------------------------------------------
     
--- a/Spaceship/Spaceship.cpp	Fri May 15 12:05:33 2020 +0000
+++ b/Spaceship/Spaceship.cpp	Fri May 15 14:19:54 2020 +0000
@@ -23,6 +23,10 @@
 }
  
 void Spaceship::init() {
+    CheckCollision::sprite_x_length = 13;
+    CheckCollision::sprite_y_length = 4;
+    CheckAlienCollision::sprite_x_length = 13;
+    CheckAlienCollision::sprite_y_length = 4;
     position_x_ = 36;
     position_y_ = 22;
     
@@ -90,68 +94,6 @@
     }
 }
 
-bool Spaceship::check_collision(Weapons bullet) {
-    bool collision = false; 
-    Vector2D bullet_pos = bullet.get_pos();
-    
-    // checks collision if bullet is going in east direction 
-    if(bullet.get_direction()){
-        
-        if((bullet_pos.x + 1)>= position_x_ &&
-        (bullet_pos.x + 1) <= (position_x_ + 13) && 
-        position_y_ <= bullet_pos.y && 
-        bullet_pos.y <= (position_y_ + 4)){
-            collision = true;
-        } 
-      
-    // checks collision if bullet is going in west direction 
-    }else if(!bullet.get_direction()){
-        
-        if(bullet_pos.x <= (position_x_ + 13) && 
-        bullet_pos.x >= position_x_ &&
-        position_y_ <= bullet_pos.y && 
-        bullet_pos.y <= (position_y_ + 4)){
-            collision = true;
-        } 
-    }
-    return collision;
-}
-
-bool Spaceship::check_alien_collision(Alien alien) {
-    bool collision = false; 
-    Vector2D alien_pos = alien.get_pos();
-    
-    // checks collision of top left corner of alien with spacehsip     
-    if(alien_pos.x >= position_x_ &&
-    alien_pos.x <= (position_x_ + 13) && 
-    position_y_ <= alien_pos.y && 
-    alien_pos.y <= (position_y_ + 4)){
-        collision = true;
-        
-    // checks collision of top right corner of alien with spacehsip   
-    }else if((alien_pos.x + 7) >= position_x_ &&
-    (alien_pos.x + 7) <= (position_x_ + 13) && 
-    position_y_ <= alien_pos.y && 
-    alien_pos.y <= (position_y_ + 4)){
-        collision = true;
-        
-    // checks collision of bottom left corner of alien with spacehsip   
-    }else if(alien_pos.x >= position_x_ &&
-    alien_pos.x <= (position_x_ + 13) && 
-    position_y_ <= (alien_pos.y + 6)&& 
-   (alien_pos.y + 6) <= (position_y_ + 4)){
-        collision = true;
-        
-    // checks collision of bottom right corner of alien with spacehsip    
-    }else if((alien_pos.x + 7) >= position_x_ &&
-    (alien_pos.x + 7) <= (position_x_ + 13) && 
-    position_y_ <= (alien_pos.y + 6) && 
-    (alien_pos.y + 6) <= (position_y_ + 4)){
-        collision = true;
-    } 
-    return collision;
-}
-
 bool Spaceship::get_spaceship_sprite_direction(){
     return direction_;
 }
\ No newline at end of file
--- a/Spaceship/Spaceship.h	Fri May 15 12:05:33 2020 +0000
+++ b/Spaceship/Spaceship.h	Fri May 15 14:19:54 2020 +0000
@@ -8,13 +8,15 @@
 #include "Position.h"
 #include "Weapons.h"
 #include "Alien.h"
+#include "CheckCollision.h"
+#include "CheckAlienCollision.h"
 
 /** Spaceship class
  * @brief Draws and moves spaceship
  * @author Benjamin Evans, University of Leeds
  * @date April 2020
  */
-class Spaceship: public Position{
+class Spaceship: public CheckAlienCollision, public CheckCollision {
     public:
         /** Constructor */
         Spaceship();
@@ -35,16 +37,6 @@
          */
         void movement(Direction d_);   
         
-         /** Checks if bullet collides with a spaceship
-         * @param bullet @details Weapons object
-         */
-        bool check_collision(Weapons bullet);
-        
-        /** Checks if alien collides with a spaceship
-         * @param alien @details Alien object
-         */
-        bool check_alien_collision(Alien alien);
-        
     // Accessors and mutators --------------------------------------------------
         
         /** Gets sprtie directon if spaceship