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@29:e96d91f1d39c, 2020-05-14 (annotated)
- Committer:
- evanso
- Date:
- Thu May 14 14:19:24 2020 +0000
- Revision:
- 29:e96d91f1d39c
- Parent:
- 28:a5958497d5ce
- Child:
- 31:6015e8ed859c
Fixed collision of spaceship bullets and alien. Added collision of alien bullet with spaceship which cause explosion.
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 | 24:479da6ca0e7e | 20 | void Alien::init(Gamepad &pad) { |
evanso | 27:8bb2bd97c319 | 21 | position_x_ = 10; |
evanso | 27:8bb2bd97c319 | 22 | position_y_ = 22; |
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 | 24:479da6ca0e7e | 99 | |
evanso | 20:febd920ec29e | 100 | // checks collision if bullet is going in east direction |
evanso | 20:febd920ec29e | 101 | if(bullet.get_direction()){ |
evanso | 20:febd920ec29e | 102 | Vector2D bullet_pos_two = bullet.get_pos_two(); |
evanso | 20:febd920ec29e | 103 | //int bullet_pos_twox = bullet_pos_two.x; |
evanso | 20:febd920ec29e | 104 | //int bullet_pos_twoy = bullet_pos_two.y; |
evanso | 27:8bb2bd97c319 | 105 | //printf ("alien x = %d, bullet x = %d\n", position_x_,bullet_pos_twox); |
evanso | 27:8bb2bd97c319 | 106 | //printf ("alien y = %d, bullet y = %d\n", position_y_,bullet_pos_twoy); |
evanso | 24:479da6ca0e7e | 107 | |
evanso | 29:e96d91f1d39c | 108 | if(bullet_pos_two.x >= position_x_ && |
evanso | 29:e96d91f1d39c | 109 | bullet_pos_two.x <= (position_x_ + 6) && |
evanso | 29:e96d91f1d39c | 110 | position_y_ <= bullet_pos_two.y && |
evanso | 27:8bb2bd97c319 | 111 | bullet_pos_two.y <= (position_y_ + 7)){ |
evanso | 20:febd920ec29e | 112 | collision = true; |
evanso | 20:febd920ec29e | 113 | } |
evanso | 24:479da6ca0e7e | 114 | //printf ("Collision 2 = %d\n", collision); |
evanso | 24:479da6ca0e7e | 115 | |
evanso | 20:febd920ec29e | 116 | // checks collision if bullet is going in west direction |
evanso | 20:febd920ec29e | 117 | }else if(!bullet.get_direction()){ |
evanso | 20:febd920ec29e | 118 | Vector2D bullet_pos_one = bullet.get_pos_one(); |
evanso | 29:e96d91f1d39c | 119 | if(bullet_pos_one.x <= (position_x_ + 6) && |
evanso | 29:e96d91f1d39c | 120 | bullet_pos_one.x >= position_x_ && |
evanso | 27:8bb2bd97c319 | 121 | position_y_ <= bullet_pos_one.y && |
evanso | 27:8bb2bd97c319 | 122 | bullet_pos_one.y <= (position_y_ + 7)){ |
evanso | 20:febd920ec29e | 123 | collision = true; |
evanso | 20:febd920ec29e | 124 | } |
evanso | 27:8bb2bd97c319 | 125 | |
evanso | 20:febd920ec29e | 126 | //printf ("Collision 3 = %d\n", collision); |
evanso | 20:febd920ec29e | 127 | } |
evanso | 27:8bb2bd97c319 | 128 | |
evanso | 20:febd920ec29e | 129 | //printf ("Collision 4 = %d\n", collision); |
evanso | 20:febd920ec29e | 130 | return collision; |
evanso | 21:f7d7834e3af1 | 131 | } |
evanso | 21:f7d7834e3af1 | 132 | |
evanso | 21:f7d7834e3af1 | 133 | int Alien::calc_alien_movement(Direction d_){ |
evanso | 27:8bb2bd97c319 | 134 | // moves the alien in oposite direction to spaceship when it's position is |
evanso | 27:8bb2bd97c319 | 135 | // at min and max x positions and joystick has direction |
evanso | 21:f7d7834e3af1 | 136 | if (d_ == W || d_ == NW || d_ == SW){ |
evanso | 21:f7d7834e3af1 | 137 | return 2; |
evanso | 21:f7d7834e3af1 | 138 | }else if (d_ == E || d_ == NE || d_ == SE){ |
evanso | 21:f7d7834e3af1 | 139 | return -2; |
evanso | 21:f7d7834e3af1 | 140 | }else { |
evanso | 21:f7d7834e3af1 | 141 | return 0; |
evanso | 21:f7d7834e3af1 | 142 | } |
evanso | 22:053c11a202e1 | 143 | } |
evanso | 22:053c11a202e1 | 144 | |
evanso | 22:053c11a202e1 | 145 | Vector2D Alien::get_pos(){ |
evanso | 27:8bb2bd97c319 | 146 | Vector2D pos = {position_x_,position_y_}; |
evanso | 22:053c11a202e1 | 147 | return pos; |
evanso | 24:479da6ca0e7e | 148 | } |
evanso | 24:479da6ca0e7e | 149 | |
evanso | 28:a5958497d5ce | 150 | int Alien::get_alien_fire_counter(){ |
evanso | 28:a5958497d5ce | 151 | return alien_move_counter_; |
evanso | 28:a5958497d5ce | 152 | } |