Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
evanso
Date:
Thu Apr 16 16:25:53 2020 +0000
Parent:
5:acd809cc824c
Child:
7:0af4ced868f5
Commit message:
Restricted spaceship movement to middle third of the screen in the x-direction. Started adding drawing functions to map class.

Changed in this revision

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
Spaceship/Spaceship.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Map/Map.cpp	Thu Apr 16 16:25:53 2020 +0000
@@ -0,0 +1,20 @@
+#include "Map.h"
+
+Map::Map() {
+    
+}
+ 
+Map::~Map() {
+    
+}
+
+void Map::init() {
+    position_x_map_ = 0;
+    position_y_map_ = 48;
+}
+ 
+
+void Map::draw_triangle(N5110 &lcd){   
+   lcd.drawLine(0,48,5,43,1); 
+   lcd.drawLine(5,43,10,48,1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Map/Map.h	Thu Apr 16 16:25:53 2020 +0000
@@ -0,0 +1,37 @@
+#ifndef MAP_H
+#define MAP_H
+ 
+// Include libraries
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+ 
+/** Map class
+@brief Draws and moves map
+@author Benjamin Evans, University of Leeds
+@date April 2020
+*/
+
+class Map {
+    public:
+        /** Constructor */
+        Map();
+        
+        /** Destructor */
+        ~Map();
+        
+        /** Initalises Spaceship */
+        void init();
+        
+        /** Draws triangle to represent a mountain 
+         * @param LCD object
+         */
+        void draw_triangle(N5110 &lcd);
+      
+    private:
+        int position_x_map_;
+        int position_y_map_;
+};
+ 
+#endif
\ No newline at end of file
--- a/Spaceship/Spaceship.cpp	Wed Apr 15 11:11:00 2020 +0000
+++ b/Spaceship/Spaceship.cpp	Thu Apr 16 16:25:53 2020 +0000
@@ -31,13 +31,14 @@
 }
  
 void Spaceship::draw(N5110 &lcd) {
+    off_screen_x_y_checker();
     // Draws spaceships at defined x and y positions with different sprite direction depending on joystick postion 
     if (spaceship_sprite_direction_){
         lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_E);
     }else if (!spaceship_sprite_direction_){
     lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_W);
     }
-    // printf to find position of spaceship at bottom of screen  
+    // printf to find position of spaceship for making the off screen checkers
     //usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_);
 }
 
@@ -46,6 +47,22 @@
     lcd.drawRect(position_x_spaceship_, position_y_spaceship_, 13, 4, FILL_WHITE);
 }
 
+void Spaceship::off_screen_x_y_checker(){
+    // checks y postion and alters y position 
+    if (position_y_spaceship_ < 1) {
+        position_y_spaceship_ = 1;
+    }
+    if (position_y_spaceship_ > 44) {
+        position_y_spaceship_ = 44;
+    }  
+    
+    if (position_x_spaceship_ > 52) {
+        position_x_spaceship_ = 52;
+    }
+    if (position_x_spaceship_ < 22) {
+        position_x_spaceship_ = 22;
+    }
+}    
 // NEED TO REDUCE FUNTION LENGTH ONCE WORKING 
 void Spaceship::movement(Gamepad &pad){
     Direction joystick_direction = pad.get_direction();
@@ -53,37 +70,29 @@
     if(joystick_direction == N){
         position_y_spaceship_+= -1;
     }else if(joystick_direction == NE){
-        position_x_spaceship_+= 2; 
+        position_x_spaceship_+= 1; 
         position_y_spaceship_+= -1; 
         spaceship_sprite_direction_ = true;
     }else if(joystick_direction == E){
         position_x_spaceship_+= 2;
         spaceship_sprite_direction_ = true; 
     }else if(joystick_direction == SE){
-        position_x_spaceship_+= 2;
+        position_x_spaceship_+= 1;
         position_y_spaceship_+= 1;   
         spaceship_sprite_direction_ = true;  
     }else if(joystick_direction == S){
         position_y_spaceship_+= 1;
     }else if(joystick_direction == SW){
         position_y_spaceship_+= 1;
-        position_x_spaceship_-= 2;
+        position_x_spaceship_-= 1;
         spaceship_sprite_direction_ = false; 
     }else if(joystick_direction == W){
         position_x_spaceship_-= 2;
         spaceship_sprite_direction_ = false; 
     }else if(joystick_direction == NW){
-        position_x_spaceship_-= 2;
+        position_x_spaceship_-= 1;
         position_y_spaceship_+= -1;
         spaceship_sprite_direction_ = false; 
-    }
-    
-    // Checks y position of spaceship and stops it from going of screen 
-    if (position_y_spaceship_ < 1) {
-        position_y_spaceship_ = 1;
-    }
-    if (position_y_spaceship_ > 44) {
-        position_y_spaceship_ = 44;
-    }  
+    } 
 }
 
--- a/Spaceship/Spaceship.h	Wed Apr 15 11:11:00 2020 +0000
+++ b/Spaceship/Spaceship.h	Thu Apr 16 16:25:53 2020 +0000
@@ -7,7 +7,7 @@
 #include "Gamepad.h"
  
 /** Spaceship class
-@brief Draws spaceship
+@brief Draws and moves spaceship
 @author Benjamin Evans, University of Leeds
 @date April 2020
 */
@@ -36,12 +36,19 @@
         /** Move Spaceship either up,down,left,right
          * @param Gampad object
          */
-         void movement(Gamepad &pad); 
+        void movement(Gamepad &pad);   
+         
+        /** Checks sapceship x and y position and stops spacship comming of the screen in y direction
+         * holds spaceship in middle 3rd of screen 
+         * @param position_y_spaceship_
+         */
+        void off_screen_x_y_checker();
         
     private:
         int position_x_spaceship_;
         int position_y_spaceship_;
         bool spaceship_sprite_direction_;
+      
 
 };
  
--- a/main.cpp	Wed Apr 15 11:11:00 2020 +0000
+++ b/main.cpp	Thu Apr 16 16:25:53 2020 +0000
@@ -15,6 +15,7 @@
 #include "Gamepad.h"
 #include "N5110.h"
 #include "Spaceship.h"
+#include "Map.h"
 
 
 // Objects
@@ -23,6 +24,7 @@
 Ticker ticker;
 Spaceship spaceship;
 AnalogIn pot_1(PTB2);
+Map map;
 
 // Global varable definitions 
 volatile int g_lcd_frame_time_flag = 0;   
@@ -38,6 +40,7 @@
     pad.init();
     lcd.init();
     spaceship.init();
+    map.init();
     lcd.setContrast(0.45);
     lcd.refresh(); 
     spaceship.draw(lcd);
@@ -50,7 +53,8 @@
             lcd.setContrast(pot_1.read());
             spaceship.clear_spaceship(lcd);
             spaceship.movement(pad);
-            spaceship.draw(lcd); 
+            spaceship.draw(lcd);
+            map.draw_triangle(lcd); 
             lcd.refresh();     
         }
         // MCU put to sleep between each frame to save power