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 05 16:16:06 2020 +0000
Parent:
20:febd920ec29e
Child:
22:053c11a202e1
Commit message:
Alien now moves with the map as the spaceship moves. Alien also moves towards the x position of the spaceship by its self.

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
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
Map/Map.h Show annotated file Show diff for this revision Revisions of this file
Map/Map_test.h 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
Spaceship/Spaceship_test.h Show annotated file Show diff for this revision Revisions of this file
--- a/Alien/Alien.cpp	Mon May 04 10:47:06 2020 +0000
+++ b/Alien/Alien.cpp	Tue May 05 16:16:06 2020 +0000
@@ -10,7 +10,7 @@
 };
 
 Alien::Alien() {
-    
+
 }
  
 Alien::~Alien() {
@@ -20,12 +20,30 @@
 void Alien::init() {
     position_x_alien_ = 10;
     position_y_alien_ = 22;
+    alien_move_counter = 0;
 }
 
-void Alien::draw_alien(N5110 &lcd) {
+void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_) {
+    //moves alien spaceship movemen 
+    position_x_alien_ += calc_alien_movement(d_);
+    
+    //Alien moves on its own but at 3rd speed of spaceship 
+    if (alien_move_counter%3 == 0) {
+        move_alien(spaceship_pos);
+    }
+    alien_move_counter++;
+    
     lcd.drawSprite(position_x_alien_, position_y_alien_, 6, 7, (int*)k_alien_sprite);
 }
 
+void Alien::move_alien(Vector2D spaceship_pos){
+    if(spaceship_pos.x <= position_x_alien_){
+       position_x_alien_ --;
+    }else{
+        position_x_alien_ ++;
+    }
+}
+
 bool Alien::check_collision(Weapons bullet) {
     bool collision = false; 
     //printf ("Collision 1 = %d\n", collision);
@@ -50,4 +68,15 @@
     }
     //printf ("Collision 4 = %d\n", collision);   
     return collision;
+}
+
+int Alien::calc_alien_movement(Direction d_){  
+    // moves the alien in oposite direction to spaceship when it's position is at min and max x positions and joystick has direction     
+    if (d_ == W || d_ == NW || d_ == SW){ 
+        return 2;
+    }else if (d_ == E || d_ == NE || d_ == SE){ 
+        return -2; 
+    }else {
+        return 0;
+    }
 }
\ No newline at end of file
--- a/Alien/Alien.h	Mon May 04 10:47:06 2020 +0000
+++ b/Alien/Alien.h	Tue May 05 16:16:06 2020 +0000
@@ -27,20 +27,28 @@
         /** Draws the alien
          * @param lcd @details : N5110 object
          */
-        void draw_alien(N5110 &lcd);
+        void draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_);
         
         /** Checks if bullet collides with a alien 
          * @param lcd @details : N5110 object
          */
         bool check_collision(Weapons bullet);
     
-        // Accessors and mutators -----------------------------------------------
-        
-        void set_position(int position_x_alien, int opsition_y_alien);
+    // Accessors and mutators --------------------------------------------------
     
     private:
     // Function prototypes -----------------------------------------------------
-        
+       
+        /** Moves the alien towards the spaceship and around the map 
+         * @param spaceship_pos @details : Position of the spaceship
+         */
+        void move_alien(Vector2D spaceship_pos);
+       
+        /** Calulates the aliens movement depeding on spaceship positions and joystick input 
+         * @param d_ @details : Direction object of joystick
+         * @retrun integer @details move alien value for alien draw function 
+         */
+        int calc_alien_movement(Direction d_);
         
     // Variables ---------------------------------------------------------------
          
@@ -49,6 +57,10 @@
             
         // Aliens y position on lcd
         int position_y_alien_;
+        
+        // Alien movement counter
+        int alien_move_counter;     
+        
 };
  
 #endif
