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:
Fri May 15 12:05:33 2020 +0000
Revision:
34:85ccc16f24d2
Parent:
33:7fedd8029473
Child:
36:27aa597db3d2
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?

UserRevisionLine numberNew contents of line
evanso 8:dd1037c5435b 1 #include "GameEngine.h"
evanso 8:dd1037c5435b 2
evanso 8:dd1037c5435b 3 GameEngine::GameEngine() {
evanso 8:dd1037c5435b 4
evanso 8:dd1037c5435b 5 }
evanso 8:dd1037c5435b 6
evanso 8:dd1037c5435b 7 GameEngine::~GameEngine() {
evanso 8:dd1037c5435b 8
evanso 8:dd1037c5435b 9 }
evanso 8:dd1037c5435b 10
evanso 13:12276eed13ac 11 void GameEngine::init() {
evanso 32:c006a9882778 12 srand(pad.read_adc()*64000);
evanso 8:dd1037c5435b 13 pad.init();
evanso 8:dd1037c5435b 14 lcd.init();
evanso 8:dd1037c5435b 15 spaceship.init();
evanso 13:12276eed13ac 16 map.init(pad);
evanso 33:7fedd8029473 17 spawn_alien_rate = 250;
evanso 34:85ccc16f24d2 18 alien_number_counter = 3;
evanso 33:7fedd8029473 19 spawn_alien_counter = 0;
evanso 30:814674b189f0 20 spaceship_lives = 3;
evanso 30:814674b189f0 21 spaceship_destroyed = false;
evanso 30:814674b189f0 22 reset_map_counter = 0;
evanso 31:6015e8ed859c 23 smart_bomb_counter = 5;
evanso 33:7fedd8029473 24 create_people();
evanso 8:dd1037c5435b 25 }
evanso 8:dd1037c5435b 26
evanso 13:12276eed13ac 27 void GameEngine::gameplay_loop() {
evanso 30:814674b189f0 28 // clear screen and set contrast
evanso 13:12276eed13ac 29 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 30 lcd.clear();
evanso 11:ab578a151f67 31
evanso 33:7fedd8029473 32 // creat objects
evanso 31:6015e8ed859c 33 create_weapons();
evanso 32:c006a9882778 34 spawn_aliens();
evanso 34:85ccc16f24d2 35 spawn_people();
evanso 19:1bc0a2d22054 36
evanso 33:7fedd8029473 37 //If spaceship is destroyed can't move
evanso 30:814674b189f0 38 if (!spaceship_destroyed){
evanso 33:7fedd8029473 39 read_joystick_direction();
evanso 33:7fedd8029473 40 spaceship.movement(d_);
evanso 30:814674b189f0 41 spaceship.draw(lcd);
evanso 30:814674b189f0 42 }
evanso 33:7fedd8029473 43
evanso 33:7fedd8029473 44 // Draws objects
evanso 18:11068b98e261 45 map.draw_map(lcd, d_);
evanso 20:febd920ec29e 46 draw_aliens();
evanso 19:1bc0a2d22054 47 draw_bullets();
evanso 33:7fedd8029473 48 draw_explosions();
evanso 33:7fedd8029473 49 draw_people();
evanso 30:814674b189f0 50
evanso 30:814674b189f0 51 // Resets map after set frames so spaceship explosion animation showes
evanso 30:814674b189f0 52 if(spaceship_destroyed){
evanso 30:814674b189f0 53 reset_map_counter++;
evanso 33:7fedd8029473 54 d_ = CENTRE;
evanso 30:814674b189f0 55 if(reset_map_counter == 50){
evanso 30:814674b189f0 56 reset_map();
evanso 30:814674b189f0 57 }
evanso 30:814674b189f0 58 }
evanso 30:814674b189f0 59
evanso 18:11068b98e261 60 // refresh's screen
evanso 18:11068b98e261 61 lcd.refresh();
evanso 13:12276eed13ac 62 }
evanso 13:12276eed13ac 63
evanso 15:90b6821bcf64 64 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 65 d_ = pad.get_direction();
evanso 18:11068b98e261 66 }
evanso 18:11068b98e261 67
evanso 32:c006a9882778 68 void GameEngine::spawn_aliens(){
evanso 34:85ccc16f24d2 69 if(alien_vector.size() <= alien_number_counter){
evanso 34:85ccc16f24d2 70 create_alien();
evanso 32:c006a9882778 71 }
evanso 32:c006a9882778 72
evanso 34:85ccc16f24d2 73 //slowley incresing the alien counter as game goes on to make harder
evanso 34:85ccc16f24d2 74 //int rounded_spawn_rate = floor(spawn_alien_rate);
evanso 34:85ccc16f24d2 75 if(spawn_alien_counter%750 == 0){
evanso 34:85ccc16f24d2 76 //alien_number_counter++;
evanso 32:c006a9882778 77 }
evanso 34:85ccc16f24d2 78
evanso 33:7fedd8029473 79 spawn_alien_counter++;
evanso 32:c006a9882778 80 }
evanso 32:c006a9882778 81
evanso 34:85ccc16f24d2 82 void GameEngine::spawn_people(){
evanso 34:85ccc16f24d2 83 if(people_vector.size() <= 5){
evanso 34:85ccc16f24d2 84 create_people();
evanso 34:85ccc16f24d2 85 }
evanso 34:85ccc16f24d2 86 }
evanso 34:85ccc16f24d2 87
evanso 31:6015e8ed859c 88 void GameEngine::create_weapons(){
evanso 31:6015e8ed859c 89 //Spaceship bullets
evanso 19:1bc0a2d22054 90 if (pad.A_pressed()){
evanso 19:1bc0a2d22054 91 // Bullet object
evanso 19:1bc0a2d22054 92 Weapons new_bullet;
evanso 19:1bc0a2d22054 93
evanso 27:8bb2bd97c319 94 new_bullet.init(spaceship.get_pos(),
evanso 28:a5958497d5ce 95 spaceship.get_spaceship_sprite_direction(), true);
evanso 19:1bc0a2d22054 96
evanso 20:febd920ec29e 97 // Stores bullet object in vector
evanso 19:1bc0a2d22054 98 bullet_vector.push_back(new_bullet);
evanso 20:febd920ec29e 99 }
evanso 33:7fedd8029473 100
evanso 31:6015e8ed859c 101 // Smart bomb
evanso 31:6015e8ed859c 102 if (pad.B_pressed() && smart_bomb_counter > 0){
evanso 31:6015e8ed859c 103 weapons.smart_bomb(lcd);
evanso 31:6015e8ed859c 104 smart_bomb_counter--;
evanso 31:6015e8ed859c 105
evanso 31:6015e8ed859c 106 //Deletes alien object if on screen
evanso 33:7fedd8029473 107 for (int i = (alien_vector.size() -1) ; i >= 0 ; i--){
evanso 31:6015e8ed859c 108 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 31:6015e8ed859c 109
evanso 31:6015e8ed859c 110 if(alien_pos.x <= 84 && alien_pos.x >= -6){
evanso 31:6015e8ed859c 111 create_explosion(alien_pos);
evanso 34:85ccc16f24d2 112 //delete person object if was carried by destroyed alien
evanso 34:85ccc16f24d2 113 if(alien_vector[i].get_collision_people_element() > -1){
evanso 34:85ccc16f24d2 114 people_vector.erase(people_vector.begin() +
evanso 34:85ccc16f24d2 115 alien_vector[i].get_collision_people_element());
evanso 34:85ccc16f24d2 116 }
evanso 34:85ccc16f24d2 117 alien_vector.erase(alien_vector.begin()+ i);
evanso 31:6015e8ed859c 118 }
evanso 31:6015e8ed859c 119 }
evanso 31:6015e8ed859c 120
evanso 31:6015e8ed859c 121 }
evanso 20:febd920ec29e 122 }
evanso 20:febd920ec29e 123
evanso 33:7fedd8029473 124 void GameEngine::create_people(){
evanso 33:7fedd8029473 125 // People object
evanso 33:7fedd8029473 126 People people;
evanso 33:7fedd8029473 127
evanso 33:7fedd8029473 128 people.init(pad, (rand() % 168 - 84));
evanso 33:7fedd8029473 129
evanso 33:7fedd8029473 130 // Stores alien object in vector
evanso 33:7fedd8029473 131 people_vector.push_back(people);
evanso 33:7fedd8029473 132 }
evanso 33:7fedd8029473 133
evanso 33:7fedd8029473 134 void GameEngine::draw_people(){
evanso 33:7fedd8029473 135 for (int i = 0; i < people_vector.size(); i++){
evanso 34:85ccc16f24d2 136 people_vector[i].draw_people(lcd, d_,
evanso 34:85ccc16f24d2 137 map.get_length_map(), map.get_position_x_map());
evanso 34:85ccc16f24d2 138
evanso 34:85ccc16f24d2 139 //errase person if at top of screen as captured by alien and alien is
evanso 34:85ccc16f24d2 140 //set to track mode
evanso 34:85ccc16f24d2 141 Vector2D people_pos = people_vector[i].get_pos();
evanso 34:85ccc16f24d2 142 if(people_pos.y <= 0){
evanso 34:85ccc16f24d2 143 for (int x = 0; x < alien_vector.size(); x++){
evanso 34:85ccc16f24d2 144 if(alien_vector[x].get_collision_people_element() == i){
evanso 34:85ccc16f24d2 145 alien_vector[x].set_track_flag(true);
evanso 34:85ccc16f24d2 146 }
evanso 34:85ccc16f24d2 147 }
evanso 34:85ccc16f24d2 148 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 149
evanso 34:85ccc16f24d2 150 }
evanso 33:7fedd8029473 151 }
evanso 33:7fedd8029473 152 }
evanso 33:7fedd8029473 153
evanso 20:febd920ec29e 154 void GameEngine::create_alien(){
evanso 32:c006a9882778 155 int position_x_start = 0;
evanso 32:c006a9882778 156
evanso 20:febd920ec29e 157 // Alien object
evanso 20:febd920ec29e 158 Alien new_alien;
evanso 32:c006a9882778 159
evanso 32:c006a9882778 160 //Creats randon number for x pos for x > 84 and x <0
evanso 32:c006a9882778 161 if(rand() % 2){
evanso 33:7fedd8029473 162 position_x_start = rand() % 84 + 84;
evanso 32:c006a9882778 163 }else{
evanso 33:7fedd8029473 164 position_x_start =rand() % 83 - 84;
evanso 32:c006a9882778 165 }
evanso 32:c006a9882778 166
evanso 32:c006a9882778 167 new_alien.init(pad, position_x_start, (rand() % 45));
evanso 20:febd920ec29e 168
evanso 20:febd920ec29e 169 // Stores alien object in vector
evanso 20:febd920ec29e 170 alien_vector.push_back(new_alien);
evanso 19:1bc0a2d22054 171 }
evanso 19:1bc0a2d22054 172
evanso 29:e96d91f1d39c 173 void GameEngine::create_explosion(Vector2D destroyed_position){
evanso 25:70b55f5bfc87 174 // explosion object
evanso 25:70b55f5bfc87 175 Explosion new_explosion;
evanso 25:70b55f5bfc87 176
evanso 29:e96d91f1d39c 177 new_explosion.init(destroyed_position);
evanso 25:70b55f5bfc87 178
evanso 25:70b55f5bfc87 179 // Stores explosion object in vector
evanso 25:70b55f5bfc87 180 explosion_vector.push_back(new_explosion);
evanso 25:70b55f5bfc87 181 }
evanso 25:70b55f5bfc87 182
evanso 19:1bc0a2d22054 183 void GameEngine::draw_bullets(){
evanso 29:e96d91f1d39c 184 // interates over bullet vectors, moves and draws bullet object
evanso 29:e96d91f1d39c 185
evanso 29:e96d91f1d39c 186 // Alien bullets
evanso 28:a5958497d5ce 187 for (int i = 0; i < alien_bullet_vector.size(); i++){
evanso 28:a5958497d5ce 188 alien_bullet_vector[i].draw_alien_bullet(lcd, d_);
evanso 28:a5958497d5ce 189
evanso 29:e96d91f1d39c 190 // deletes alien bullet object after bullet has moved set distance
evanso 33:7fedd8029473 191 if(alien_bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 28:a5958497d5ce 192 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 28:a5958497d5ce 193 }
evanso 29:e96d91f1d39c 194
evanso 30:814674b189f0 195 // Deletes bullet and shpaceship if collision detected
evanso 30:814674b189f0 196 if (spaceship.check_collision(alien_bullet_vector[i]) &&
evanso 30:814674b189f0 197 !spaceship_destroyed ){
evanso 29:e96d91f1d39c 198 create_explosion(spaceship.get_pos());
evanso 30:814674b189f0 199
evanso 30:814674b189f0 200 spaceship_destroyed = true;
evanso 30:814674b189f0 201 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 29:e96d91f1d39c 202 }
evanso 28:a5958497d5ce 203 }
evanso 29:e96d91f1d39c 204
evanso 29:e96d91f1d39c 205 // Spaceship bullets
evanso 19:1bc0a2d22054 206 for (int i = 0; i < bullet_vector.size(); i++){
evanso 19:1bc0a2d22054 207 bullet_vector[i].draw_bullet(lcd);
evanso 28:a5958497d5ce 208
evanso 28:a5958497d5ce 209 // deletes bullet object after bullet has moved set distance
evanso 28:a5958497d5ce 210 if(bullet_vector[i].get_bullet_delete_counter() >> 5){
evanso 20:febd920ec29e 211 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 20:febd920ec29e 212 }
evanso 19:1bc0a2d22054 213 }
evanso 19:1bc0a2d22054 214 }
evanso 19:1bc0a2d22054 215
evanso 20:febd920ec29e 216 void GameEngine::draw_aliens(){
evanso 29:e96d91f1d39c 217 // interates over alien vector and draws each new_alien object
evanso 20:febd920ec29e 218 for (int i = 0; i < alien_vector.size(); i++){
evanso 34:85ccc16f24d2 219 // Checks Alien and person collision
evanso 34:85ccc16f24d2 220 for (int x = 0; x < people_vector.size(); x++){
evanso 34:85ccc16f24d2 221 if (!people_vector[x].get_alien_collision_flag() &&
evanso 34:85ccc16f24d2 222 people_vector[x].check_alien_collision(alien_vector[i])){
evanso 34:85ccc16f24d2 223 alien_vector[i].set_collision_people_element(x);
evanso 34:85ccc16f24d2 224 }
evanso 34:85ccc16f24d2 225 }
evanso 34:85ccc16f24d2 226 //draws collision if detected
evanso 34:85ccc16f24d2 227 if(alien_vector[i].get_collision_people_element() > -1){
evanso 34:85ccc16f24d2 228 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 34:85ccc16f24d2 229 map.get_length_map(), map.get_position_x_map(),true);
evanso 34:85ccc16f24d2 230 }else{
evanso 34:85ccc16f24d2 231 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_,
evanso 34:85ccc16f24d2 232 map.get_length_map(), map.get_position_x_map(),false);
evanso 34:85ccc16f24d2 233 }
evanso 20:febd920ec29e 234
evanso 30:814674b189f0 235 // deleted spaceship if alien ship touches spaceship
evanso 30:814674b189f0 236 if (spaceship.check_alien_collision(alien_vector[i]) &&
evanso 30:814674b189f0 237 !spaceship_destroyed){
evanso 30:814674b189f0 238 create_explosion(spaceship.get_pos());
evanso 30:814674b189f0 239 spaceship_destroyed = true;
evanso 30:814674b189f0 240 }
evanso 30:814674b189f0 241
evanso 29:e96d91f1d39c 242 // alien sprites fire bullet randomley
evanso 28:a5958497d5ce 243 if (alien_vector[i].get_alien_fire_counter()%60 == 0){
evanso 28:a5958497d5ce 244 Weapons alien_bullet;
evanso 28:a5958497d5ce 245
evanso 28:a5958497d5ce 246 // fires bullet towards direction of spaceship
evanso 28:a5958497d5ce 247 bool alien_bullet_direction = false;
evanso 28:a5958497d5ce 248 Vector2D spaceship_pos = spaceship.get_pos();
evanso 28:a5958497d5ce 249 Vector2D alien_pos = alien_vector[i].get_pos();
evanso 28:a5958497d5ce 250 if(spaceship_pos.x > alien_pos.x){
evanso 28:a5958497d5ce 251 alien_bullet_direction = true;
evanso 28:a5958497d5ce 252 }
evanso 28:a5958497d5ce 253
evanso 28:a5958497d5ce 254 alien_bullet.init(alien_vector[i].get_pos(),
evanso 28:a5958497d5ce 255 alien_bullet_direction , false);
evanso 28:a5958497d5ce 256
evanso 28:a5958497d5ce 257 // Stores bullet object in vector
evanso 28:a5958497d5ce 258 alien_bullet_vector.push_back(alien_bullet);
evanso 28:a5958497d5ce 259 }
evanso 28:a5958497d5ce 260
evanso 29:e96d91f1d39c 261 // Deletes bullet and alien if collision detected and draws explosion
evanso 29:e96d91f1d39c 262 for (int x = 0; x < bullet_vector.size(); x++){
evanso 29:e96d91f1d39c 263 if (alien_vector[i].check_collision(bullet_vector[x])){
evanso 29:e96d91f1d39c 264 create_explosion(alien_vector[i].get_pos());
evanso 34:85ccc16f24d2 265
evanso 34:85ccc16f24d2 266 //delete person object if was carried by destroyed alien
evanso 34:85ccc16f24d2 267 if(alien_vector[i].get_collision_people_element() > -1){
evanso 34:85ccc16f24d2 268 people_vector.erase(people_vector.begin() +
evanso 34:85ccc16f24d2 269 alien_vector[i].get_collision_people_element());
evanso 34:85ccc16f24d2 270 }
evanso 25:70b55f5bfc87 271
evanso 29:e96d91f1d39c 272 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 34:85ccc16f24d2 273 alien_vector.erase(alien_vector.begin()+ i);
evanso 29:e96d91f1d39c 274 }
evanso 25:70b55f5bfc87 275 }
evanso 25:70b55f5bfc87 276 }
evanso 25:70b55f5bfc87 277 }
evanso 25:70b55f5bfc87 278
evanso 25:70b55f5bfc87 279 void GameEngine::draw_explosions(){
evanso 27:8bb2bd97c319 280 // interates over expoltion vector and draws each explosion object then
evanso 27:8bb2bd97c319 281 // deleted object after set size
evanso 25:70b55f5bfc87 282 for (int i = 0; i < explosion_vector.size(); i++){
evanso 25:70b55f5bfc87 283 explosion_vector[i].draw_explosion(lcd);
evanso 25:70b55f5bfc87 284
evanso 25:70b55f5bfc87 285 // delete explosion after reaches set size
evanso 25:70b55f5bfc87 286 if(explosion_vector[i].get_explosion_radius() == 8){
evanso 25:70b55f5bfc87 287 explosion_vector.erase(explosion_vector.begin()+ i);
evanso 20:febd920ec29e 288 }
evanso 20:febd920ec29e 289 }
evanso 30:814674b189f0 290 }
evanso 30:814674b189f0 291
evanso 30:814674b189f0 292 void GameEngine::reset_map(){
evanso 30:814674b189f0 293 spaceship.init();
evanso 30:814674b189f0 294 spaceship_lives--;
evanso 30:814674b189f0 295 spaceship_destroyed = false;
evanso 30:814674b189f0 296 reset_map_counter = 0;
evanso 33:7fedd8029473 297
evanso 34:85ccc16f24d2 298 // erase aliens so redrawn in random positions when respawning spaceship
evanso 34:85ccc16f24d2 299 for (int i = (alien_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 300 alien_vector.erase(alien_vector.begin()+ i);
evanso 34:85ccc16f24d2 301 }
evanso 34:85ccc16f24d2 302 // erase all people so redrawn at bottom of map
evanso 34:85ccc16f24d2 303 for (int i = (people_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 304 people_vector.erase(people_vector.begin()+ i);
evanso 34:85ccc16f24d2 305 }
evanso 34:85ccc16f24d2 306 // erase all palien bullets
evanso 34:85ccc16f24d2 307 for (int i = (alien_bullet_vector.size() - 1); i >= 0 ; i--){
evanso 34:85ccc16f24d2 308 alien_bullet_vector.erase(alien_bullet_vector.begin()+ i);
evanso 33:7fedd8029473 309 }
evanso 20:febd920ec29e 310 }