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.

Committer:
evanso
Date:
Wed May 27 02:06:05 2020 +0000
Revision:
87:832ca78426b5
Parent:
85:87bc28b151d8
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 6:12e8433382b3 1 #include "Map.h"
evanso 11:ab578a151f67 2
evanso 85:87bc28b151d8 3 // Constant Definitions
evanso 33:7fedd8029473 4 #define MAP_TERRAIN_Y_POSITION 41
evanso 11:ab578a151f67 5 #define MAP_TERRAIN_X_POSITION -84
evanso 11:ab578a151f67 6 #define MAP_TERRAIN_X_LENGTH 0
evanso 11:ab578a151f67 7
evanso 6:12e8433382b3 8 Map::Map() {
evanso 6:12e8433382b3 9
evanso 6:12e8433382b3 10 }
evanso 6:12e8433382b3 11
evanso 6:12e8433382b3 12 Map::~Map() {
evanso 6:12e8433382b3 13
evanso 6:12e8433382b3 14 }
evanso 6:12e8433382b3 15
evanso 13:12276eed13ac 16 void Map::init(Gamepad &pad) {
evanso 36:27aa597db3d2 17 // Assign values to variables
evanso 11:ab578a151f67 18 map_length_ = MAP_TERRAIN_X_LENGTH;
evanso 27:8bb2bd97c319 19 position_x_ = MAP_TERRAIN_X_POSITION;
evanso 27:8bb2bd97c319 20 position_y_ = MAP_TERRAIN_Y_POSITION;
evanso 11:ab578a151f67 21
evanso 11:ab578a151f67 22 // Initialises random arrays to make random map
evanso 14:7419c680656f 23 fill_random_arrays(pad);
evanso 24:479da6ca0e7e 24
evanso 6:12e8433382b3 25 }
evanso 7:0af4ced868f5 26
evanso 82:3211b31e9421 27 void Map::fill_random_arrays(Gamepad &pad) {
evanso 13:12276eed13ac 28 srand(pad.read_adc()*64000);
evanso 85:87bc28b151d8 29 for (int i = 0; i < 11; i++) {
evanso 9:1ddb8dc93e48 30 rand_heights_[i]= rand() % 8 + 5;
evanso 11:ab578a151f67 31 rand_lengths_[i]= rand() % 16 + 15;
evanso 7:0af4ced868f5 32 }
evanso 15:90b6821bcf64 33
evanso 85:87bc28b151d8 34 // To check random numbers generated are actually random
evanso 15:90b6821bcf64 35 #ifdef RANDOM_NUMBER_DEBUG
evanso 82:3211b31e9421 36 for (int i = 0; i < 11; i++) {
evanso 15:90b6821bcf64 37 printf("map height random array = %d\n", rand_heights_[i]);
evanso 15:90b6821bcf64 38 }
evanso 15:90b6821bcf64 39 #endif
evanso 7:0af4ced868f5 40 }
evanso 7:0af4ced868f5 41
evanso 82:3211b31e9421 42 void Map::draw_triangle(N5110 &lcd,int triangle_height) {
evanso 85:87bc28b151d8 43 // Draws triangle by drawing two lines with one line having negative gradient
evanso 27:8bb2bd97c319 44 lcd.drawLine(position_x_, position_y_, position_x_ + triangle_height,
evanso 27:8bb2bd97c319 45 position_y_ - triangle_height,1);
evanso 27:8bb2bd97c319 46 lcd.drawLine(position_x_ + triangle_height, position_y_ - triangle_height,
evanso 27:8bb2bd97c319 47 position_x_ + 2*triangle_height,position_y_,1);
evanso 11:ab578a151f67 48
evanso 82:3211b31e9421 49 // Changes the position of the map next draw line starts at the end of the
evanso 27:8bb2bd97c319 50 // drawn triangle
evanso 27:8bb2bd97c319 51 position_x_ = position_x_ + 2*triangle_height,position_y_;
evanso 7:0af4ced868f5 52 }
evanso 7:0af4ced868f5 53
evanso 82:3211b31e9421 54 void Map::draw_line(N5110 &lcd,int line_length) {
evanso 27:8bb2bd97c319 55 lcd.drawLine(position_x_, position_y_, position_x_ + line_length,
evanso 27:8bb2bd97c319 56 position_y_,1);
evanso 11:ab578a151f67 57
evanso 82:3211b31e9421 58 // Changes the position of the map next draw triangle starts at the end of
evanso 27:8bb2bd97c319 59 // the drawn line
evanso 27:8bb2bd97c319 60 position_x_ += line_length;
evanso 7:0af4ced868f5 61 }
evanso 7:0af4ced868f5 62
evanso 82:3211b31e9421 63 void Map::draw_map(N5110 &lcd, Direction d_) {
evanso 82:3211b31e9421 64 // printf("position_x_ map = %d\n", position_x_);
evanso 83:35d1e846eab2 65 // printf("move map = %d\n", move_map);
evanso 24:479da6ca0e7e 66
evanso 27:8bb2bd97c319 67 reset_position_x_map_to_origonal_ = position_x_;
evanso 24:479da6ca0e7e 68 map_length_ = 0;
evanso 9:1ddb8dc93e48 69
evanso 82:3211b31e9421 70 // Prints main part of map
evanso 85:87bc28b151d8 71 for (int i = 0; i < 11; i++) {
evanso 9:1ddb8dc93e48 72 draw_triangle(lcd,rand_heights_[i]);
evanso 9:1ddb8dc93e48 73 draw_line(lcd,rand_lengths_[i]);
evanso 9:1ddb8dc93e48 74 final_random_element_used_ = i;
evanso 11:ab578a151f67 75
evanso 82:3211b31e9421 76 // Calculates the length of the random map produced
evanso 9:1ddb8dc93e48 77 map_length_ += rand_lengths_[i] + 2*rand_heights_[i];
evanso 11:ab578a151f67 78
evanso 82:3211b31e9421 79 // Stops random maps lengths being to large only want it about 3 screen
evanso 27:8bb2bd97c319 80 // widths
evanso 82:3211b31e9421 81 if (map_length_ >252) {
evanso 9:1ddb8dc93e48 82 break;
evanso 9:1ddb8dc93e48 83 }
evanso 9:1ddb8dc93e48 84 }
evanso 9:1ddb8dc93e48 85
evanso 82:3211b31e9421 86 // Checks if the map need duplicating in forward and backwards loop and
evanso 82:3211b31e9421 87 // fills gap
evanso 11:ab578a151f67 88 check_duplicates_map_forward(lcd);
evanso 11:ab578a151f67 89 check_duplicates_map_backwards(lcd);
evanso 9:1ddb8dc93e48 90
evanso 85:87bc28b151d8 91 // Resets position of map and moves it
evanso 27:8bb2bd97c319 92 position_x_ = reset_position_x_map_to_origonal_ + calc_map_movement(d_);
evanso 9:1ddb8dc93e48 93
evanso 85:87bc28b151d8 94 // Moves map to different position so make it look like its looping
evanso 85:87bc28b151d8 95 if (position_x_+ map_length_ < 0) {
evanso 27:8bb2bd97c319 96 position_x_ = 0;
evanso 85:87bc28b151d8 97 } else if (position_x_ > 84) {
evanso 27:8bb2bd97c319 98 position_x_ = 84 - map_length_;
evanso 9:1ddb8dc93e48 99 }
evanso 9:1ddb8dc93e48 100 }
evanso 9:1ddb8dc93e48 101
evanso 82:3211b31e9421 102 void Map::check_duplicates_map_forward(N5110 &lcd) {
evanso 85:87bc28b151d8 103 // Prints 1st part of map to fill gap wear map isn't present just before its
evanso 27:8bb2bd97c319 104 // about to loop round it's self
evanso 85:87bc28b151d8 105 if (reset_position_x_map_to_origonal_ + map_length_ <84 ) {
evanso 85:87bc28b151d8 106 for (int i = 0; i < 4; i++) {
evanso 9:1ddb8dc93e48 107 draw_triangle(lcd,rand_heights_[i]);
evanso 9:1ddb8dc93e48 108 draw_line(lcd,rand_lengths_[i]);
evanso 9:1ddb8dc93e48 109 }
evanso 9:1ddb8dc93e48 110 }
evanso 9:1ddb8dc93e48 111
evanso 9:1ddb8dc93e48 112 }
evanso 9:1ddb8dc93e48 113
evanso 82:3211b31e9421 114 void Map::check_duplicates_map_backwards(N5110 &lcd) {
evanso 85:87bc28b151d8 115 // Prints last part of map to fill gap wear map isn't present just before its
evanso 27:8bb2bd97c319 116 // about to loop round it's self
evanso 85:87bc28b151d8 117 if (reset_position_x_map_to_origonal_ > 0 ) {
evanso 9:1ddb8dc93e48 118 int print_reverse_position = 0;
evanso 11:ab578a151f67 119
evanso 36:27aa597db3d2 120 // prints the last 4 parts of the map to fill gap
evanso 85:87bc28b151d8 121 for (int i = final_random_element_used_ ;
evanso 82:3211b31e9421 122 i > final_random_element_used_ - 4; i--) {
evanso 27:8bb2bd97c319 123 position_x_ = reset_position_x_map_to_origonal_ - rand_lengths_[i] -
evanso 27:8bb2bd97c319 124 print_reverse_position;
evanso 9:1ddb8dc93e48 125 draw_line(lcd,rand_lengths_[i]);
evanso 9:1ddb8dc93e48 126 print_reverse_position += rand_lengths_[i];
evanso 27:8bb2bd97c319 127 position_x_ = reset_position_x_map_to_origonal_ - 2*rand_heights_[i]
evanso 27:8bb2bd97c319 128 - print_reverse_position;
evanso 9:1ddb8dc93e48 129 draw_triangle(lcd,rand_heights_[i]);
evanso 9:1ddb8dc93e48 130 print_reverse_position += 2*rand_heights_[i];
evanso 9:1ddb8dc93e48 131 }
evanso 9:1ddb8dc93e48 132 }
evanso 7:0af4ced868f5 133 }
evanso 7:0af4ced868f5 134
evanso 82:3211b31e9421 135 int Map::calc_map_movement(Direction d_) {
evanso 85:87bc28b151d8 136 // Moves the map in opposite direction to spaceship when it's position is at
evanso 27:8bb2bd97c319 137 // min and max x positions and joystick has direction
evanso 82:3211b31e9421 138 if (d_ == W || d_ == NW || d_ == SW) {
evanso 18:11068b98e261 139 return 2;
evanso 82:3211b31e9421 140 }else if (d_ == E || d_ == NE || d_ == SE) {
evanso 18:11068b98e261 141 return -2;
evanso 18:11068b98e261 142 }else {
evanso 18:11068b98e261 143 return 0;
evanso 18:11068b98e261 144 }
evanso 18:11068b98e261 145
evanso 24:479da6ca0e7e 146 // Debug and check variables when defined
evanso 18:11068b98e261 147 #ifdef CALCULATE_MAP_MOVEMENT_DEBUG
evanso 18:11068b98e261 148 printf("move map = %d\n", move_map_);
evanso 18:11068b98e261 149 printf("direction = %d\n", d_);
evanso 18:11068b98e261 150 printf("x = %d\n", position_x_spaceship_);
evanso 18:11068b98e261 151 #endif
evanso 18:11068b98e261 152 }
evanso 18:11068b98e261 153
evanso 82:3211b31e9421 154 int Map::get_position_x_map() {
evanso 27:8bb2bd97c319 155 return position_x_;
evanso 7:0af4ced868f5 156 }
evanso 24:479da6ca0e7e 157
evanso 82:3211b31e9421 158 int Map::get_length_map() {
evanso 24:479da6ca0e7e 159 return map_length_;
evanso 24:479da6ca0e7e 160 }