\ No newline at end of file
--- a/GameEngine/GameEngine.cpp	Mon May 04 10:47:06 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Tue May 05 16:16:06 2020 +0000
@@ -55,7 +55,7 @@
 void GameEngine::create_alien(){
     // Alien object
     Alien new_alien;
-    
+
     new_alien.init();
     
     // Stores alien object in vector 
@@ -78,7 +78,7 @@
 void GameEngine::draw_aliens(){
     // interates over alien vector and get each new_alien object to draw its self
     for (int i = 0; i < alien_vector.size(); i++){
-        alien_vector[i].draw_alien(lcd);
+        alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_);
         
         // Deletes bullet and alien if collision detected
         if (alien_vector[i].check_collision(bullet_vector[i])){
--- a/GameEngine/GameEngine.h	Mon May 04 10:47:06 2020 +0000
+++ b/GameEngine/GameEngine.h	Tue May 05 16:16:06 2020 +0000
@@ -32,10 +32,10 @@
         /** Main gameplay loop that runs playable part of game */
         void gameplay_loop();
         
-        // Accessors and mutators ----------------------------------------------
+    // Accessors and mutators ----------------------------------------------
           
     private:
-        // Function prototypes -------------------------------------------------
+    // Function prototypes -------------------------------------------------
         
         /** Gets joystick direction from gamepad and stores it in d_*/
         void read_joystick_direction();
@@ -52,7 +52,7 @@
         /** Draws each alien object*/
         void draw_aliens();
         
-        // Variables -----------------------------------------------------------
+    // Variables -----------------------------------------------------------
         
         // Direction of joystick
         Direction d_; 
@@ -63,7 +63,7 @@
         // Vector to store each new alien object
         std::vector<Alien> alien_vector;
         
-        // Objects -------------------------------------------------------------
+    // Objects -------------------------------------------------------------
     
         // Gamepad object 
         Gamepad pad;
@@ -76,7 +76,6 @@
         
         // Map object 
         Map map; 
-        
     
 };
  
--- a/Map/Map.h	Mon May 04 10:47:06 2020 +0000
+++ b/Map/Map.h	Tue May 05 16:16:06 2020 +0000
@@ -31,7 +31,7 @@
          */
         void draw_map(N5110 &lcd, Direction d_);
     
-        // Accessors and mutators ----------------------------------------------
+    // Accessors and mutators ----------------------------------------------
         
         /** Gets x postion of the map for testing 
          * @return maps x postion 
@@ -39,7 +39,7 @@
         int get_position_x_map(); 
        
     private:
-        // Functions prototypes ------------------------------------------------
+    // Functions prototypes ------------------------------------------------
     
         /** Draws a triangle from position with specified hight wich represents a mountain of the map
          * @param lcd, tirangle_height @details : N5110 object and teh random hight of triangle produced
@@ -68,11 +68,12 @@
         void fill_random_arrays(Gamepad &pad);
         
         /** Calulates the map movement depeding on spaceship positions and joystick input 
-         * @retrun move_map_ @details move map variable for map draw function 
+         * @param d_ @details : Direction object of joystick
+         * @retrun inger @details move map value for map draw function 
          */
         int calc_map_movement(Direction d_);
     
-        // Variables -----------------------------------------------------------
+    // Variables -----------------------------------------------------------
     
         // Map x postion on lcd
         int position_x_map_;
--- a/Map/Map_test.h	Mon May 04 10:47:06 2020 +0000
+++ b/Map/Map_test.h	Tue May 05 16:16:06 2020 +0000
@@ -10,7 +10,7 @@
 */
 
 bool map_move_test(Direction d_, int expected_x_position){
-    // Objects reqired for test ------------------------------------------------
+    // Objects reqired for test
     Gamepad pad;
     Map map;
     GameEngine engine;
--- a/Spaceship/Spaceship.h	Mon May 04 10:47:06 2020 +0000
+++ b/Spaceship/Spaceship.h	Tue May 05 16:16:06 2020 +0000
@@ -33,7 +33,7 @@
          */
         void movement(Direction d_);   
         
-        // Accessors and mutators -----------------------------------------------
+    // Accessors and mutators -----------------------------------------------
         
         /** Gets sprtie directon if spaceship 
          * @details true = east, false = west
--- a/Spaceship/Spaceship_test.h	Mon May 04 10:47:06 2020 +0000
+++ b/Spaceship/Spaceship_test.h	Tue May 05 16:16:06 2020 +0000
@@ -9,7 +9,7 @@
 */
 
 bool spaceship_movement_test(Direction d_, int expected_x,int expected_y){
-    // Objects reqired for test ------------------------------------------------
+    // Objects reqired for test 
     Gamepad pad;
     Spaceship spaceship;
     
@@ -39,7 +39,7 @@
 }
 
 bool spaceship_draw_test(Direction d_, int expected_pixel_status, int expected_postion_x, int expected_postion_y){
-    // Objects reqired for test ------------------------------------------------
+    // Objects reqired for test 
     Gamepad pad;
     Spaceship spaceship;
     N5110 lcd;