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:
Thu May 14 20:04:14 2020 +0000
Revision:
32:c006a9882778
Parent:
31:6015e8ed859c
Child:
33:7fedd8029473
Multiple aliens now spawn in random positions and game has a set spawn rate.

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 32:c006a9882778 20 void Alien::init(Gamepad &pad, int position_x_start, int position_y_start) {
evanso 32:c006a9882778 21 position_x_ = position_x_start;
evanso 32:c006a9882778 22 position_y_ = position_y_start;
evanso 28:a5958497d5ce 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 28:a5958497d5ce 28 alien_move_counter_ = 0;
evanso 19:1bc0a2d22054 29 }
evanso 24:479da6ca0e7e 30
evanso 27:8bb2bd97c319 31 void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_,
evanso 27:8bb2bd97c319 32 int map_length_, int position_x_map_) {
evanso 21:f7d7834e3af1 33 //moves alien spaceship movemen
evanso 27:8bb2bd97c319 34 position_x_ += calc_alien_movement(d_);
evanso 21:f7d7834e3af1 35
evanso 24:479da6ca0e7e 36 //Alien moves on its own but at half speed of spaceship
evanso 28:a5958497d5ce 37 if (alien_move_counter_%2 == 0) {
evanso 24:479da6ca0e7e 38 if (random_move_counter_ == 0){
evanso 24:479da6ca0e7e 39 set_random_move();
evanso 24:479da6ca0e7e 40 }
evanso 24:479da6ca0e7e 41 move_direction();
evanso 24:479da6ca0e7e 42 off_screen_x_y_checker(map_length_, position_x_map_);
evanso 24:479da6ca0e7e 43 random_move_counter_ --;
evanso 28:a5958497d5ce 44 alien_fire_counter_++;
evanso 24:479da6ca0e7e 45
evanso 28:a5958497d5ce 46 //printf("random_move_counter_ = %d\n", random_move_counter_);
evanso 21:f7d7834e3af1 47 }
evanso 28:a5958497d5ce 48 alien_move_counter_++;
evanso 28:a5958497d5ce 49
evanso 27:8bb2bd97c319 50 lcd.drawSprite(position_x_, position_y_, 6, 7, (int*)k_alien_sprite);
evanso 20:febd920ec29e 51 }
evanso 20:febd920ec29e 52
evanso 24:479da6ca0e7e 53 void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_){
evanso 28:a5958497d5ce 54 // checks y postion of alien and then alters alien movement direction if at
evanso 28:a5958497d5ce 55 // edge of map
evanso 27:8bb2bd97c319 56 if (position_y_ < 0) {
evanso 24:479da6ca0e7e 57 // sets alien direction to one that increces y value
evanso 27:8bb2bd97c319 58 if(direction_){
evanso 24:479da6ca0e7e 59 random_direction_ = 1;
evanso 24:479da6ca0e7e 60 }else{
evanso 24:479da6ca0e7e 61 random_direction_ = 4;
evanso 24:479da6ca0e7e 62 }
evanso 27:8bb2bd97c319 63 }else if (position_y_ > 40) {
evanso 24:479da6ca0e7e 64 // sets alien direction to one that decreces y value
evanso 27:8bb2bd97c319 65 if(direction_){
evanso 24:479da6ca0e7e 66 random_direction_ = 2;
evanso 24:479da6ca0e7e 67 }else{
evanso 24:479da6ca0e7e 68 random_direction_ = 5;
evanso 24:479da6ca0e7e 69 }
evanso 24:479da6ca0e7e 70 }
evanso 24:479da6ca0e7e 71
evanso 24:479da6ca0e7e 72 // loops the alien round if it reaches the end of the map
evanso 27:8bb2bd97c319 73 if(position_x_ <= (84 - map_length_)){
evanso 27:8bb2bd97c319 74 position_x_ += map_length_;
evanso 27:8bb2bd97c319 75 }else if (position_x_ >= map_length_){
evanso 27:8bb2bd97c319 76 position_x_ -= map_length_ + 10;
evanso 24:479da6ca0e7e 77 }
evanso 24:479da6ca0e7e 78
evanso 24:479da6ca0e7e 79 // Debug and check variables when defined
evanso 24:479da6ca0e7e 80 #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG
evanso 24:479da6ca0e7e 81 printf("\nposition_x_map_ = %d\n", position_x_map_);
evanso 24:479da6ca0e7e 82 printf("map_length_ = %d\n", map_length_ );
evanso 27:8bb2bd97c319 83 printf("map_length_ + position_x_map_ = %d\n", map_length_ +
evanso 27:8bb2bd97c319 84 position_x_map_);
evanso 24:479da6ca0e7e 85 #endif
evanso 24:479da6ca0e7e 86 }
evanso 24:479da6ca0e7e 87
evanso 24:479da6ca0e7e 88 void Alien::move_hunt_mode(Vector2D spaceship_pos){
evanso 27:8bb2bd97c319 89 if(spaceship_pos.x <= position_x_){
evanso 27:8bb2bd97c319 90 position_x_ --;
evanso 21:f7d7834e3af1 91 }else{
evanso 27:8bb2bd97c319 92 position_x_ ++;
evanso 21:f7d7834e3af1 93 }
evanso 21:f7d7834e3af1 94 }
evanso 21:f7d7834e3af1 95
evanso 20:febd920ec29e 96 bool Alien::check_collision(Weapons bullet) {
evanso 20:febd920ec29e 97 bool collision = false;
evanso 20:febd920ec29e 98 //printf ("Collision 1 = %d\n", collision);
evanso 31:6015e8ed859c 99 Vector2D bullet_pos = bullet.get_pos();
evanso 20:febd920ec29e 100 // checks collision if bullet is going in east direction
evanso 20:febd920ec29e 101 if(bullet.get_direction()){
evanso 31:6015e8ed859c 102 //int bullet_pos_twox = bullet_pos.x;
evanso 31:6015e8ed859c 103 //int bullet_pos_twoy = bullet_pos.y;
evanso 27:8bb2bd97c319 104 //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox);
evanso 27:8bb2bd97c319 105 //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy);
evanso 24:479da6ca0e7e 106
evanso 31:6015e8ed859c 107 if((bullet_pos.x + 1) >= position_x_ &&
evanso 31:6015e8ed859c 108 (bullet_pos.x + 1) <= (position_x_ + 6) &&
evanso 31:6015e8ed859c 109 position_y_ <= bullet_pos.y &&
evanso 31:6015e8ed859c 110 bullet_pos.y <= (position_y_ + 7)){
evanso 20:febd920ec29e 111 collision = true;
evanso 20:febd920ec29e 112 }
evanso 24:479da6ca0e7e 113 //printf ("Collision 2 = %d\n", collision);
evanso 24:479da6ca0e7e 114
evanso 20:febd920ec29e 115 // checks collision if bullet is going in west direction
evanso 20:febd920ec29e 116 }else if(!bullet.get_direction()){
evanso 31:6015e8ed859c 117
evanso 31:6015e8ed859c 118 if(bullet_pos.x <= (position_x_ + 6) &&
evanso 31:6015e8ed859c 119 bullet_pos.x >= position_x_ &&
evanso 31:6015e8ed859c 120 position_y_ <= bullet_pos.y &&
evanso 31:6015e8ed859c 121 bullet_pos.y <= (position_y_ + 7)){
evanso 20:febd920ec29e 122 collision = true;
evanso 20:febd920ec29e 123 }
evanso 27:8bb2bd97c319 124
evanso 20:febd920ec29e 125 //printf ("Collision 3 = %d\n", collision);
evanso 20:febd920ec29e 126 }
evanso 27:8bb2bd97c319 127
evanso 20:febd920ec29e 128 //printf ("Collision 4 = %d\n", collision);
evanso 20:febd920ec29e 129 return collision;
evanso 21:f7d7834e3af1 130 }
evanso 21:f7d7834e3af1 131
evanso 21:f7d7834e3af1 132 int Alien::calc_alien_movement(Direction d_){
evanso 27:8bb2bd97c319 133 // moves the alien in oposite direction to spaceship when it's position is
evanso 27:8bb2bd97c319 134 // at min and max x positions and joystick has direction
evanso 21:f7d7834e3af1 135 if (d_ == W || d_ == NW || d_ == SW){
evanso 21:f7d7834e3af1 136 return 2;
evanso 21:f7d7834e3af1 137 }else if (d_ == E || d_ == NE || d_ == SE){
evanso 21:f7d7834e3af1 138 return -2;
evanso 21:f7d7834e3af1 139 }else {
evanso 21:f7d7834e3af1 140 return 0;
evanso 21:f7d7834e3af1 141 }
evanso 22:053c11a202e1 142 }
evanso 22:053c11a202e1 143
evanso 28:a5958497d5ce 144 int Alien::get_alien_fire_counter(){
evanso 28:a5958497d5ce 145 return alien_move_counter_;
evanso 28:a5958497d5ce 146 }