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:
Tue May 26 19:38:48 2020 +0000
Revision:
85:87bc28b151d8
Parent:
84:f61c85a5f13a
Child:
86:eecd168c3a23
Spell checked all of code and comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 19:1bc0a2d22054 1 #include "Alien.h"
evanso 19:1bc0a2d22054 2
evanso 85:87bc28b151d8 3 // Constant Definitions
evanso 82:3211b31e9421 4 #define SPRITE_X_LENGTH 7
evanso 82:3211b31e9421 5 #define SPRITE_Y_LENGTH 6
evanso 82:3211b31e9421 6
evanso 84:f61c85a5f13a 7 const int k_alien_sprite[6][7] = {
evanso 20:febd920ec29e 8 { 0,0,1,1,1,1,0,},
evanso 20:febd920ec29e 9 { 0,1,0,1,1,0,1,},
evanso 20:febd920ec29e 10 { 0,1,0,1,1,0,1,},
evanso 20:febd920ec29e 11 { 0,0,1,1,1,1,0,},
evanso 20:febd920ec29e 12 { 0,1,0,1,0,1,0,},
evanso 20:febd920ec29e 13 { 1,0,0,1,0,0,1,},
evanso 19:1bc0a2d22054 14 };
evanso 19:1bc0a2d22054 15
evanso 19:1bc0a2d22054 16 Alien::Alien() {
evanso 21:f7d7834e3af1 17
evanso 19:1bc0a2d22054 18 }
evanso 19:1bc0a2d22054 19
evanso 19:1bc0a2d22054 20 Alien::~Alien() {
evanso 19:1bc0a2d22054 21
evanso 19:1bc0a2d22054 22 }
evanso 19:1bc0a2d22054 23
evanso 32:c006a9882778 24 void Alien::init(Gamepad &pad, int position_x_start, int position_y_start) {
evanso 36:27aa597db3d2 25 // Assign values to variables
evanso 82:3211b31e9421 26 sprite_x_length = SPRITE_X_LENGTH;
evanso 82:3211b31e9421 27 sprite_y_length = SPRITE_Y_LENGTH;
evanso 32:c006a9882778 28 position_x_ = position_x_start;
evanso 32:c006a9882778 29 position_y_ = position_y_start;
evanso 28:a5958497d5ce 30 alien_move_counter_ = 0;
evanso 24:479da6ca0e7e 31 srand(pad.read_adc()*64000);
evanso 24:479da6ca0e7e 32 random_move_counter_ = 0;
evanso 24:479da6ca0e7e 33 random_direction_ = 0;
evanso 27:8bb2bd97c319 34 direction_ = rand()%2;
evanso 34:85ccc16f24d2 35 collision_people_element_ = -1;
evanso 34:85ccc16f24d2 36 track_flag_ = false;
evanso 19:1bc0a2d22054 37 }
evanso 24:479da6ca0e7e 38
evanso 27:8bb2bd97c319 39 void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_,
evanso 34:85ccc16f24d2 40 int map_length_, int position_x_map_, bool alien_collision) {
evanso 82:3211b31e9421 41 // Moves alien with map movement
evanso 34:85ccc16f24d2 42 position_x_ += calc_sprite_movement(d_);
evanso 21:f7d7834e3af1 43
evanso 36:27aa597db3d2 44 // Moves alien at half speed of spaceship
evanso 28:a5958497d5ce 45 if (alien_move_counter_%2 == 0) {
evanso 36:27aa597db3d2 46
evanso 85:87bc28b151d8 47 // Alien tracks spaceship if abducted people
evanso 85:87bc28b151d8 48 if (track_flag_) {
evanso 34:85ccc16f24d2 49 move_hunt_mode(spaceship_pos);
evanso 84:f61c85a5f13a 50
evanso 82:3211b31e9421 51 // printf("tack_flag = %d\n", track_flag_);
evanso 82:3211b31e9421 52 // printf("pos_x = %d : pos y = %d\n",position_x_, position_y_);
evanso 36:27aa597db3d2 53
evanso 36:27aa597db3d2 54 // If no abduction alien moves randomly
evanso 82:3211b31e9421 55 }else if (random_move_counter_ == 0 || alien_collision) {
evanso 36:27aa597db3d2 56
evanso 82:3211b31e9421 57 // Move alien to top of screen if a collision with person
evanso 85:87bc28b151d8 58 if (alien_collision) {
evanso 34:85ccc16f24d2 59 random_direction_ = 6;
evanso 34:85ccc16f24d2 60 random_move_counter_ = 42;
evanso 34:85ccc16f24d2 61 alien_collision = false;
evanso 34:85ccc16f24d2 62 }else{
evanso 34:85ccc16f24d2 63 set_random_move();
evanso 34:85ccc16f24d2 64 }
evanso 34:85ccc16f24d2 65
evanso 84:f61c85a5f13a 66 }
evanso 84:f61c85a5f13a 67
evanso 85:87bc28b151d8 68 // Move alien not tracking spaceship move randomly
evanso 85:87bc28b151d8 69 if (!track_flag_) {
evanso 34:85ccc16f24d2 70 move_direction();
evanso 34:85ccc16f24d2 71 }
evanso 36:27aa597db3d2 72
evanso 82:3211b31e9421 73 // Stop alien going of map
evanso 24:479da6ca0e7e 74 off_screen_x_y_checker(map_length_, position_x_map_);
evanso 36:27aa597db3d2 75
evanso 24:479da6ca0e7e 76 random_move_counter_ --;
evanso 36:27aa597db3d2 77 alien_fire_counter_++;
evanso 21:f7d7834e3af1 78 }
evanso 36:27aa597db3d2 79
evanso 28:a5958497d5ce 80 alien_move_counter_++;
evanso 84:f61c85a5f13a 81 lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH,
evanso 82:3211b31e9421 82 (int*)k_alien_sprite);
evanso 20:febd920ec29e 83 }
evanso 20:febd920ec29e 84
evanso 82:3211b31e9421 85 void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_) {
evanso 85:87bc28b151d8 86 // Checks y position of alien and then alters alien movement direction if at
evanso 28:a5958497d5ce 87 // edge of map
evanso 77:2ee9d1f9e282 88 if (position_y_ < 9) {
evanso 85:87bc28b151d8 89 // Sets alien direction to one that increases y value
evanso 85:87bc28b151d8 90 if (direction_) {
evanso 24:479da6ca0e7e 91 random_direction_ = 1;
evanso 24:479da6ca0e7e 92 }else{
evanso 24:479da6ca0e7e 93 random_direction_ = 4;
evanso 24:479da6ca0e7e 94 }
evanso 36:27aa597db3d2 95
evanso 39:fc5586b930e3 96 }else if (position_y_ > 42) {
evanso 85:87bc28b151d8 97 // Sets alien direction to one that decrees y value
evanso 85:87bc28b151d8 98 if (direction_) {
evanso 24:479da6ca0e7e 99 random_direction_ = 2;
evanso 24:479da6ca0e7e 100 }else{
evanso 24:479da6ca0e7e 101 random_direction_ = 5;
evanso 24:479da6ca0e7e 102 }
evanso 24:479da6ca0e7e 103 }
evanso 24:479da6ca0e7e 104
evanso 82:3211b31e9421 105 // Loops the alien round if it reaches the end of the map
evanso 85:87bc28b151d8 106 if (position_x_ <= (84 - map_length_)) {
evanso 27:8bb2bd97c319 107 position_x_ += map_length_;
evanso 82:3211b31e9421 108 }else if (position_x_ >= map_length_) {
evanso 27:8bb2bd97c319 109 position_x_ -= map_length_ + 10;
evanso 24:479da6ca0e7e 110 }
evanso 24:479da6ca0e7e 111
evanso 24:479da6ca0e7e 112 // Debug and check variables when defined
evanso 24:479da6ca0e7e 113 #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG
evanso 24:479da6ca0e7e 114 printf("\nposition_x_map_ = %d\n", position_x_map_);
evanso 24:479da6ca0e7e 115 printf("map_length_ = %d\n", map_length_ );
evanso 27:8bb2bd97c319 116 printf("map_length_ + position_x_map_ = %d\n", map_length_ +
evanso 27:8bb2bd97c319 117 position_x_map_);
evanso 24:479da6ca0e7e 118 #endif
evanso 24:479da6ca0e7e 119 }
evanso 24:479da6ca0e7e 120
evanso 82:3211b31e9421 121 void Alien::move_hunt_mode(Vector2D spaceship_pos) {
evanso 85:87bc28b151d8 122 if (spaceship_pos.x < position_x_) {
evanso 34:85ccc16f24d2 123 position_x_ --;
evanso 85:87bc28b151d8 124 }else if (spaceship_pos.x > position_x_) {
evanso 27:8bb2bd97c319 125 position_x_ ++;
evanso 21:f7d7834e3af1 126 }
evanso 34:85ccc16f24d2 127
evanso 85:87bc28b151d8 128 if (spaceship_pos.y > position_y_) {
evanso 34:85ccc16f24d2 129 position_y_ ++;
evanso 85:87bc28b151d8 130 }else if (spaceship_pos.y < position_y_) {
evanso 34:85ccc16f24d2 131 position_y_ --;
evanso 34:85ccc16f24d2 132 }
evanso 21:f7d7834e3af1 133 }
evanso 21:f7d7834e3af1 134
evanso 82:3211b31e9421 135 int Alien::get_alien_fire_counter() {
evanso 28:a5958497d5ce 136 return alien_move_counter_;
evanso 28:a5958497d5ce 137 }
evanso 33:7fedd8029473 138
evanso 82:3211b31e9421 139 void Alien::set_alien_x_pos(int position_x) {
evanso 33:7fedd8029473 140 position_x_ = position_x;
evanso 33:7fedd8029473 141 }
evanso 34:85ccc16f24d2 142
evanso 82:3211b31e9421 143 void Alien::set_collision_people_element(int people_element) {
evanso 34:85ccc16f24d2 144 collision_people_element_ = people_element;
evanso 34:85ccc16f24d2 145 }
evanso 34:85ccc16f24d2 146
evanso 82:3211b31e9421 147 int Alien::get_collision_people_element() {
evanso 34:85ccc16f24d2 148 return collision_people_element_;
evanso 34:85ccc16f24d2 149 }
evanso 34:85ccc16f24d2 150
evanso 82:3211b31e9421 151 void Alien::set_track_flag(bool track_flag) {
evanso 34:85ccc16f24d2 152 track_flag_ = track_flag;
evanso 40:71f947254fda 153 }
evanso 40:71f947254fda 154
evanso 82:3211b31e9421 155 bool Alien::get_track_flag() {
evanso 40:71f947254fda 156 return track_flag_;
evanso 85:87bc28b151d8 157 }