Ben Evans University Second Year Project. Game Called Defender.
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.
Alien/Alien.cpp@34:85ccc16f24d2, 2020-05-15 (annotated)
- Committer:
- evanso
- Date:
- Fri May 15 12:05:33 2020 +0000
- Revision:
- 34:85ccc16f24d2
- Parent:
- 33:7fedd8029473
- Child:
- 35:577c65bf914e
Added when aliens collide with people, the people are abducted to the top of the screen. Then that alien tracks the spaceship and moves towards it rather than randomly moving.
Who changed what in which revision?
User | Revision | Line number | New 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 | 34:85ccc16f24d2 | 28 | collision_people_element_ = -1; |
evanso | 34:85ccc16f24d2 | 29 | track_flag_ = false; |
evanso | 19:1bc0a2d22054 | 30 | } |
evanso | 24:479da6ca0e7e | 31 | |
evanso | 27:8bb2bd97c319 | 32 | void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, |
evanso | 34:85ccc16f24d2 | 33 | int map_length_, int position_x_map_, bool alien_collision) { |
evanso | 21:f7d7834e3af1 | 34 | //moves alien spaceship movemen |
evanso | 34:85ccc16f24d2 | 35 | position_x_ += calc_sprite_movement(d_); |
evanso | 21:f7d7834e3af1 | 36 | |
evanso | 24:479da6ca0e7e | 37 | //Alien moves on its own but at half speed of spaceship |
evanso | 28:a5958497d5ce | 38 | if (alien_move_counter_%2 == 0) { |
evanso | 34:85ccc16f24d2 | 39 | if(track_flag_){ |
evanso | 34:85ccc16f24d2 | 40 | move_hunt_mode(spaceship_pos); |
evanso | 34:85ccc16f24d2 | 41 | //printf("tack_flag = %d\n", track_flag_); |
evanso | 34:85ccc16f24d2 | 42 | //printf("pos_x = %d : pos y = %d\n",position_x_, position_y_); |
evanso | 34:85ccc16f24d2 | 43 | }else if (random_move_counter_ == 0 || alien_collision){ |
evanso | 34:85ccc16f24d2 | 44 | if(alien_collision){ |
evanso | 34:85ccc16f24d2 | 45 | //move alien to top of screen |
evanso | 34:85ccc16f24d2 | 46 | random_direction_ = 6; |
evanso | 34:85ccc16f24d2 | 47 | random_move_counter_ = 42; |
evanso | 34:85ccc16f24d2 | 48 | alien_collision = false; |
evanso | 34:85ccc16f24d2 | 49 | }else{ |
evanso | 34:85ccc16f24d2 | 50 | set_random_move(); |
evanso | 34:85ccc16f24d2 | 51 | } |
evanso | 34:85ccc16f24d2 | 52 | |
evanso | 24:479da6ca0e7e | 53 | } |
evanso | 34:85ccc16f24d2 | 54 | if(!track_flag_){ |
evanso | 34:85ccc16f24d2 | 55 | move_direction(); |
evanso | 34:85ccc16f24d2 | 56 | } |
evanso | 24:479da6ca0e7e | 57 | off_screen_x_y_checker(map_length_, position_x_map_); |
evanso | 24:479da6ca0e7e | 58 | random_move_counter_ --; |
evanso | 28:a5958497d5ce | 59 | alien_fire_counter_++; |
evanso | 24:479da6ca0e7e | 60 | |
evanso | 34:85ccc16f24d2 | 61 | |
evanso | 21:f7d7834e3af1 | 62 | } |
evanso | 28:a5958497d5ce | 63 | alien_move_counter_++; |
evanso | 28:a5958497d5ce | 64 | |
evanso | 27:8bb2bd97c319 | 65 | lcd.drawSprite(position_x_, position_y_, 6, 7, (int*)k_alien_sprite); |
evanso | 20:febd920ec29e | 66 | } |
evanso | 20:febd920ec29e | 67 | |
evanso | 24:479da6ca0e7e | 68 | void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_){ |
evanso | 28:a5958497d5ce | 69 | // checks y postion of alien and then alters alien movement direction if at |
evanso | 28:a5958497d5ce | 70 | // edge of map |
evanso | 27:8bb2bd97c319 | 71 | if (position_y_ < 0) { |
evanso | 24:479da6ca0e7e | 72 | // sets alien direction to one that increces y value |
evanso | 27:8bb2bd97c319 | 73 | if(direction_){ |
evanso | 24:479da6ca0e7e | 74 | random_direction_ = 1; |
evanso | 24:479da6ca0e7e | 75 | }else{ |
evanso | 24:479da6ca0e7e | 76 | random_direction_ = 4; |
evanso | 24:479da6ca0e7e | 77 | } |
evanso | 27:8bb2bd97c319 | 78 | }else if (position_y_ > 40) { |
evanso | 24:479da6ca0e7e | 79 | // sets alien direction to one that decreces y value |
evanso | 27:8bb2bd97c319 | 80 | if(direction_){ |
evanso | 24:479da6ca0e7e | 81 | random_direction_ = 2; |
evanso | 24:479da6ca0e7e | 82 | }else{ |
evanso | 24:479da6ca0e7e | 83 | random_direction_ = 5; |
evanso | 24:479da6ca0e7e | 84 | } |
evanso | 24:479da6ca0e7e | 85 | } |
evanso | 24:479da6ca0e7e | 86 | |
evanso | 24:479da6ca0e7e | 87 | // loops the alien round if it reaches the end of the map |
evanso | 27:8bb2bd97c319 | 88 | if(position_x_ <= (84 - map_length_)){ |
evanso | 27:8bb2bd97c319 | 89 | position_x_ += map_length_; |
evanso | 27:8bb2bd97c319 | 90 | }else if (position_x_ >= map_length_){ |
evanso | 27:8bb2bd97c319 | 91 | position_x_ -= map_length_ + 10; |
evanso | 24:479da6ca0e7e | 92 | } |
evanso | 24:479da6ca0e7e | 93 | |
evanso | 24:479da6ca0e7e | 94 | // Debug and check variables when defined |
evanso | 24:479da6ca0e7e | 95 | #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG |
evanso | 24:479da6ca0e7e | 96 | printf("\nposition_x_map_ = %d\n", position_x_map_); |
evanso | 24:479da6ca0e7e | 97 | printf("map_length_ = %d\n", map_length_ ); |
evanso | 27:8bb2bd97c319 | 98 | printf("map_length_ + position_x_map_ = %d\n", map_length_ + |
evanso | 27:8bb2bd97c319 | 99 | position_x_map_); |
evanso | 24:479da6ca0e7e | 100 | #endif |
evanso | 24:479da6ca0e7e | 101 | } |
evanso | 24:479da6ca0e7e | 102 | |
evanso | 24:479da6ca0e7e | 103 | void Alien::move_hunt_mode(Vector2D spaceship_pos){ |
evanso | 34:85ccc16f24d2 | 104 | if(spaceship_pos.x < position_x_){ |
evanso | 34:85ccc16f24d2 | 105 | position_x_ --; |
evanso | 34:85ccc16f24d2 | 106 | }else if(spaceship_pos.x > position_x_){ |
evanso | 27:8bb2bd97c319 | 107 | position_x_ ++; |
evanso | 21:f7d7834e3af1 | 108 | } |
evanso | 34:85ccc16f24d2 | 109 | |
evanso | 34:85ccc16f24d2 | 110 | if(spaceship_pos.y > position_y_){ |
evanso | 34:85ccc16f24d2 | 111 | position_y_ ++; |
evanso | 34:85ccc16f24d2 | 112 | }else if(spaceship_pos.y < position_y_){ |
evanso | 34:85ccc16f24d2 | 113 | position_y_ --; |
evanso | 34:85ccc16f24d2 | 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 | 31:6015e8ed859c | 120 | Vector2D bullet_pos = bullet.get_pos(); |
evanso | 20:febd920ec29e | 121 | // checks collision if bullet is going in east direction |
evanso | 20:febd920ec29e | 122 | if(bullet.get_direction()){ |
evanso | 31:6015e8ed859c | 123 | //int bullet_pos_twox = bullet_pos.x; |
evanso | 31:6015e8ed859c | 124 | //int bullet_pos_twoy = bullet_pos.y; |
evanso | 27:8bb2bd97c319 | 125 | //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox); |
evanso | 27:8bb2bd97c319 | 126 | //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy); |
evanso | 24:479da6ca0e7e | 127 | |
evanso | 31:6015e8ed859c | 128 | if((bullet_pos.x + 1) >= position_x_ && |
evanso | 31:6015e8ed859c | 129 | (bullet_pos.x + 1) <= (position_x_ + 6) && |
evanso | 31:6015e8ed859c | 130 | position_y_ <= bullet_pos.y && |
evanso | 31:6015e8ed859c | 131 | bullet_pos.y <= (position_y_ + 7)){ |
evanso | 20:febd920ec29e | 132 | collision = true; |
evanso | 20:febd920ec29e | 133 | } |
evanso | 24:479da6ca0e7e | 134 | //printf ("Collision 2 = %d\n", collision); |
evanso | 24:479da6ca0e7e | 135 | |
evanso | 20:febd920ec29e | 136 | // checks collision if bullet is going in west direction |
evanso | 20:febd920ec29e | 137 | }else if(!bullet.get_direction()){ |
evanso | 31:6015e8ed859c | 138 | |
evanso | 31:6015e8ed859c | 139 | if(bullet_pos.x <= (position_x_ + 6) && |
evanso | 31:6015e8ed859c | 140 | bullet_pos.x >= position_x_ && |
evanso | 31:6015e8ed859c | 141 | position_y_ <= bullet_pos.y && |
evanso | 31:6015e8ed859c | 142 | bullet_pos.y <= (position_y_ + 7)){ |
evanso | 20:febd920ec29e | 143 | collision = true; |
evanso | 20:febd920ec29e | 144 | } |
evanso | 27:8bb2bd97c319 | 145 | |
evanso | 20:febd920ec29e | 146 | //printf ("Collision 3 = %d\n", collision); |
evanso | 20:febd920ec29e | 147 | } |
evanso | 27:8bb2bd97c319 | 148 | |
evanso | 20:febd920ec29e | 149 | //printf ("Collision 4 = %d\n", collision); |
evanso | 20:febd920ec29e | 150 | return collision; |
evanso | 21:f7d7834e3af1 | 151 | } |
evanso | 21:f7d7834e3af1 | 152 | |
evanso | 22:053c11a202e1 | 153 | |
evanso | 28:a5958497d5ce | 154 | int Alien::get_alien_fire_counter(){ |
evanso | 28:a5958497d5ce | 155 | return alien_move_counter_; |
evanso | 28:a5958497d5ce | 156 | } |
evanso | 33:7fedd8029473 | 157 | |
evanso | 33:7fedd8029473 | 158 | void Alien::set_alien_x_pos(int position_x){ |
evanso | 33:7fedd8029473 | 159 | position_x_ = position_x; |
evanso | 33:7fedd8029473 | 160 | } |
evanso | 34:85ccc16f24d2 | 161 | |
evanso | 34:85ccc16f24d2 | 162 | void Alien::set_collision_people_element(int people_element){ |
evanso | 34:85ccc16f24d2 | 163 | collision_people_element_ = people_element; |
evanso | 34:85ccc16f24d2 | 164 | } |
evanso | 34:85ccc16f24d2 | 165 | |
evanso | 34:85ccc16f24d2 | 166 | int Alien::get_collision_people_element(){ |
evanso | 34:85ccc16f24d2 | 167 | return collision_people_element_; |
evanso | 34:85ccc16f24d2 | 168 | } |
evanso | 34:85ccc16f24d2 | 169 | |
evanso | 34:85ccc16f24d2 | 170 | void Alien::set_track_flag(bool track_flag){ |
evanso | 34:85ccc16f24d2 | 171 | track_flag_ = track_flag; |
evanso | 34:85ccc16f24d2 | 172 | } |