Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
evanso
Date:
Tue May 12 15:08:56 2020 +0000
Parent:
23:cc44e26c08fa
Child:
25:70b55f5bfc87
Commit message:
Alien moves randomly and loops with the map when the spaceship get to the edge of the map. Alien also cant move off the map and can move towards the spaceship.

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
Map/Map.cpp 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
Spaceship/Spaceship.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Alien/Alien.cpp	Wed May 06 12:15:16 2020 +0000
+++ b/Alien/Alien.cpp	Tue May 12 15:08:56 2020 +0000
@@ -17,26 +17,93 @@
     
 }
 
-void Alien::init() {
+void Alien::init(Gamepad &pad) {
     position_x_alien_ = 10;
     position_y_alien_ = 22;
     alien_move_counter = 0;
+    srand(pad.read_adc()*64000);
+    random_move_counter_ = 0;
+    random_direction_ = 0;
+    alien_general_direction = rand()%2;
 }
-
-void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_) {
+ 
+void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, int map_length_, int position_x_map_) {
     //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 moves on its own but at half speed of spaceship 
+    if (alien_move_counter%2 == 0) {
+        if (random_move_counter_ == 0){
+            set_random_move();
+        }
+        move_direction();
+        off_screen_x_y_checker(map_length_, position_x_map_);
+        random_move_counter_ --; 
+         
+        //printf("random_move_counter_ =  %d\n", random_move_counter__);
     }
     alien_move_counter++;
     
     lcd.drawSprite(position_x_alien_, position_y_alien_, 6, 7, (int*)k_alien_sprite);
 }
 
