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:
86:eecd168c3a23
Final Submission. I have read and agreed with Statement of Academic Integrity.

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 87:832ca78426b5 59 random_direction_ = 6;
evanso 87:832ca78426b5 60
evanso 34:85ccc16f24d2 61 random_move_counter_ = 42;
evanso 34:85ccc16f24d2 62 alien_collision = false;
evanso 34:85ccc16f24d2 63 }else{
evanso 34:85ccc16f24d2 64 set_random_move();
evanso 34:85ccc16f24d2 65 }
evanso 34:85ccc16f24d2 66
evanso 84:f61c85a5f13a 67 }
evanso 87:832ca78426b5 68 // Stop alien going of map
evanso 87:832ca78426b5 69 off_screen_x_y_checker(map_length_, position_x_map_);
evanso 84:f61c85a5f13a 70
evanso 85:87bc28b151d8 71 // Move alien not tracking spaceship move randomly
evanso 85:87bc28b151d8 72 if (!track_flag_) {
evanso 34:85ccc16f24d2 73 move_direction();
evanso 34:85ccc16f24d2 74 }
evanso 36:27aa597db3d2 75
evanso 86:eecd168c3a23 76
evanso 36:27aa597db3d2 77
evanso 24:479da6ca0e7e 78 random_move_counter_ --;
evanso 36:27aa597db3d2 79 alien_fire_counter_++;
evanso 21:f7d7834e3af1 80 }
evanso 28:a5958497d5ce 81 alien_move_counter_++;
evanso 84:f61c85a5f13a 82 lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH,
evanso 82:3211b31e9421 83 (int*)k_alien_sprite);
evanso 20:febd920ec29e 84 }
evanso 20:febd920ec29e 85
evanso 82:3211b31e9421 86 void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_) {
evanso 85:87bc28b151d8 87 // Checks y position of alien and then alters alien movement direction if at
evanso 28:a5958497d5ce 88 // edge of map
evanso 77:2ee9d1f9e282 89 if (position_y_ < 9) {
evanso 85:87bc28b151d8 90 // Sets alien direction to one that increases y value
evanso 85:87bc28b151d8 91 if (direction_) {
evanso 24:479da6ca0e7e 92 random_direction_ = 1;
evanso 24:479da6ca0e7e 93 }else{
evanso 24:479da6ca0e7e 94 random_direction_ = 4;
evanso 24:479da6ca0e7e 95 }
evanso 36:27aa597db3d2 96
evanso 39:fc5586b930e3 97 }else if (position_y_ > 42) {
evanso 85:87bc28b151d8 98 // Sets alien direction to one that decrees y value
evanso 85:87bc28b151d8 99 if (direction_) {
evanso 24:479da6ca0e7e 100 random_direction_ = 2;
evanso 24:479da6ca0e7e 101 }else{
evanso 24:479da6ca0e7e 102 random_direction_ = 5;
evanso 24:479da6ca0e7e 103 }
evanso 24:479da6ca0e7e 104 }
evanso 24:479da6ca0e7e 105
evanso 82:3211b31e9421 106 // Loops the alien round if it reaches the end of the map
evanso 85:87bc28b151d8 107 if (position_x_ <= (84 - map_length_)) {
evanso 27:8bb2bd97c319 108 position_x_ += map_length_;
evanso 82:3211b31e9421 109 }else if (position_x_ >= map_length_) {
evanso 27:8bb2bd97c319 110 position_x_ -= map_length_ + 10;
evanso 24:479da6ca0e7e 111 }
evanso 24:479da6ca0e7e 112
evanso 24:479da6ca0e7e 113 // Debug and check variables when defined
evanso 24:479da6ca0e7e 114 #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG
evanso 24:479da6ca0e7e 115 printf("\nposition_x_map_ = %d\n", position_x_map_);
evanso 24:479da6ca0e7e 116 printf("map_length_ = %d\n", map_length_ );
evanso 27:8bb2bd97c319 117 printf("map_length_ + position_x_map_ = %d\n", map_length_ +
evanso 27:8bb2bd97c319 118 position_x_map_);
evanso 24:479da6ca0e7e 119 #endif
evanso 24:479da6ca0e7e 120 }
evanso 24:479da6ca0e7e 121
evanso 82:3211b31e9421 122 void Alien::move_hunt_mode(Vector2D spaceship_pos) {
evanso 85:87bc28b151d8 123 if (spaceship_pos.x < position_x_) {
evanso 34:85ccc16f24d2 124 position_x_ --;
evanso 85:87bc28b151d8 125 }else if (spaceship_pos.x > position_x_) {
evanso 27:8bb2bd97c319 126 position_x_ ++;
evanso 21:f7d7834e3af1 127 }
evanso 34:85ccc16f24d2 128
evanso 85:87bc28b151d8 129 if (spaceship_pos.y > position_y_) {
evanso 34:85ccc16f24d2 130 position_y_ ++;
evanso 85:87bc28b151d8 131 }else if (spaceship_pos.y < position_y_) {
evanso 34:85ccc16f24d2 132 position_y_ --;
evanso 34:85ccc16f24d2 133 }
evanso 21:f7d7834e3af1 134 }
evanso 21:f7d7834e3af1 135
evanso 82:3211b31e9421 136 int Alien::get_alien_fire_counter() {
evanso 28:a5958497d5ce 137 return alien_move_counter_;
evanso 28:a5958497d5ce 138 }
evanso 33:7fedd8029473 139
evanso 82:3211b31e9421 140 void Alien::set_alien_x_pos(int position_x) {
evanso 33:7fedd8029473 141 position_x_ = position_x;
evanso 33:7fedd8029473 142 }
evanso 34:85ccc16f24d2 143
evanso 82:3211b31e9421 144 void Alien::set_collision_people_element(int people_element) {
evanso 34:85ccc16f24d2 145 collision_people_element_ = people_element;
evanso 34:85ccc16f24d2 146 }
evanso 34:85ccc16f24d2 147
evanso 82:3211b31e9421 148 int Alien::get_collision_people_element() {
evanso 34:85ccc16f24d2 149 return collision_people_element_;
evanso 34:85ccc16f24d2 150 }
evanso 34:85ccc16f24d2 151
evanso 82:3211b31e9421 152 void Alien::set_track_flag(bool track_flag) {
evanso 34:85ccc16f24d2 153 track_flag_ = track_flag;
evanso 40:71f947254fda 154 }
evanso 40:71f947254fda 155
evanso 82:3211b31e9421 156 bool Alien::get_track_flag() {
evanso 40:71f947254fda 157 return track_flag_;
evanso 85:87bc28b151d8 158 }