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:
Wed May 13 23:07:52 2020 +0000
Parent:
27:8bb2bd97c319
Child:
29:e96d91f1d39c
Commit message:
Added Random Movement base class which is inherited by alien bullets and alien direction. Alien now shoots bullets randomly 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
Explosion/Explosion.cpp 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
Position/Position.h Show annotated file Show diff for this revision Revisions of this file
RandomMovement/RandomMovement.cpp 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
Weapons/Weapons.cpp Show annotated file Show diff for this revision Revisions of this file
Weapons/Weapons.h Show annotated file Show diff for this revision Revisions of this file
--- a/Alien/Alien.cpp	Wed May 13 14:30:12 2020 +0000
+++ b/Alien/Alien.cpp	Wed May 13 23:07:52 2020 +0000
@@ -20,11 +20,12 @@
 void Alien::init(Gamepad &pad) {
     position_x_ = 10;
     position_y_ = 22;
-    alien_move_counter = 0;
+    alien_move_counter_ = 0;
     srand(pad.read_adc()*64000);
     random_move_counter_ = 0;
     random_direction_ = 0;
     direction_ = rand()%2;
+    alien_move_counter_ = 0;
 }
  
 void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, 
@@ -33,35 +34,25 @@
     position_x_ += calc_alien_movement(d_);
     
     //Alien moves on its own but at half speed of spaceship 
-    if (alien_move_counter%2 == 0) {
+    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_ --; 
+        alien_fire_counter_++;
          
-        //printf("random_move_counter_ =  %d\n", random_move_counter__);
+        //printf("random_move_counter_ =  %d\n", random_move_counter_);
     }
-    alien_move_counter++;
-    
+    alien_move_counter_++;
+
     lcd.drawSprite(position_x_, position_y_, 6, 7, (int*)k_alien_sprite);
 }
 
-void Alien::set_random_move(){
-    // alien only moves in one general direction
-    if(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
+    // checks y postion of alien and then alters alien movement direction if at 
+    // edge  of map
     if (position_y_ < 0) {
         // sets alien direction to one that increces y value 
         if(direction_){
@@ -91,21 +82,9 @@
         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_);
     #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_){
        position_x_ --;
@@ -165,7 +144,6 @@
     return pos;
 }
 
-void Alien::set_alien_direction(int x_change,int y_change){
-    position_x_ += x_change;
-    position_y_ += y_change;
-}
\ No newline at end of file
+int Alien::get_alien_fire_counter(){
+    return alien_move_counter_;
+} 
--- a/Alien/Alien.h	Wed May 13 14:30:12 2020 +0000
+++ b/Alien/Alien.h	Wed May 13 23:07:52 2020 +0000
@@ -7,13 +7,14 @@
 #include "Gamepad.h"
 #include "Weapons.h"
 #include "Position.h"
+#include "RandomMovement.h"
 
 /** Alien class
  * @brief Draws and moves aliens 
  * @author Benjamin Evans, University of Leeds
  * @date May 2020
  */ 
-class Alien: private Position {
+class Alien:private RandomMovement {
     public:
         /** Constructor */
         Alien();
@@ -46,7 +47,8 @@
          * @return position_x_alien_
          */
         Vector2D get_pos();
-    
+        
+        int get_alien_fire_counter(); 
     private:
     // Function prototypes -----------------------------------------------------
        
@@ -62,19 +64,6 @@
          */
         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  
@@ -86,13 +75,11 @@
     // Variables ---------------------------------------------------------------
          
         /** Alien movement counter */
-        int alien_move_counter;
-         
-        /** Alien randome move counter */
-        int random_move_counter_;
-         
-        /** Random direction variable */
-        int random_direction_;     
+        int alien_move_counter_; 
+        
+        /** Alien fire bullet counter */
+        int alien_fire_counter_;    
+        
 };
  
 #endif
\ No newline at end of file
--- a/Explosion/Explosion.cpp	Wed May 13 14:30:12 2020 +0000
+++ b/Explosion/Explosion.cpp	Wed May 13 23:07:52 2020 +0000
@@ -29,8 +29,8 @@
         animation_fsm[fsm_counter_].circle_one);
     }
     if(animation_fsm[fsm_counter_].draw_circle_two){
-        lcd.drawCircle(position_x_, position_y_,(explosion_radius_ - 2), a
-        nimation_fsm[fsm_counter_].circle_two);
+        lcd.drawCircle(position_x_, position_y_,(explosion_radius_ - 2), 
+        animation_fsm[fsm_counter_].circle_two);
     }
     
     // Slows down annimation change time, so eplosion animation is longer
--- a/GameEngine/GameEngine.cpp	Wed May 13 14:30:12 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Wed May 13 23:07:52 2020 +0000
@@ -47,7 +47,7 @@
         Weapons new_bullet;
         
         new_bullet.init(spaceship.get_pos(), 
-        spaceship.get_spaceship_sprite_direction());
+        spaceship.get_spaceship_sprite_direction(), true);
         
         // Stores bullet object in vector 
         bullet_vector.push_back(new_bullet);
