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:
Sun May 03 18:15:58 2020 +0000
Parent:
18:11068b98e261
Child:
20:febd920ec29e
Commit message:
Bullets can now be shot in both directions. Began work on alien class.

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
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Alien/Alien.cpp	Sun May 03 18:15:58 2020 +0000
@@ -0,0 +1,29 @@
+#include "Alien.h"
+
+const int k_alien_sprite[8][9] = {
+    { 0,0,0,1,1,1,0,0,0},
+    { 0,0,1,1,1,1,1,0,0},
+    { 0,1,1,0,1,1,0,1,0},
+    { 0,1,1,0,1,1,0,1,0},
+    { 0,0,1,1,1,1,1,0,0},
+    { 0,0,1,0,1,0,1,0,0},
+    { 0,1,0,0,1,0,0,1,0},
+    { 1,0,0,0,1,0,0,0,1},
+};
+
+Alien::Alien() {
+    
+}
+ 
+Alien::~Alien() {
+    
+}
+
+void Alien::init() {
+    position_x_alien_ = 70;
+    position_y_alien_ = 22;
+}
+
+void Alien::draw(N5110 &lcd) {
+    lcd.drawSprite(position_x_alien_, position_y_alien_, 8, 9, (int*)k_alien_sprite);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Alien/Alien.h	Sun May 03 18:15:58 2020 +0000
@@ -0,0 +1,46 @@
+#ifndef ALIEN_H
+#define ALIEN_H
+ 
+// Included libraries -----------------------------------------------------------
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+/** Weapons class
+@brief Draws and moves weapons 
+@author Benjamin Evans, University of Leeds
+@date April 2020
+*/
+ 
+class Alien {
+    public:
+        /** Constructor */
+        Alien();
+        
+        /** Destructor */
+        ~Alien();
+        
+        /** Initalises Alien */
+        void init();
+        
+        void draw(N5110 &lcd);
+    
+        // Accessors and mutators -----------------------------------------------
+        
+        void set_position(int position_x_alien, int opsition_y_alien);
+        
+        
+    private:
+    // Function prototypes -----------------------------------------------------
+        
+        
+    // Variables ---------------------------------------------------------------
+         
+        //Aliens x position on lcd
+        int position_x_alien_;
+            
+        // Aliens y position on lcd
+        int position_y_alien_;
+};
+ 
+#endif
\ No newline at end of file
--- a/GameEngine/GameEngine.cpp	Fri May 01 20:37:10 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Sun May 03 18:15:58 2020 +0000
@@ -13,8 +13,6 @@
     lcd.init();
     spaceship.init();
     map.init(pad);
-    move_map_= 0;
-    is_bullet_firing_ = false;
 }
 
 void GameEngine::gameplay_loop() {
@@ -25,25 +23,12 @@
     // Gets movements  
     read_joystick_direction();
     spaceship.movement(d_);
-    
-    // Fires bullet 
-    if (pad.A_pressed()) {
-        is_bullet_firing_ = true;
-        //printf("is_bullet_firing_ = %d",is_bullet_firing_);
-    }
-    if (is_bullet_firing_){
-        Weapons new_bullet;
-        new_bullet.calc_bullets_start_pos(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction());
-        bullet_vector.push_back(new_bullet);
-        is_bullet_firing_ = false; 
-    }
-    for (int i = 0; i < bullet_vector.size(); i++){
-        bullet_vector[i].draw_bullet(lcd);
-    }
-    
+    create_bullet();
+
     // Draws 
     spaceship.draw(lcd);
     map.draw_map(lcd, d_);
+    draw_bullets();
 
     // refresh's screen
     lcd.refresh(); 
@@ -53,3 +38,22 @@
     d_ = pad.get_direction();
 }
 
+void GameEngine::create_bullet(){
+    if (pad.A_pressed()){
+        // Bullet object
+        Weapons new_bullet;
+        
+        new_bullet.init(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction());
+        
+        // Stores bullet object on vector 
+        bullet_vector.push_back(new_bullet);
+    }   
+}
+
+void GameEngine::draw_bullets(){
+    // interates over bullet vector and get each new_bullet draw it's self with their updated postion 
+    for (int i = 0; i < bullet_vector.size(); i++){
+        bullet_vector[i].draw_bullet(lcd);
+    }
+}
+
--- a/GameEngine/GameEngine.h	Fri May 01 20:37:10 2020 +0000
+++ b/GameEngine/GameEngine.h	Sun May 03 18:15:58 2020 +0000
@@ -8,6 +8,7 @@
 #include "Spaceship.h"
 #include "Map.h"
 #include "Weapons.h"
