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 13 14:30:12 2020 +0000
Revision:
27:8bb2bd97c319
Parent:
26:1a7056eb3253
Child:
28:a5958497d5ce
Added position parent class for other classes to inherit XY positions and direction bool. Added more Doxygen comments. Made code and comments multi-line to increase readability.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 19:1bc0a2d22054 1 #include "Alien.h"
evanso 19:1bc0a2d22054 2
evanso 20:febd920ec29e 3 const int k_alien_sprite[6][7] = {
evanso 20:febd920ec29e 4 { 0,0,1,1,1,1,0,},
evanso 20:febd920ec29e 5 { 0,1,0,1,1,0,1,},
evanso 20:febd920ec29e 6 { 0,1,0,1,1,0,1,},
evanso 20:febd920ec29e 7 { 0,0,1,1,1,1,0,},
evanso 20:febd920ec29e 8 { 0,1,0,1,0,1,0,},
evanso 20:febd920ec29e 9 { 1,0,0,1,0,0,1,},
evanso 19:1bc0a2d22054 10 };
evanso 19:1bc0a2d22054 11
evanso 19:1bc0a2d22054 12 Alien::Alien() {
evanso 21:f7d7834e3af1 13
evanso 19:1bc0a2d22054 14 }
evanso 19:1bc0a2d22054 15
evanso 19:1bc0a2d22054 16 Alien::~Alien() {
evanso 19:1bc0a2d22054 17
evanso 19:1bc0a2d22054 18 }
evanso 19:1bc0a2d22054 19
evanso 24:479da6ca0e7e 20 void Alien::init(Gamepad &pad) {
evanso 27:8bb2bd97c319 21 position_x_ = 10;
evanso 27:8bb2bd97c319 22 position_y_ = 22;
evanso 21:f7d7834e3af1 23 alien_move_counter = 0;
evanso 24:479da6ca0e7e 24 srand(pad.read_adc()*64000);
evanso 24:479da6ca0e7e 25 random_move_counter_ = 0;
evanso 24:479da6ca0e7e 26 random_direction_ = 0;
evanso 27:8bb2bd97c319 27 direction_ = rand()%2;
evanso 19:1bc0a2d22054 28 }
evanso 24:479da6ca0e7e 29
evanso 27:8bb2bd97c319 30 void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_,
evanso 27:8bb2bd97c319 31 int map_length_, int position_x_map_) {
evanso 21:f7d7834e3af1 32 //moves alien spaceship movemen
evanso 27:8bb2bd97c319 33 position_x_ += calc_alien_movement(d_);
evanso 21:f7d7834e3af1 34
evanso 24:479da6ca0e7e 35 //Alien moves on its own but at half speed of spaceship
evanso 24:479da6ca0e7e 36 if (alien_move_counter%2 == 0) {
evanso 24:479da6ca0e7e 37 if (random_move_counter_ == 0){
evanso 24:479da6ca0e7e 38 set_random_move();
evanso 24:479da6ca0e7e 39 }
evanso 24:479da6ca0e7e 40 move_direction();
evanso 24:479da6ca0e7e 41 off_screen_x_y_checker(map_length_, position_x_map_);
evanso 24:479da6ca0e7e 42 random_move_counter_ --;
evanso 24:479da6ca0e7e 43
evanso 24:479da6ca0e7e 44 //printf("random_move_counter_ = %d\n", random_move_counter__);
evanso 21:f7d7834e3af1 45 }
evanso 21:f7d7834e3af1 46 alien_move_counter++;
evanso 21:f7d7834e3af1 47
evanso 27:8bb2bd97c319 48 lcd.drawSprite(position_x_, position_y_, 6, 7, (int*)k_alien_sprite);
evanso 20:febd920ec29e 49 }
evanso 20:febd920ec29e 50
evanso 24:479da6ca0e7e 51 void Alien::set_random_move(){
evanso 24:479da6ca0e7e 52 // alien only moves in one general direction
evanso 27:8bb2bd97c319 53 if(direction_){
evanso 24:479da6ca0e7e 54 random_direction_ = rand() % 3;
evanso 24:479da6ca0e7e 55 }else{
evanso 24:479da6ca0e7e 56 random_direction_ = rand() % 3 + 3;
evanso 26:1a7056eb3253 57 //printf("\random_direction_ = %d\n", random_direction_);
evanso 24:479da6ca0e7e 58 }
evanso 24:479da6ca0e7e 59 random_move_counter_ = rand() % 10 + 20;
evanso 24:479da6ca0e7e 60 }
evanso 24:479da6ca0e7e 61
evanso 24:479da6ca0e7e 62 void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_){
evanso 27:8bb2bd97c319 63 // checks y postion of alien and then alters moement direction if at edge of
evanso 27:8bb2bd97c319 64 // map
evanso 27:8bb2bd97c319 65 if (position_y_ < 0) {
evanso 24:479da6ca0e7e 66 // sets alien direction to one that increces y value
evanso 27:8bb2bd97c319 67 if(direction_){
evanso 24:479da6ca0e7e 68 random_direction_ = 1;
evanso 24:479da6ca0e7e 69 }else{
evanso 24:479da6ca0e7e 70 random_direction_ = 4;
evanso 24:479da6ca0e7e 71 }
evanso 27:8bb2bd97c319 72 }else if (position_y_ > 40) {
evanso 24:479da6ca0e7e 73 // sets alien direction to one that decreces y value
evanso 27:8bb2bd97c319 74 if(direction_){
evanso 24:479da6ca0e7e 75 random_direction_ = 2;
evanso 24:479da6ca0e7e 76 }else{
evanso 24:479da6ca0e7e 77 random_direction_ = 5;
evanso 24:479da6ca0e7e 78 }
evanso 24:479da6ca0e7e 79 }
evanso 24:479da6ca0e7e 80
evanso 24:479da6ca0e7e 81 // loops the alien round if it reaches the end of the map
evanso 27:8bb2bd97c319 82 if(position_x_ <= (84 - map_length_)){
evanso 27:8bb2bd97c319 83 position_x_ += map_length_;
evanso 27:8bb2bd97c319 84 }else if (position_x_ >= map_length_){
evanso 27:8bb2bd97c319 85 position_x_ -= map_length_ + 10;
evanso 24:479da6ca0e7e 86 }
evanso 24:479da6ca0e7e 87
evanso 24:479da6ca0e7e 88 // Debug and check variables when defined
evanso 24:479da6ca0e7e 89 #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG
evanso 24:479da6ca0e7e 90 printf("\nposition_x_map_ = %d\n", position_x_map_);
evanso 24:479da6ca0e7e 91 printf("map_length_ = %d\n", map_length_ );
evanso 27:8bb2bd97c319 92 printf("map_length_ + position_x_map_ = %d\n", map_length_ +
evanso 27:8bb2bd97c319 93 position_x_map_);
evanso 27:8bb2bd97c319 94 printf("alien x pos = %d\n",position_x_);
evanso 24:479da6ca0e7e 95 #endif
evanso 24:479da6ca0e7e 96 }
evanso 24:479da6ca0e7e 97
evanso 24:479da6ca0e7e 98 void Alien::move_direction(){
evanso 24:479da6ca0e7e 99 switch (random_direction_) {
evanso 24:479da6ca0e7e 100 case 0: set_alien_direction(1, 0); break;
evanso 24:479da6ca0e7e 101 case 1: set_alien_direction(1, 1); break;
evanso 24:479da6ca0e7e 102 case 2: set_alien_direction(1, -1); break;
evanso 24:479da6ca0e7e 103 case 3: set_alien_direction(-1, 0); break;
evanso 24:479da6ca0e7e 104 case 4: set_alien_direction(-1, 1); break;
evanso 24:479da6ca0e7e 105 case 5: set_alien_direction(-1, -1); break;
evanso 24:479da6ca0e7e 106 }
evanso 24:479da6ca0e7e 107 }
evanso 24:479da6ca0e7e 108
evanso 24:479da6ca0e7e 109 void Alien::move_hunt_mode(Vector2D spaceship_pos){
evanso 27:8bb2bd97c319 110 if(spaceship_pos.x <= position_x_){
evanso 27:8bb2bd97c319 111 position_x_ --;
evanso 21:f7d7834e3af1 112 }else{
evanso 27:8bb2bd97c319 113 position_x_ ++;
evanso 21:f7d7834e3af1 114 }
evanso 21:f7d7834e3af1 115 }
evanso 21:f7d7834e3af1 116
evanso 20:febd920ec29e 117 bool Alien::check_collision(Weapons bullet) {
evanso 20:febd920ec29e 118 bool collision = false;
evanso 20:febd920ec29e 119 //printf ("Collision 1 = %d\n", collision);
evanso 24:479da6ca0e7e 120
evanso 20:febd920ec29e 121 // checks collision if bullet is going in east direction
evanso 20:febd920ec29e 122 if(bullet.get_direction()){
evanso 20:febd920ec29e 123 Vector2D bullet_pos_two = bullet.get_pos_two();
evanso 20:febd920ec29e 124 //int bullet_pos_twox = bullet_pos_two.x;
evanso 20:febd920ec29e 125 //int bullet_pos_twoy = bullet_pos_two.y;
evanso 27:8bb2bd97c319 126 //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox);
evanso 27:8bb2bd97c319 127 //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy);
evanso 24:479da6ca0e7e 128
evanso 27:8bb2bd97c319 129 if(bullet_pos_two.x >= position_x_ && position_y_ <= bullet_pos_two.y &&
evanso 27:8bb2bd97c319 130 bullet_pos_two.y <= (position_y_ + 7)){
evanso 20:febd920ec29e 131 collision = true;
evanso 20:febd920ec29e 132 }
evanso 24:479da6ca0e7e 133 //printf ("Collision 2 = %d\n", collision);
evanso 24:479da6ca0e7e 134
evanso 20:febd920ec29e 135 // checks collision if bullet is going in west direction
evanso 20:febd920ec29e 136 }else if(!bullet.get_direction()){
evanso 20:febd920ec29e 137 Vector2D bullet_pos_one = bullet.get_pos_one();
evanso 27:8bb2bd97c319 138 if(bullet_pos_one.x <= position_x_ + 6 &&
evanso 27:8bb2bd97c319 139 position_y_ <= bullet_pos_one.y &&
evanso 27:8bb2bd97c319 140 bullet_pos_one.y <= (position_y_ + 7)){
evanso 20:febd920ec29e 141 collision = true;
evanso 20:febd920ec29e 142 }
evanso 27:8bb2bd97c319 143
evanso 20:febd920ec29e 144 //printf ("Collision 3 = %d\n", collision);
evanso 20:febd920ec29e 145 }
evanso 27:8bb2bd97c319 146
evanso 20:febd920ec29e 147 //printf ("Collision 4 = %d\n", collision);
evanso 20:febd920ec29e 148 return collision;
evanso 21:f7d7834e3af1 149 }
evanso 21:f7d7834e3af1 150
evanso 21:f7d7834e3af1 151 int Alien::calc_alien_movement(Direction d_){
evanso 27:8bb2bd97c319 152 // moves the alien in oposite direction to spaceship when it's position is
evanso 27:8bb2bd97c319 153 // at min and max x positions and joystick has direction
evanso 21:f7d7834e3af1 154 if (d_ == W || d_ == NW || d_ == SW){
evanso 21:f7d7834e3af1 155 return 2;
evanso 21:f7d7834e3af1 156 }else if (d_ == E || d_ == NE || d_ == SE){
evanso 21:f7d7834e3af1 157 return -2;
evanso 21:f7d7834e3af1 158 }else {
evanso 21:f7d7834e3af1 159 return 0;
evanso 21:f7d7834e3af1 160 }
evanso 22:053c11a202e1 161 }
evanso 22:053c11a202e1 162
evanso 22:053c11a202e1 163 Vector2D Alien::get_pos(){
evanso 27:8bb2bd97c319 164 Vector2D pos = {position_x_,position_y_};
evanso 22:053c11a202e1 165 return pos;
evanso 24:479da6ca0e7e 166 }
evanso 24:479da6ca0e7e 167
evanso 24:479da6ca0e7e 168 void Alien::set_alien_direction(int x_change,int y_change){
evanso 27:8bb2bd97c319 169 position_x_ += x_change;
evanso 27:8bb2bd97c319 170 position_y_ += y_change;
evanso 27:8bb2bd97c319 171 }