@@ -75,15 +75,21 @@
 }
 
 void GameEngine::draw_bullets(){
-    // interates over bullet vector and get each new_bullet object to draw its 
+    // interates over bullet vectors and get each bullet object to draw its 
     // self and move
+    for (int i = 0; i < alien_bullet_vector.size(); i++){
+        alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
+        
+        // deletes bullet object after bullet has moved set distance   
+        if(alien_bullet_vector[i].get_bullet_delete_counter() >> 7){
+             alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
+        }
+    }
     for (int i = 0; i < bullet_vector.size(); i++){
         bullet_vector[i].draw_bullet(lcd);
-        
-        // deletes bullet object after bullet is out of view of paceship    
-        int bullet_delete_counter = 
-        bullet_vector[i].get_bullet_delete_counter();
-        if(bullet_delete_counter >> 5){
+         
+        // deletes bullet object after bullet has moved set distance   
+        if(bullet_vector[i].get_bullet_delete_counter() >> 5){
              bullet_vector.erase(bullet_vector.begin()+ i);
         }
     }
@@ -95,6 +101,25 @@
         alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, 
         map.get_length_map(), map.get_position_x_map());
         
+        // alien sprites fire bullet randomeley
+        if (alien_vector[i].get_alien_fire_counter()%60 == 0){
+            Weapons alien_bullet;
+            
+            // fires bullet towards direction of spaceship
+            bool alien_bullet_direction = false;
+            Vector2D spaceship_pos = spaceship.get_pos();
+            Vector2D alien_pos = alien_vector[i].get_pos();
+            if(spaceship_pos.x > alien_pos.x){
+                alien_bullet_direction = true;
+            }
+            
+            alien_bullet.init(alien_vector[i].get_pos(), 
+            alien_bullet_direction , false);
+        
+            // Stores bullet object in vector 
+            alien_bullet_vector.push_back(alien_bullet); 
+        }
+        
         // Deletes bullet and alien if collision detected
         if (alien_vector[i].check_collision(bullet_vector[i])){
             create_explosion();
--- a/GameEngine/GameEngine.h	Wed May 13 14:30:12 2020 +0000
+++ b/GameEngine/GameEngine.h	Wed May 13 23:07:52 2020 +0000
@@ -71,6 +71,9 @@
         /** Vector to store each new bullet object*/
         std::vector<Weapons> bullet_vector;
         
+        /** Vector to store each alien bullet object*/
+        std::vector<Weapons> alien_bullet_vector;
+        
         /** Vector to store each new alien object*/
         std::vector<Alien> alien_vector;
         
--- a/Position/Position.h	Wed May 13 14:30:12 2020 +0000
+++ b/Position/Position.h	Wed May 13 23:07:52 2020 +0000
@@ -9,11 +9,8 @@
 * @author Benjamin Evans, University of Leeds
 * @date May 2020
 */         
-class Position {
-    public:
-        
+class Position {   
     protected:   
-       
     // Varibles ---------------------------------------------------------------- 
         
         /** x position on lcd */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RandomMovement/RandomMovement.cpp	Wed May 13 23:07:52 2020 +0000
@@ -0,0 +1,28 @@
+#include "RandomMovement.h"
+
+void RandomMovement::move_direction(){
+    switch (random_direction_) {
+    case 0: set_sprite_direction(1, 0); break;
+    case 1: set_sprite_direction(1, 1); break;
+    case 2: set_sprite_direction(1, -1); break;
+    case 3: set_sprite_direction(-1, 0); break;
+    case 4: set_sprite_direction(-1, 1); break;
+    case 5: set_sprite_direction(-1, -1); break;
+    }
+}
+
+void RandomMovement::set_sprite_direction(int x_change,int y_change){
+    position_x_ += x_change;
+    position_y_ += y_change;
+}
+
+void RandomMovement::set_random_move(){
+    // sprite only moves in one general direction
+    if(direction_){
+        random_direction_ = rand() % 3;
+    }else{
+        random_direction_ = rand() % 3 + 3;
+        //printf("\random_direction_ =  %d\n", random_direction_);
+    }    
+    random_move_counter_ = rand() % 10 + 20;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RandomMovement/RandomMovement.h	Wed May 13 23:07:52 2020 +0000
@@ -0,0 +1,39 @@
+#ifndef RANDOMMOVEMENT_H
+#define RANDOMMOVEMENT_H
+
+// Included libraries ----------------------------------------------------------
+#include "mbed.h"
+#include "Position.h"
+
+/** Random Movement class
+* @brief Random Movement perant class
+* @author Benjamin Evans, University of Leeds
+* @date May 2020
+*/         
+class RandomMovement: protected Position{    
+    protected: 
+    // Function prototypes -----------------------------------------------------
+    
+        /** Changes the x and y positions of the sprite bject depeding on the  
+         * movement direction 
+         * @param x_change @details number to change sprite x position by
+         * @param y_change @detials number to change sprite y position by
+         */
+        void set_sprite_direction(int x_change,int y_change);
+        
+        /** Generates the random move dirction and length for the sprite */
+        void set_random_move();
+        
+        /** Gets the movement direction of the sprite */
+        void move_direction();
+        
+    // Variables ---------------------------------------------------------------
+        
+        /** Sprite random move counter */
+        int random_move_counter_;
+         
+        /** Random direction variable */
+        int random_direction_;  
+};
+ 
+#endif
\ No newline at end of file
--- a/Weapons/Weapons.cpp	Wed May 13 14:30:12 2020 +0000
+++ b/Weapons/Weapons.cpp	Wed May 13 23:07:52 2020 +0000
@@ -8,10 +8,16 @@
     
 }
  
-void Weapons::init(Vector2D spaceship_pos, bool spaceship_sprite_direction) {
-    calc_bullets_start_pos(spaceship_pos, spaceship_sprite_direction);
-    set_direction(spaceship_sprite_direction);
-    bullet_delete_cunter_ = 0;
+void Weapons::init(Vector2D sprite_pos, bool sprite_direction, 
+bool bullet_type) {
+    //bullet for alien or for spacship 
+    set_direction(sprite_direction);
+    if (bullet_type){
+        calc_bullets_start_pos(sprite_pos, sprite_direction);
+    }else{
+        calc_alien_bullets_start_pos(sprite_pos, sprite_direction);
+    }
+    bullet_delete_counter_ = 0;
 }
 
 void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos, 
@@ -38,9 +44,43 @@
     }else{
         position_x_ -= 3;
     }
+  
+    // increments counter
+    bullet_delete_counter_++;
+}
+
+void Weapons::calc_alien_bullets_start_pos(Vector2D alien_pos, 
+bool alien_sprite_direction_){
+    // Sets start position of bullet to middle of alien
+    position_x_ = alien_pos.x + 3;
+    position_y_ = alien_pos.y + 3;
+    
+    //gets a random direction for the bullet to travel in
+    set_random_move();
+}
+
+void Weapons::draw_alien_bullet(N5110 &lcd,Direction d_){
+    // draws then moves the bullet position
+    lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
+    
+    //Moves bullet in random direction and accomodates for spaceship movement
+    move_direction();
+    position_x_ += calc_bullet_movement(d_);
     
     // increments counter
-    bullet_delete_cunter_++;
+    bullet_delete_counter_++;
+}
+
+int Weapons::calc_bullet_movement(Direction d_){  
+    // moves the bullets 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;
+    }
 }
 
 Vector2D Weapons::get_pos_one(){
@@ -58,12 +98,12 @@
     return pos;
 }
 
-void Weapons::set_direction(bool spaceship_sprite_direction){
-    direction_ = spaceship_sprite_direction;   
+void Weapons::set_direction(bool sprite_direction){
+    direction_ = sprite_direction;   
 }
 
 int Weapons::get_bullet_delete_counter(){
-    return bullet_delete_cunter_; 
+    return bullet_delete_counter_; 
 }
 
 bool Weapons::get_direction(){
--- a/Weapons/Weapons.h	Wed May 13 14:30:12 2020 +0000
+++ b/Weapons/Weapons.h	Wed May 13 23:07:52 2020 +0000
@@ -5,14 +5,14 @@
 #include "mbed.h"
 #include "N5110.h"
 #include "Gamepad.h"
-#include "Position.h"
+#include "RandomMovement.h"
 
 /** Weapons class
  * @brief Draws and moves weapons 
  * @author Benjamin Evans, University of Leeds
  * @date April 2020
  */
-class Weapons: private Position{
+class Weapons: private RandomMovement {
     public:
         /** Constructor */
         Weapons();
@@ -21,16 +21,23 @@
         ~Weapons();
         
         /** Initalises Weapons 
-         * @param spaceship_pos @details vector 2D of bullet xy position 
-         * @param paceship_sprite_direction @details sprite direction bool, 
+         * @param sprite_pos @details vector 2D of sprite xy position 
+         * @param sprite_direction @details sprite direction bool, 
          * true = E, false = W
+         * @param bullet_type @details true = spaceship, false = alien
          */
-        void init(Vector2D spaceship_pos, bool spaceship_sprite_direction);
+        void init(Vector2D sprite_pos, bool sprite_direction,bool bullet_type);
         
         /** Draws the bullet and moves it in x direction each frame
          * @param lcd @details N5110 object
          */
         void draw_bullet(N5110 &lcd);
+        
+         /** Draws the aliens bullet and moves it in xy direction each frame
+         * @param lcd @details N5110 object
+         * @param d_ @details : Direction object of joystick
+         */
+        void draw_alien_bullet(N5110 &lcd, Direction d_);
          
     // Accessors and mutators --------------------------------------------------
           
@@ -65,7 +72,7 @@
          * @param spaceship_sprite_direction_ @details sprite direction bool, 
          * true = E, false = W
          */
-        void set_direction(bool spaceship_sprite_direction_);
+        void set_direction(bool sprite_direction_);
         
     private:
     // Function prototypes -----------------------------------------------------
@@ -77,11 +84,26 @@
          */
         void calc_bullets_start_pos(Vector2D spaceship_pos, 
         bool spaceship_sprite_direction_);
+        
+        /** Calculates allien bullets start postion
+         * @param alien_pos @details x and y postion of spaceship
+         * @param alien_sprite_direction_ @details sprite direction bool, 
+         * true = E, false = W
+         */
+        void calc_alien_bullets_start_pos(Vector2D alien_pos, 
+        bool alien_sprite_direction_);
+        
+        /** Calulates the bullet movement depeding on spaceship positions and 
+         * joystick input 
+         * @param d_ @details : Direction object of joystick
+         * @retrun integer @details move bullet value for bullet draw function 
+         */
+        int calc_bullet_movement(Direction d_);
     
     // Variables ---------------------------------------------------------------
         
         /** Counter to deleted bullet */
-        int bullet_delete_cunter_; 
+        int bullet_delete_counter_; 
 };
  
 #endif
\ No newline at end of file