Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Map/Map.cpp@11:ab578a151f67, 2020-04-23 (annotated)
- Committer:
- evanso
- Date:
- Thu Apr 23 18:17:28 2020 +0000
- Revision:
- 11:ab578a151f67
- Parent:
- 9:1ddb8dc93e48
- Child:
- 13:12276eed13ac
Added test files and ran a spaceship unit test, which it passed! Also cleaned up code and comments.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| evanso | 6:12e8433382b3 | 1 | #include "Map.h" |
| evanso | 11:ab578a151f67 | 2 | |
| evanso | 9:1ddb8dc93e48 | 3 | #define MAP_TERRAIN_Y_POSITION 42 |
| evanso | 11:ab578a151f67 | 4 | #define MAP_TERRAIN_X_POSITION -84 |
| evanso | 11:ab578a151f67 | 5 | #define MAP_TERRAIN_X_LENGTH 0 |
| evanso | 11:ab578a151f67 | 6 | |
| evanso | 6:12e8433382b3 | 7 | Map::Map() { |
| evanso | 6:12e8433382b3 | 8 | |
| evanso | 6:12e8433382b3 | 9 | } |
| evanso | 6:12e8433382b3 | 10 | |
| evanso | 6:12e8433382b3 | 11 | Map::~Map() { |
| evanso | 6:12e8433382b3 | 12 | |
| evanso | 6:12e8433382b3 | 13 | } |
| evanso | 6:12e8433382b3 | 14 | |
| evanso | 8:dd1037c5435b | 15 | void Map::init(AnalogIn &adc) { |
| evanso | 11:ab578a151f67 | 16 | map_length_ = MAP_TERRAIN_X_LENGTH; |
| evanso | 11:ab578a151f67 | 17 | position_x_map_ = MAP_TERRAIN_X_POSITION; |
| evanso | 9:1ddb8dc93e48 | 18 | position_y_map_ = MAP_TERRAIN_Y_POSITION; |
| evanso | 11:ab578a151f67 | 19 | |
| evanso | 11:ab578a151f67 | 20 | // Initialises random arrays to make random map |
| evanso | 11:ab578a151f67 | 21 | get_random_arrays(adc); |
| evanso | 6:12e8433382b3 | 22 | } |
| evanso | 7:0af4ced868f5 | 23 | |
| evanso | 11:ab578a151f67 | 24 | void Map::get_random_arrays(AnalogIn &adc){ |
| evanso | 7:0af4ced868f5 | 25 | srand(adc.read()*64000); |
| evanso | 9:1ddb8dc93e48 | 26 | for(int i = 0; i < 11; i++){ |
| evanso | 9:1ddb8dc93e48 | 27 | rand_heights_[i]= rand() % 8 + 5; |
| evanso | 11:ab578a151f67 | 28 | rand_lengths_[i]= rand() % 16 + 15; |
| evanso | 7:0af4ced868f5 | 29 | } |
| evanso | 9:1ddb8dc93e48 | 30 | //printf loop to check correct random numbers are generated |
| evanso | 11:ab578a151f67 | 31 | //for (int i = 0; i < 11; i++){ |
| evanso | 9:1ddb8dc93e48 | 32 | //usb.printf("map height random array = %d\n", rand_heights_[i]); |
| evanso | 7:0af4ced868f5 | 33 | //} |
| evanso | 7:0af4ced868f5 | 34 | } |
| evanso | 7:0af4ced868f5 | 35 | |
| evanso | 9:1ddb8dc93e48 | 36 | void Map::draw_triangle(N5110 &lcd,int triangle_height){ |
| evanso | 7:0af4ced868f5 | 37 | // draws triangle by drawing two lines with one line having negative gadient |
| evanso | 9:1ddb8dc93e48 | 38 | lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + triangle_height, position_y_map_ - triangle_height,1); |
| evanso | 9:1ddb8dc93e48 | 39 | lcd.drawLine(position_x_map_ + triangle_height, position_y_map_ - triangle_height,position_x_map_ + 2*triangle_height,position_y_map_,1); |
| evanso | 11:ab578a151f67 | 40 | |
| evanso | 11:ab578a151f67 | 41 | // changes the position of the map next draw line starts at the end of the drawn triangle |
| evanso | 9:1ddb8dc93e48 | 42 | position_x_map_ = position_x_map_ + 2*triangle_height,position_y_map_; |
| evanso | 7:0af4ced868f5 | 43 | } |
| evanso | 7:0af4ced868f5 | 44 | |
| evanso | 7:0af4ced868f5 | 45 | void Map::draw_line(N5110 &lcd,int line_length){ |
| evanso | 7:0af4ced868f5 | 46 | lcd.drawLine(position_x_map_, position_y_map_, position_x_map_ + line_length, position_y_map_,1); |
| evanso | 11:ab578a151f67 | 47 | |
| evanso | 11:ab578a151f67 | 48 | // changes the position of the map next draw triangle starts at the end of the drawn line |
| evanso | 9:1ddb8dc93e48 | 49 | position_x_map_ += line_length; |
| evanso | 7:0af4ced868f5 | 50 | } |
| evanso | 7:0af4ced868f5 | 51 | |
| evanso | 7:0af4ced868f5 | 52 | void Map::draw_map(N5110 &lcd, int move_map){ |
| evanso | 8:dd1037c5435b | 53 | //usb.printf("position_x_map_ = %d\n", position_x_map_); |
| evanso | 8:dd1037c5435b | 54 | //usb.printf("move map = %d\n", move_map); |
| evanso | 9:1ddb8dc93e48 | 55 | |
| evanso | 9:1ddb8dc93e48 | 56 | reset_position_x_map_to_origonal_ = position_x_map_; |
| evanso | 9:1ddb8dc93e48 | 57 | int map_length_ = 0; |
| evanso | 9:1ddb8dc93e48 | 58 | |
| evanso | 9:1ddb8dc93e48 | 59 | //prints main part of map |
| evanso | 9:1ddb8dc93e48 | 60 | for(int i = 0; i < 11; i++){ |
| evanso | 9:1ddb8dc93e48 | 61 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 62 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 63 | final_random_element_used_ = i; |
| evanso | 11:ab578a151f67 | 64 | |
| evanso | 11:ab578a151f67 | 65 | // calculates the length of the random map produced |
| evanso | 9:1ddb8dc93e48 | 66 | map_length_ += rand_lengths_[i] + 2*rand_heights_[i]; |
| evanso | 11:ab578a151f67 | 67 | |
| evanso | 11:ab578a151f67 | 68 | // stops random maps lengths being to large only want it about 3 screen widths |
| evanso | 11:ab578a151f67 | 69 | if (map_length_ >252){ |
| evanso | 9:1ddb8dc93e48 | 70 | break; |
| evanso | 9:1ddb8dc93e48 | 71 | } |
| evanso | 9:1ddb8dc93e48 | 72 | } |
| evanso | 9:1ddb8dc93e48 | 73 | |
| evanso | 9:1ddb8dc93e48 | 74 | //checks is map need duplicating on forward and backwards loop and fills gap |
| evanso | 11:ab578a151f67 | 75 | check_duplicates_map_forward(lcd); |
| evanso | 11:ab578a151f67 | 76 | check_duplicates_map_backwards(lcd); |
| evanso | 9:1ddb8dc93e48 | 77 | |
| evanso | 11:ab578a151f67 | 78 | // Resets postion of map and moves it |
| evanso | 9:1ddb8dc93e48 | 79 | position_x_map_ = reset_position_x_map_to_origonal_ + move_map; |
| evanso | 9:1ddb8dc93e48 | 80 | |
| evanso | 11:ab578a151f67 | 81 | // Moves map to different persition so make it look like its looping |
| evanso | 9:1ddb8dc93e48 | 82 | if(position_x_map_+ map_length_ < 0){ |
| evanso | 9:1ddb8dc93e48 | 83 | position_x_map_ = 0; |
| evanso | 11:ab578a151f67 | 84 | } else if(position_x_map_ > 84){ |
| evanso | 9:1ddb8dc93e48 | 85 | position_x_map_ = 84 - map_length_; |
| evanso | 9:1ddb8dc93e48 | 86 | } |
| evanso | 9:1ddb8dc93e48 | 87 | } |
| evanso | 9:1ddb8dc93e48 | 88 | |
| evanso | 11:ab578a151f67 | 89 | void Map::check_duplicates_map_forward(N5110 &lcd){ |
| evanso | 9:1ddb8dc93e48 | 90 | // Prints 1st part of map to fill gap wear map isn't present just befor its about to loop round it's self |
| evanso | 9:1ddb8dc93e48 | 91 | if(reset_position_x_map_to_origonal_ + map_length_ <84 ){ |
| evanso | 9:1ddb8dc93e48 | 92 | for(int i = 0; i < 4; i++){ |
| evanso | 9:1ddb8dc93e48 | 93 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 94 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 95 | } |
| evanso | 9:1ddb8dc93e48 | 96 | } |
| evanso | 9:1ddb8dc93e48 | 97 | |
| evanso | 9:1ddb8dc93e48 | 98 | } |
| evanso | 9:1ddb8dc93e48 | 99 | |
| evanso | 11:ab578a151f67 | 100 | void Map::check_duplicates_map_backwards(N5110 &lcd){ |
| evanso | 9:1ddb8dc93e48 | 101 | // Prints last part of map to fill gap wear map isn't present just befor its about to loop round it's self |
| evanso | 9:1ddb8dc93e48 | 102 | if(reset_position_x_map_to_origonal_ > 0 ){ |
| evanso | 9:1ddb8dc93e48 | 103 | int print_reverse_position = 0; |
| evanso | 11:ab578a151f67 | 104 | |
| evanso | 11:ab578a151f67 | 105 | // prints the last 4 parts of map to fill gap |
| evanso | 9:1ddb8dc93e48 | 106 | for(int i = final_random_element_used_ ; i > final_random_element_used_ - 4; i--){ |
| evanso | 9:1ddb8dc93e48 | 107 | position_x_map_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] - print_reverse_position; |
| evanso | 9:1ddb8dc93e48 | 108 | draw_line(lcd,rand_lengths_[i]); |
| evanso | 9:1ddb8dc93e48 | 109 | print_reverse_position += rand_lengths_[i]; |
| evanso | 9:1ddb8dc93e48 | 110 | position_x_map_ = reset_position_x_map_to_origonal_ - 2*rand_heights_[i] - print_reverse_position; |
| evanso | 9:1ddb8dc93e48 | 111 | draw_triangle(lcd,rand_heights_[i]); |
| evanso | 9:1ddb8dc93e48 | 112 | print_reverse_position += 2*rand_heights_[i]; |
| evanso | 9:1ddb8dc93e48 | 113 | } |
| evanso | 9:1ddb8dc93e48 | 114 | } |
| evanso | 7:0af4ced868f5 | 115 | } |
| evanso | 7:0af4ced868f5 | 116 | |
| evanso | 7:0af4ced868f5 | 117 | int Map::get_position_x_map(){ |
| evanso | 7:0af4ced868f5 | 118 | return position_x_map_; |
| evanso | 7:0af4ced868f5 | 119 | } |
| evanso | 7:0af4ced868f5 | 120 | |
| evanso | 9:1ddb8dc93e48 | 121 | |
| evanso | 9:1ddb8dc93e48 | 122 | |
| evanso | 9:1ddb8dc93e48 | 123 | |
| evanso | 9:1ddb8dc93e48 | 124 |