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 Apr 29 12:35:14 2020 +0000
Parent:
16:1ee3d3804557
Child:
18:11068b98e261
Commit message:
Changed spaceship class to return x and y position as a Vector2D. Added calculate bullet start position function to weapons class.

Changed in this revision

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
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
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GameEngine/GameEngine.cpp	Tue Apr 28 23:31:22 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Wed Apr 29 12:35:14 2020 +0000
@@ -14,7 +14,6 @@
     spaceship.init();
     map.init(pad);
     move_map_= 0;
-
 }
 
 void GameEngine::gameplay_loop() {
@@ -25,45 +24,45 @@
     // Gets movements  
     read_joystick_direction();
     spaceship.movement(d_);
-    calculate_map_movement();
+    weapons.calc_bullets_start_pos(spaceship.get_pos(),spaceship.get_spaceship_sprite_direction());
     
     // Redraws 
     spaceship.draw(lcd);
-    map.draw_map(lcd, move_map_);
-    
+    map.draw_map(lcd, calc_map_movement());
+
     //refresh's screen
     lcd.refresh(); 
     
-    
-    //draws map again so when moveing mountains are vitable on screen
+    //draws map again and refreshes screen so map moves faster than spaceship but doesnt require double pixel moement 
     map.draw_map(lcd, move_map_);
     lcd.refresh();
 }
 
-void GameEngine::calculate_map_movement(){ 
+int GameEngine::calc_map_movement(){ 
     // Gets postition of spaceship and sprite direction      
-    int position_x_spaceship_= spaceship.get_position_x_spaceship();      
-    
-     
+    Vector2D spaceship_pos= spaceship.get_pos();      
+
     // moves the map 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){
         move_map_ = 1;
         
         // moves map fater when changing direction to offset ship 
-        if (position_x_spaceship_ != 57){
-            move_map_ = 1;
+        if (spaceship_pos.x != 57){
+           move_map_ = 1;
         }
     }else if (d_ == E || d_ == NE || d_ == SE){ 
         move_map_ = -1; 
         
         // moves map fater when changing direction to offset ship
-        if (position_x_spaceship_ != 14){
-            move_map_ = -1;
+        if (spaceship_pos.x != 14){
+           move_map_ = -1;
         } 
     }else {
-        move_map_ = 0;
+         move_map_ = 0;
     }
     
+    return move_map_;
+    
     // Debug and check variables when function is called 
     #ifdef CALCULATE_MAP_MOVEMENT_DEBUG   
         printf("move map = %d\n", move_map_);   
--- a/GameEngine/GameEngine.h	Tue Apr 28 23:31:22 2020 +0000
+++ b/GameEngine/GameEngine.h	Wed Apr 29 12:35:14 2020 +0000
@@ -7,6 +7,7 @@
 #include "Gamepad.h"
 #include "Spaceship.h"
 #include "Map.h"
+#include "Weapons.h"
 
  
 /** GameEngine class
@@ -32,23 +33,21 @@
         /** Gets joystick direction from gamepad and stores it in d_*/
         void read_joystick_direction();
         
-        
         // Accessors and mutators ----------------------------------------------
           
     private:
         // Function prototypes -------------------------------------------------
     
-        /** Calulates the map movement depeding on spaceship positions and joystick input */
-        void calculate_map_movement();
+        /** Calulates the map movement depeding on spaceship positions and joystick input 
+         * @retrun move_map_ @details move map variable for map draw function 
+         */
+        int calc_map_movement();
     
         // Variables -----------------------------------------------------------
         
         // Changes the drawing x postion of map, 1 moves right, -1 moves left and 0 doesnt change map position 
         int move_map_;
         
-        // // Changes the drawing x postion of map, 1 moves right, -1 moves left and 0 doesnt change map position 
-        int offset_map_;
-        
         // Direction of joystick
         Direction d_; 
     
@@ -65,6 +64,9 @@
         
         // Map object 
         Map map;  
+        
+        // Weapons object
+        Weapons weapons;
 };
  
 #endif
\ No newline at end of file
--- a/Spaceship/Spaceship.cpp	Tue Apr 28 23:31:22 2020 +0000
+++ b/Spaceship/Spaceship.cpp	Wed Apr 29 12:35:14 2020 +0000
@@ -85,13 +85,14 @@
     }
 }
 