-void Alien::move_alien(Vector2D spaceship_pos){
+void Alien::set_random_move(){
+    // alien only moves in one general direction
+    if(alien_general_direction){
+        random_direction_ = rand() % 3;
+    }else{
+        random_direction_ = rand() % 3 + 3;
+        printf("\random_direction_ =  %d\n", random_direction_);
+    }    
+    random_move_counter_ = rand() % 10 + 20;
+}
+
+void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_){
+    // checks y postion of alien and then alters moement direction if at edge of map
+    if (position_y_alien_ < 0) {
+        // sets alien direction to one that increces y value 
+        if(alien_general_direction){
+            random_direction_ = 1;
+        }else{
+            random_direction_ = 4;
+        }
+    }else if (position_y_alien_ > 40) {
+        // sets alien direction to one that decreces y value 
+        if(alien_general_direction){
+            random_direction_ = 2;
+        }else{
+            random_direction_ = 5;
+        }
+    } 
+    
+    // loops the alien round if it reaches the end of the map 
+    if(position_x_alien_ <= (84 - map_length_)){
+       position_x_alien_ += map_length_; 
+    }else if (position_x_alien_ >= map_length_){
+        position_x_alien_ -= map_length_ + 10;
+    }   
+    
+    // Debug and check variables when defined  
+    #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG   
+        printf("\nposition_x_map_ =  %d\n", position_x_map_);
+        printf("map_length_  =  %d\n", map_length_ );
+        printf("map_length_ + position_x_map_ =  %d\n", map_length_ + position_x_map_);
+        printf("alien x pos = %d\n",position_x_alien_);
+    #endif
+}   
+
+void Alien::move_direction(){
+    switch (random_direction_) {
+    case 0: set_alien_direction(1, 0); break;
+    case 1: set_alien_direction(1, 1); break;
+    case 2: set_alien_direction(1, -1); break;
+    case 3: set_alien_direction(-1, 0); break;
+    case 4: set_alien_direction(-1, 1); break;
+    case 5: set_alien_direction(-1, -1); break;
+    }
+}
+
+void Alien::move_hunt_mode(Vector2D spaceship_pos){
     if(spaceship_pos.x <= position_x_alien_){
        position_x_alien_ --;
     }else{
@@ -47,6 +114,7 @@
 bool Alien::check_collision(Weapons bullet) {
     bool collision = false; 
     //printf ("Collision 1 = %d\n", collision);
+    
     // checks collision if bullet is going in east direction 
     if(bullet.get_direction()){
         Vector2D bullet_pos_two = bullet.get_pos_two();
@@ -54,14 +122,16 @@
         //int bullet_pos_twoy = bullet_pos_two.y;
         //printf ("alien x  = %d, bullet x = %d\n", position_x_alien_,bullet_pos_twox );
         //printf ("alien y  = %d, bullet y = %d\n", position_y_alien_,bullet_pos_twoy );
-        if(bullet_pos_two.x >= position_x_alien_ && bullet_pos_two.x <= position_x_alien_ + 6 && position_y_alien_ <= bullet_pos_two.y <= position_y_alien_ + 7){
+        
+        if(bullet_pos_two.x >= position_x_alien_ && position_y_alien_ <= bullet_pos_two.y && bullet_pos_two.y <= (position_y_alien_ + 7)){
             collision = true;
         }
-    //printf ("Collision 2 = %d\n", collision);    
+    //printf ("Collision 2 = %d\n", collision);  
+      
     // checks collision if bullet is going in west direction 
     }else if(!bullet.get_direction()){
         Vector2D bullet_pos_one = bullet.get_pos_one();
-        if(bullet_pos_one.x >= position_x_alien_  && bullet_pos_one.x <= position_x_alien_ + 6  && position_y_alien_ <= bullet_pos_one.y <= position_y_alien_ + 7){
+        if(bullet_pos_one.x <= position_x_alien_ + 6 && position_y_alien_ <= bullet_pos_one.y && bullet_pos_one.y <= (position_y_alien_ + 7)){
             collision = true;
         }
      //printf ("Collision 3 = %d\n", collision);   
@@ -84,4 +154,9 @@
 Vector2D Alien::get_pos(){
     Vector2D pos = {position_x_alien_,position_y_alien_};
     return pos;
+}
+
+void Alien::set_alien_direction(int x_change,int y_change){
+    position_x_alien_ += x_change;
+    position_y_alien_ += y_change;
 }
\ No newline at end of file
--- a/Alien/Alien.h	Wed May 06 12:15:16 2020 +0000
+++ b/Alien/Alien.h	Tue May 12 15:08:56 2020 +0000
@@ -22,12 +22,12 @@
         ~Alien();
         
         /** Initalises Alien */
-        void init();
+        void init(Gamepad &pad);
     
         /** Draws the alien
          * @param lcd @details : N5110 object
          */
-        void draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_);
+        void draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, int map_length_, int position_x_map_);
         
         /** Checks if bullet collides with a alien 
          * @param lcd @details : N5110 object
@@ -47,7 +47,7 @@
         /** Moves the alien towards the spaceship and around the map 
          * @param spaceship_pos @details : Position of the spaceship
          */
-        void move_alien(Vector2D spaceship_pos);
+        void move_hunt_mode(Vector2D spaceship_pos);
        
         /** Calulates the aliens movement depeding on spaceship positions and joystick input 
          * @param d_ @details : Direction object of joystick
@@ -55,6 +55,24 @@
          */
         int calc_alien_movement(Direction d_);
         
+        /** Generates the random move dirction and length for the alien*/
+        void set_random_move();
+        
+        /** Gets the movement direction of the alien */
+        void move_direction();
+        
+        /** Changes the x and y positions of the alien depeding on the movement direction 
+         * @param x_change @details : number to change alien x position by
+         * @param y_change @detials : number to change alien y position by
+         */
+        void set_alien_direction(int x_change,int y_change);
+        
+        /** Stops the alien from moving off the edge of the map and moves alien if the map loops
+         * @param map_length_@details : length of the map  
+         * @param position_x_map_ @detials : the drawing start posisiton of the map 
+         */
+        void off_screen_x_y_checker(int map_length_, int position_x_map_);
+        
     // Variables ---------------------------------------------------------------
          
         //Aliens x position on lcd
@@ -64,8 +82,16 @@
         int position_y_alien_;
         
         // Alien movement counter
-        int alien_move_counter;     
+        int alien_move_counter; 
+        
+        // Alien randome move counter 
+        int random_move_counter_;
         
+        // Random direction variable
+        int random_direction_;    
+        
+        // Aliens genreal movement direction, 1 = East , 0 = West 
+        int alien_general_direction;
 };
  
 #endif
\ No newline at end of file
--- a/GameEngine/GameEngine.cpp	Wed May 06 12:15:16 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Tue May 12 15:08:56 2020 +0000
@@ -56,7 +56,7 @@
     // Alien object
     Alien new_alien;
 
-    new_alien.init();
+    new_alien.init(pad);
     
     // Stores alien object in vector 
     alien_vector.push_back(new_alien);
@@ -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,spaceship.get_pos(),d_);
+        alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, map.get_length_map(), map.get_position_x_map());
         
         // Deletes bullet and alien if collision detected
         if (alien_vector[i].check_collision(bullet_vector[i])){
--- a/Map/Map.cpp	Wed May 06 12:15:16 2020 +0000
+++ b/Map/Map.cpp	Tue May 12 15:08:56 2020 +0000
@@ -19,6 +19,7 @@
     
     // Initialises random arrays to make random map
     fill_random_arrays(pad);
+    
 }
 
 void Map::fill_random_arrays(Gamepad &pad){
@@ -53,11 +54,11 @@
 }
 
 void Map::draw_map(N5110 &lcd, Direction d_){
-    //usb.printf("position_x_map_ = %d\n", position_x_map_);
+    //printf("position_x_map_ map = %d\n", position_x_map_);
     //usb.printf("move map = %d\n", move_map);
-
+    
     reset_position_x_map_to_origonal_ = position_x_map_;
-    int map_length_ = 0;
+    map_length_ = 0;
     
     //prints main part of map
     for(int i = 0; i < 11; i++){
@@ -127,7 +128,7 @@
         return 0;
     }
     
-    // Debug and check variables when function is called 
+    // Debug and check variables when defined  
     #ifdef CALCULATE_MAP_MOVEMENT_DEBUG   
         printf("move map = %d\n", move_map_);   
         printf("direction = %d\n", d_);     
@@ -138,3 +139,7 @@
 int Map::get_position_x_map(){
     return position_x_map_;
 }
+
+int Map::get_length_map(){
+    return map_length_;
+}
--- a/Map/Map.h	Wed May 06 12:15:16 2020 +0000
+++ b/Map/Map.h	Tue May 12 15:08:56 2020 +0000
@@ -36,7 +36,9 @@
         /** Gets x postion of the map for testing 
          * @return maps x postion 
          */
-        int get_position_x_map(); 
+        int get_position_x_map();
+        
+        int get_length_map();
        
     private:
     // Functions prototypes ------------------------------------------------
@@ -74,9 +76,6 @@
         int calc_map_movement(Direction d_);
     
     // Variables -----------------------------------------------------------
-    
-        // Map x postion on lcd
-        int position_x_map_;
         
         // Map y postion on lcd
         int position_y_map_;
@@ -84,9 +83,6 @@
         // Float to store random seed for random function 
         float rand_seed_;
         
-        // Map length 
-        int map_length_;
-        
         // Arrays to hold random heights of triangles and lengths of lines
         int rand_heights_[12]; 
         int rand_lengths_[12];
@@ -96,6 +92,12 @@
         
         //Required to reset the map to it's origonal postion at end of each frame, as draw line and triangle functions change position_x_map_
         int reset_position_x_map_to_origonal_;    
+          
+        // Map length 
+        int map_length_;
+        
+        // Map x postion on lcd
+        int position_x_map_;
 };
  
 #endif
\ No newline at end of file
--- a/Spaceship/Spaceship.cpp	Wed May 06 12:15:16 2020 +0000
+++ b/Spaceship/Spaceship.cpp	Tue May 12 15:08:56 2020 +0000
@@ -44,7 +44,7 @@
 }
 
 void Spaceship::off_screen_x_y_checker(){
-    // checks y postion spand alters y position 
+    // checks y postion of spaceship and then alters y position if off map
     if (position_y_spaceship_ < 0) {
         position_y_spaceship_ = 0;
     }else if (position_y_spaceship_ > 44) {