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:
Mon May 25 18:39:51 2020 +0000
Revision:
82:3211b31e9421
Parent:
77:2ee9d1f9e282
Child:
84:f61c85a5f13a
Made commenting and formatting of code more consistent.

Who changed what in which revision?

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