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.

Revision:
9:1ddb8dc93e48
Parent:
8:dd1037c5435b
Child:
11:ab578a151f67
--- a/Map/Map.cpp	Sun Apr 19 17:46:57 2020 +0000
+++ b/Map/Map.cpp	Tue Apr 21 23:13:19 2020 +0000
@@ -1,6 +1,6 @@
 #include "Map.h"
-#define MAP_TERRIAN_Y_POSITION 42
-
+#define MAP_TERRAIN_Y_POSITION 42
+Serial usb(USBTX, USBRX); 
 Map::Map() {
     
 }
@@ -10,56 +10,109 @@
 }
 
 void Map::init(AnalogIn &adc) {
-    position_x_map_ = -96;
-    position_y_map_ = MAP_TERRIAN_Y_POSITION;
-    get_random_hight_array(adc);
+    position_x_map_ = -84;
+    position_y_map_ = MAP_TERRAIN_Y_POSITION;
+    get_random_height_array(adc);
     get_random_length_array(adc);
     //usb.printf("randome seed = %f\n", rand_seed_);
+    map_length_ = 0;
 }
 
-void Map::get_random_hight_array(AnalogIn &adc){
+void Map::get_random_height_array(AnalogIn &adc){
     srand(adc.read()*64000);
-    for(int i = 0; i < 12; i++){
-        rand_hights_[i]= rand() % 8 + 5;
+    for(int i = 0; i < 11; i++){
+        rand_heights_[i]= rand() % 8 + 5;
     }   
-    //printf loop to check correct random numbers are genreated
+    //printf loop to check correct random numbers are generated
     //for (int i = 0; i < 12; i++){ 
-       //usb.printf("map hight random array = %d\n", rand_hights_[i]);
+       //usb.printf("map height random array = %d\n", rand_heights_[i]);
     //}
 }
 
 void Map::get_random_length_array(AnalogIn &adc){
     srand(adc.read()*64000);
-    for(int i = 0; i < 12; i++){
+    for(int i = 0; i < 11; i++){
         rand_lengths_[i]= rand() % 16+ 15;
     }   
 }
 
-void Map::draw_triangle(N5110 &lcd,int triangle_hight){ 
+void Map::draw_triangle(N5110 &lcd,int triangle_height){ 
     // draws triangle by drawing two lines with one line having negative gadient 
-    lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,1); 
-    lcd.drawLine(position_x_map_ + triangle_hight, position_y_map_ - triangle_hight,position_x_map_ + 2*triangle_hight,position_y_map_,1);
-    position_x_map_ = position_x_map_ + 2*triangle_hight,position_y_map_;
+    lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_height, position_y_map_ - triangle_height,1); 
+    lcd.drawLine(position_x_map_ + triangle_height, position_y_map_ - triangle_height,position_x_map_ + 2*triangle_height,position_y_map_,1);
+    position_x_map_ = position_x_map_ + 2*triangle_height,position_y_map_;
 }
 
 void Map::draw_line(N5110 &lcd,int line_length){ 
-    // draws triangle by drawing two lines with one line having negative gadient 
     lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1);
-    position_x_map_ = position_x_map_ + line_length;
+    position_x_map_ += line_length;
 }
 
 void Map::draw_map(N5110 &lcd, int move_map){
     //usb.printf("position_x_map_ = %d\n", position_x_map_);
     //usb.printf("move map = %d\n", move_map);
-    int reset_position_x_map_to_origonal = position_x_map_;
-    for(int i = 0; i < 12; i++){
-        draw_triangle(lcd,rand_hights_[i]);
-        draw_line(lcd,rand_lengths_[i]);   
+
+    reset_position_x_map_to_origonal_ = position_x_map_;
+    int map_length_ = 0;
+    
+    //prints main part of map
+    for(int i = 0; i < 11; i++){
+        draw_triangle(lcd,rand_heights_[i]);
+        draw_line(lcd,rand_lengths_[i]);
+        final_random_element_used_ = i;
+        map_length_ += rand_lengths_[i] + 2*rand_heights_[i];
+        if (map_length_ >252){ // stops random maps lengths being to large only want it about 3 screen widths
+            break;
+        }
+    }
+    
+    //checks is map need duplicating on forward and backwards loop and fills gap 
+    check_duplicates_map_forward(lcd, move_map);
+    check_duplicates_map_backwards(lcd, move_map);
+    
+    position_x_map_ = reset_position_x_map_to_origonal_ + move_map;
+    
+    // Moves map to different persiton so make it look like its looping 
+    if(position_x_map_+ map_length_ < 0){
+       position_x_map_ = 0;
     } 
-    position_x_map_ = reset_position_x_map_to_origonal + move_map;
+     if(position_x_map_ > 84){
+        position_x_map_ = 84 - map_length_;
+    }
+}
+
+void Map::check_duplicates_map_forward(N5110 &lcd, int move_map){
+    // Prints 1st part of map to fill gap wear map isn't present just befor its about to loop round it's self
+    if(reset_position_x_map_to_origonal_ + map_length_ <84 ){
+        for(int i = 0; i < 4; i++){
+            draw_triangle(lcd,rand_heights_[i]);
+            draw_line(lcd,rand_lengths_[i]);
+        }
+    }
+    
+}
+
+void Map::check_duplicates_map_backwards(N5110 &lcd, int move_map){
+    // Prints last part of map to fill gap wear map isn't present just befor its about to loop round it's self
+    if(reset_position_x_map_to_origonal_ > 0 ){
+        int print_reverse_position = 0;
+        // prints last 4 parts of map to fill gap
+        for(int i = final_random_element_used_ ; i > final_random_element_used_ - 4; i--){
+            position_x_map_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] - print_reverse_position;
+            draw_line(lcd,rand_lengths_[i]);
+            print_reverse_position += rand_lengths_[i];
+            position_x_map_ = reset_position_x_map_to_origonal_ - 2*rand_heights_[i] - print_reverse_position;
+            draw_triangle(lcd,rand_heights_[i]);
+            print_reverse_position += 2*rand_heights_[i];
+        }
+    }
 }
 
 int Map::get_position_x_map(){
     return position_x_map_;
 }
 
+
+
+
+