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@36:27aa597db3d2, 2020-05-15 (annotated)
- Committer:
- evanso
- Date:
- Fri May 15 16:31:25 2020 +0000
- Revision:
- 36:27aa597db3d2
- Parent:
- 35:577c65bf914e
- Child:
- 39:fc5586b930e3
Created Parent Classes folder to hold all of the parent class files. Shortened function lengths in Game Engine class and organised function definitions.
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 | 36:27aa597db3d2 | 21 | // Assign values to variables |
evanso | 35:577c65bf914e | 22 | sprite_x_length = 7; |
evanso | 35:577c65bf914e | 23 | sprite_y_length = 6; |
evanso | 32:c006a9882778 | 24 | position_x_ = position_x_start; |
evanso | 32:c006a9882778 | 25 | position_y_ = position_y_start; |
evanso | 28:a5958497d5ce | 26 | alien_move_counter_ = 0; |
evanso | 24:479da6ca0e7e | 27 | srand(pad.read_adc()*64000); |
evanso | 24:479da6ca0e7e | 28 | random_move_counter_ = 0; |
evanso | 24:479da6ca0e7e | 29 | random_direction_ = 0; |
evanso | 27:8bb2bd97c319 | 30 | direction_ = rand()%2; |
evanso | 34:85ccc16f24d2 | 31 | collision_people_element_ = -1; |
evanso | 34:85ccc16f24d2 | 32 | track_flag_ = false; |
evanso | 19:1bc0a2d22054 | 33 | } |
evanso | 24:479da6ca0e7e | 34 | |
evanso | 27:8bb2bd97c319 | 35 | void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, |
evanso | 34:85ccc16f24d2 | 36 | int map_length_, int position_x_map_, bool alien_collision) { |
evanso | 36:27aa597db3d2 | 37 | //moves alien with map movement |
evanso | 34:85ccc16f24d2 | 38 | position_x_ += calc_sprite_movement(d_); |
evanso | 21:f7d7834e3af1 | 39 | |
evanso | 36:27aa597db3d2 | 40 | // Moves alien at half speed of spaceship |
evanso | 28:a5958497d5ce | 41 | if (alien_move_counter_%2 == 0) { |
evanso | 36:27aa597db3d2 | 42 | |
evanso | 36:27aa597db3d2 | 43 | // Alien tracks spaceship if abducted poeple |
evanso | 34:85ccc16f24d2 | 44 | if(track_flag_){ |
evanso | 34:85ccc16f24d2 | 45 | move_hunt_mode(spaceship_pos); |
evanso | 34:85ccc16f24d2 | 46 | //printf("tack_flag = %d\n", track_flag_); |
evanso | 34:85ccc16f24d2 | 47 | //printf("pos_x = %d : pos y = %d\n",position_x_, position_y_); |
evanso | 36:27aa597db3d2 | 48 | |
evanso | 36:27aa597db3d2 | 49 | // If no abduction alien moves randomly |
evanso | 34:85ccc16f24d2 | 50 | }else if (random_move_counter_ == 0 || alien_collision){ |
evanso | 36:27aa597db3d2 | 51 | |
evanso | 36:27aa597db3d2 | 52 | //move alien to top of screen if a collision with person |
evanso | 34:85ccc16f24d2 | 53 | if(alien_collision){ |
evanso | 34:85ccc16f24d2 | 54 | random_direction_ = 6; |
evanso | 34:85ccc16f24d2 | 55 | random_move_counter_ = 42; |
evanso | 34:85ccc16f24d2 | 56 | alien_collision = false; |
evanso | 34:85ccc16f24d2 | 57 | }else{ |
evanso | 34:85ccc16f24d2 | 58 | set_random_move(); |
evanso | 34:85ccc16f24d2 | 59 | } |
evanso | 34:85ccc16f24d2 | 60 | |
evanso | 24:479da6ca0e7e | 61 | } |
evanso | 36:27aa597db3d2 | 62 | |
evanso | 36:27aa597db3d2 | 63 | //Move alien not tracking spackship move randomley |
evanso | 34:85ccc16f24d2 | 64 | if(!track_flag_){ |
evanso | 34:85ccc16f24d2 | 65 | move_direction(); |
evanso | 34:85ccc16f24d2 | 66 | } |
evanso | 36:27aa597db3d2 | 67 | |
evanso | 36:27aa597db3d2 | 68 | //stop alien going of map |
evanso | 24:479da6ca0e7e | 69 | off_screen_x_y_checker(map_length_, position_x_map_); |
evanso | 36:27aa597db3d2 | 70 | |
evanso | 24:479da6ca0e7e | 71 | random_move_counter_ --; |
evanso | 36:27aa597db3d2 | 72 | alien_fire_counter_++; |
evanso | 21:f7d7834e3af1 | 73 | } |
evanso | 36:27aa597db3d2 | 74 | |
evanso | 28:a5958497d5ce | 75 | alien_move_counter_++; |
evanso | 27:8bb2bd97c319 | 76 | lcd.drawSprite(position_x_, position_y_, 6, 7, (int*)k_alien_sprite); |
evanso | 20:febd920ec29e | 77 | } |
evanso | 20:febd920ec29e | 78 | |
evanso | 24:479da6ca0e7e | 79 | void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_){ |
evanso | 28:a5958497d5ce | 80 | // checks y postion of alien and then alters alien movement direction if at |
evanso | 28:a5958497d5ce | 81 | // edge of map |
evanso | 27:8bb2bd97c319 | 82 | if (position_y_ < 0) { |
evanso | 24:479da6ca0e7e | 83 | // sets alien direction to one that increces y value |
evanso | 27:8bb2bd97c319 | 84 | if(direction_){ |
evanso | 24:479da6ca0e7e | 85 | random_direction_ = 1; |
evanso | 24:479da6ca0e7e | 86 | }else{ |
evanso | 24:479da6ca0e7e | 87 | random_direction_ = 4; |
evanso | 24:479da6ca0e7e | 88 | } |
evanso | 36:27aa597db3d2 | 89 | |
evanso | 27:8bb2bd97c319 | 90 | }else if (position_y_ > 40) { |
evanso | 24:479da6ca0e7e | 91 | // sets alien direction to one that decreces y value |
evanso | 27:8bb2bd97c319 | 92 | if(direction_){ |
evanso | 24:479da6ca0e7e | 93 | random_direction_ = 2; |
evanso | 24:479da6ca0e7e | 94 | }else{ |
evanso | 24:479da6ca0e7e | 95 | random_direction_ = 5; |
evanso | 24:479da6ca0e7e | 96 | } |
evanso | 24:479da6ca0e7e | 97 | } |
evanso | 24:479da6ca0e7e | 98 | |
evanso | 24:479da6ca0e7e | 99 | // loops the alien round if it reaches the end of the map |
evanso | 27:8bb2bd97c319 | 100 | if(position_x_ <= (84 - map_length_)){ |
evanso | 27:8bb2bd97c319 | 101 | position_x_ += map_length_; |
evanso | 27:8bb2bd97c319 | 102 | }else if (position_x_ >= map_length_){ |
evanso | 27:8bb2bd97c319 | 103 | position_x_ -= map_length_ + 10; |
evanso | 24:479da6ca0e7e | 104 | } |
evanso | 24:479da6ca0e7e | 105 | |
evanso | 24:479da6ca0e7e | 106 | // Debug and check variables when defined |
evanso | 24:479da6ca0e7e | 107 | #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG |
evanso | 24:479da6ca0e7e | 108 | printf("\nposition_x_map_ = %d\n", position_x_map_); |
evanso | 24:479da6ca0e7e | 109 | printf("map_length_ = %d\n", map_length_ ); |
evanso | 27:8bb2bd97c319 | 110 | printf("map_length_ + position_x_map_ = %d\n", map_length_ + |
evanso | 27:8bb2bd97c319 | 111 | position_x_map_); |
evanso | 24:479da6ca0e7e | 112 | #endif |
evanso | 24:479da6ca0e7e | 113 | } |
evanso | 24:479da6ca0e7e | 114 | |
evanso | 24:479da6ca0e7e | 115 | void Alien::move_hunt_mode(Vector2D spaceship_pos){ |
evanso | 34:85ccc16f24d2 | 116 | if(spaceship_pos.x < position_x_){ |
evanso | 34:85ccc16f24d2 | 117 | position_x_ --; |
evanso | 34:85ccc16f24d2 | 118 | }else if(spaceship_pos.x > position_x_){ |
evanso | 27:8bb2bd97c319 | 119 | position_x_ ++; |
evanso | 21:f7d7834e3af1 | 120 | } |
evanso | 34:85ccc16f24d2 | 121 | |
evanso | 34:85ccc16f24d2 | 122 | if(spaceship_pos.y > position_y_){ |
evanso | 34:85ccc16f24d2 | 123 | position_y_ ++; |
evanso | 34:85ccc16f24d2 | 124 | }else if(spaceship_pos.y < position_y_){ |
evanso | 34:85ccc16f24d2 | 125 | position_y_ --; |
evanso | 34:85ccc16f24d2 | 126 | } |
evanso | 21:f7d7834e3af1 | 127 | } |
evanso | 21:f7d7834e3af1 | 128 | |
evanso | 28:a5958497d5ce | 129 | int Alien::get_alien_fire_counter(){ |
evanso | 28:a5958497d5ce | 130 | return alien_move_counter_; |
evanso | 28:a5958497d5ce | 131 | } |
evanso | 33:7fedd8029473 | 132 | |
evanso | 33:7fedd8029473 | 133 | void Alien::set_alien_x_pos(int position_x){ |
evanso | 33:7fedd8029473 | 134 | position_x_ = position_x; |
evanso | 33:7fedd8029473 | 135 | } |
evanso | 34:85ccc16f24d2 | 136 | |
evanso | 34:85ccc16f24d2 | 137 | void Alien::set_collision_people_element(int people_element){ |
evanso | 34:85ccc16f24d2 | 138 | collision_people_element_ = people_element; |
evanso | 34:85ccc16f24d2 | 139 | } |
evanso | 34:85ccc16f24d2 | 140 | |
evanso | 34:85ccc16f24d2 | 141 | int Alien::get_collision_people_element(){ |
evanso | 34:85ccc16f24d2 | 142 | return collision_people_element_; |
evanso | 34:85ccc16f24d2 | 143 | } |
evanso | 34:85ccc16f24d2 | 144 | |
evanso | 34:85ccc16f24d2 | 145 | void Alien::set_track_flag(bool track_flag){ |
evanso | 34:85ccc16f24d2 | 146 | track_flag_ = track_flag; |
evanso | 34:85ccc16f24d2 | 147 | } |