-
-int Spaceship::get_position_x_spaceship(){
-    return position_x_spaceship_;
+Vector2D Spaceship::get_pos(){
+    Vector2D pos = {position_x_spaceship_,position_y_spaceship_};
+    return pos;
 }
 
-int Spaceship::get_position_y_spaceship(){
-    return position_y_spaceship_;
+bool Spaceship::get_spaceship_sprite_direction(){
+    return spaceship_sprite_direction_;
 }
 
 
+
--- a/Spaceship/Spaceship.h	Tue Apr 28 23:31:22 2020 +0000
+++ b/Spaceship/Spaceship.h	Wed Apr 29 12:35:14 2020 +0000
@@ -34,16 +34,6 @@
         void movement(Direction d_);   
         
         // Accessors and mutators -----------------------------------------------
-    
-        /** Gets the x postion of spaceship 
-         * @return position_x_spaceship_
-         */
-        int get_position_x_spaceship();
-        
-        /** Gets the y postion of spaceship 
-         * @return position_y_spaceship_ 
-         */
-        int get_position_y_spaceship();
         
         /** Gets sprtie directon if spaceship 
          * @details true = east, false = west
@@ -51,6 +41,11 @@
          */
         bool get_spaceship_sprite_direction(); 
         
+        /** Gets the xy position of the spaceship 
+         * @returns position_x_spaceship_, position_x_spaceship_
+         */
+        Vector2D get_pos();
+        
     private:
     // Function prototypes -----------------------------------------------------
     
--- a/Weapons/Weapons.cpp	Tue Apr 28 23:31:22 2020 +0000
+++ b/Weapons/Weapons.cpp	Wed Apr 29 12:35:14 2020 +0000
@@ -11,6 +11,22 @@
 void Weapons::init() {
 }
 
-void Weapons::draw_laser_bullets(N5110 &lcd, int position_x_spaceship_, int position_y_spaceship_, bool spaceship_sprite_direction_){
-   
+void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_){
+    // Sets start position of bullet to spaceships nose
+    if (spaceship_sprite_direction_){
+        position_x_bullet_ = spaceship_pos.x + 13;
+        position_y_bullet_ = spaceship_pos.y + 3;
+    }else{
+        position_x_bullet_ = spaceship_pos.x;
+        position_y_bullet_ = spaceship_pos.y + 3;
+    }
+    #ifdef POSITION_BULLET_DEBUG   
+        printf("X = %d\n", position_x_bullet_);       
+        printf("Y = %d\n", position_y_bullet_);
+    #endif 
+}
+
+Vector2D Weapons::get_pos(){
+    Vector2D pos = {position_x_bullet_,position_y_bullet_};
+    return pos;
 }
\ No newline at end of file
--- a/Weapons/Weapons.h	Tue Apr 28 23:31:22 2020 +0000
+++ b/Weapons/Weapons.h	Wed Apr 29 12:35:14 2020 +0000
@@ -1,5 +1,5 @@
-#ifndef SPACESHIP_H
-#define SPACESHIP_H
+#ifndef WEAPONS_H
+#define WEAPONS_H
  
 // Included libraries -----------------------------------------------------------
 #include "mbed.h"
@@ -23,23 +23,28 @@
         /** Initalises Weapons */
         void init();
         
-        /** Draws laser bullets
-         * @param lcd, paosition_x_spaceship_, position_y_spaceship_ @details : N5110 object, x and y spaceship positions
+        /** 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 draw_laser_bullets(N5110 &lcd, int position_x_spaceship_, int position_y_spaceship_, bool spaceship_sprite_direction_);
+        void calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_);
         
         // Accessors and mutators -----------------------------------------------
         
+        /** Gets the xy position of the bullet 
+         * @returns position_x_spaceship_, position_x_spaceship_
+         */
+        Vector2D get_pos();
     private:
     // Function prototypes -----------------------------------------------------
         
     // Variables ---------------------------------------------------------------
-    
-        // Spaceships x position on lcd
-        int position_x_bullets_;
+         
+         // bullets x position on lcd
+        int position_x_bullet_;
         
-        // Spaceships y position on lcd
-        int position_y_bullets_;
+        // bullets y position on lcd
+        int position_y_bullet_;
     
 };
  
--- a/main.cpp	Tue Apr 28 23:31:22 2020 +0000
+++ b/main.cpp	Wed Apr 29 12:35:14 2020 +0000
@@ -23,7 +23,8 @@
 #endif
 
 // TO DO
-// change test so they work with new map 
+// change comment so nulti line 
+// seperate details comments 
 
 // Objects ---------------------------------------------------------------------
 
@@ -45,7 +46,7 @@
 int main()
 { 
     // Sets fram rate of lcd by using time-triggered tasks
-    ticker.attach(&lcd_frame_time_isr,0.03);
+    ticker.attach(&lcd_frame_time_isr,0.04);
     engine.init();
     
     // Compile with tests