+#include "Alien.h"
 #include <vector>
 
  
@@ -38,16 +39,21 @@
         
         /** Gets joystick direction from gamepad and stores it in d_*/
         void read_joystick_direction();
-        // Variables -----------------------------------------------------------
         
-        // Changes the drawing x postion of map, 1 moves right, -1 moves left and 0 doesnt change map position 
-        int move_map_;
+        /** Creats bullet object if button A is pressed*/
+        void create_bullet();
+       
+       /** Draws each bullet object*/
+        void draw_bullets();
         
-        bool is_bullet_firing_;
+        // Variables -----------------------------------------------------------
         
         // Direction of joystick
         Direction d_; 
         
+        // Vector to store each new bullet object
+        std::vector<Weapons> bullet_vector;
+        
         // Objects -------------------------------------------------------------
     
         // Gamepad object 
@@ -60,12 +66,11 @@
         Spaceship spaceship;
         
         // Map object 
-        Map map;  
+        Map map; 
         
-        // Weapons object
-        //Weapons weapons;
-        
-        std::vector<Weapons> bullet_vector;
+        // Alien object 
+        Alien alien; 
+    
 };
  
 #endif
\ No newline at end of file
--- a/Weapons/Weapons.cpp	Fri May 01 20:37:10 2020 +0000
+++ b/Weapons/Weapons.cpp	Sun May 03 18:15:58 2020 +0000
@@ -8,7 +8,9 @@
     
 }
  
-void Weapons::init() {
+void Weapons::init(Vector2D spaceship_pos, bool spaceship_sprite_direction) {
+    calc_bullets_start_pos(spaceship_pos, spaceship_sprite_direction);
+    set_direction(spaceship_sprite_direction);
 }
 
 void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_){
@@ -28,7 +30,11 @@
 
 void Weapons::draw_bullet(N5110 &lcd){
     lcd.drawLine(position_x_bullet_, position_y_bullet_, position_x_bullet_+1, position_y_bullet_, 1);
-    position_x_bullet_ += 3;
+    if(bullet_direction_){
+        position_x_bullet_ += 3;
+    }else{
+        position_x_bullet_ -= 3;
+    }
 }
 
 Vector2D Weapons::get_pos_one(){
@@ -39,4 +45,8 @@
 Vector2D Weapons::get_pos_two(){
     Vector2D pos = {position_x_bullet_+1,position_y_bullet_};
     return pos;
+}
+
+bool Weapons::set_direction(bool spaceship_sprite_direction){
+    bullet_direction_ = spaceship_sprite_direction;   
 }
\ No newline at end of file
--- a/Weapons/Weapons.h	Fri May 01 20:37:10 2020 +0000
+++ b/Weapons/Weapons.h	Sun May 03 18:15:58 2020 +0000
@@ -21,13 +21,7 @@
         ~Weapons();
         
         /** Initalises Weapons */
-        void init();
-    
-        /** Calculates bullets start postion
-         * @param spaceship_pos @details x and y postion of spaceship
-         * @param spaceship_sprite_direction_ @details sprite direction bool, true = E, false = W
-         */
-        void calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_);
+        void init(Vector2D spaceship_pos, bool spaceship_sprite_direction);
         
         /** Draws the bullet and moves it in x direction each frame
          * @param lcd @details : N5110 object
@@ -45,17 +39,32 @@
          * @returns position_x_bullet_ + 1, position_x_bullet_
          */
         Vector2D get_pos_two();
+        
+        
     private:
     // Function prototypes -----------------------------------------------------
         
+        /** Sets the bullets movment direction when it is fired
+         * @param spaceship_sprite_direction_ @details sprite direction bool, true = E, false = W
+         */
+        bool set_direction(bool spaceship_sprite_direction_);
         
+        /** Calculates bullets start postion
+         * @param spaceship_pos @details x and y postion of spaceship
+         * @param spaceship_sprite_direction_ @details sprite direction bool, true = E, false = W
+         */
+        void calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_);
+   
     // Variables ---------------------------------------------------------------
          
-         // bullets x position on lcd
+        // Bullets x position on lcd
         int position_x_bullet_;
         
-        // bullets y position on lcd
+        // Bullets y position on lcd
         int position_y_bullet_;
+        
+        // Bullet movement direction
+        bool bullet_direction_;
     
 };