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:
Tue May 12 23:08:08 2020 +0000
Revision:
25:70b55f5bfc87
Parent:
24:479da6ca0e7e
Child:
27:8bb2bd97c319
Added explosion class. When the alien is now shot it explodes.

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 8:dd1037c5435b 12 pad.init();
evanso 8:dd1037c5435b 13 lcd.init();
evanso 8:dd1037c5435b 14 spaceship.init();
evanso 13:12276eed13ac 15 map.init(pad);
evanso 20:febd920ec29e 16 create_alien();
evanso 8:dd1037c5435b 17 }
evanso 8:dd1037c5435b 18
evanso 13:12276eed13ac 19 void GameEngine::gameplay_loop() {
evanso 11:ab578a151f67 20 // clear screen
evanso 13:12276eed13ac 21 lcd.setContrast(pad.read_pot1());
evanso 8:dd1037c5435b 22 lcd.clear();
evanso 11:ab578a151f67 23
evanso 15:90b6821bcf64 24 // Gets movements
evanso 15:90b6821bcf64 25 read_joystick_direction();
evanso 13:12276eed13ac 26 spaceship.movement(d_);
evanso 19:1bc0a2d22054 27 create_bullet();
evanso 19:1bc0a2d22054 28
evanso 18:11068b98e261 29 // Draws
evanso 18:11068b98e261 30 spaceship.draw(lcd);
evanso 18:11068b98e261 31 map.draw_map(lcd, d_);
evanso 20:febd920ec29e 32 draw_aliens();
evanso 19:1bc0a2d22054 33 draw_bullets();
evanso 25:70b55f5bfc87 34 draw_explosions();
evanso 18:11068b98e261 35
evanso 18:11068b98e261 36 // refresh's screen
evanso 18:11068b98e261 37 lcd.refresh();
evanso 13:12276eed13ac 38 }
evanso 13:12276eed13ac 39
evanso 15:90b6821bcf64 40 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 41 d_ = pad.get_direction();
evanso 18:11068b98e261 42 }
evanso 18:11068b98e261 43
evanso 19:1bc0a2d22054 44 void GameEngine::create_bullet(){
evanso 19:1bc0a2d22054 45 if (pad.A_pressed()){
evanso 19:1bc0a2d22054 46 // Bullet object
evanso 19:1bc0a2d22054 47 Weapons new_bullet;
evanso 19:1bc0a2d22054 48
evanso 19:1bc0a2d22054 49 new_bullet.init(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction());
evanso 19:1bc0a2d22054 50
evanso 20:febd920ec29e 51 // Stores bullet object in vector
evanso 19:1bc0a2d22054 52 bullet_vector.push_back(new_bullet);
evanso 20:febd920ec29e 53 }
evanso 20:febd920ec29e 54 }
evanso 20:febd920ec29e 55
evanso 20:febd920ec29e 56 void GameEngine::create_alien(){
evanso 20:febd920ec29e 57 // Alien object
evanso 20:febd920ec29e 58 Alien new_alien;
evanso 21:f7d7834e3af1 59
evanso 24:479da6ca0e7e 60 new_alien.init(pad);
evanso 20:febd920ec29e 61
evanso 20:febd920ec29e 62 // Stores alien object in vector
evanso 20:febd920ec29e 63 alien_vector.push_back(new_alien);
evanso 19:1bc0a2d22054 64 }
evanso 19:1bc0a2d22054 65
evanso 25:70b55f5bfc87 66 void GameEngine::create_explosion(){
evanso 25:70b55f5bfc87 67 // explosion object
evanso 25:70b55f5bfc87 68 Explosion new_explosion;
evanso 25:70b55f5bfc87 69
evanso 25:70b55f5bfc87 70 new_explosion.init();
evanso 25:70b55f5bfc87 71
evanso 25:70b55f5bfc87 72 // Stores explosion object in vector
evanso 25:70b55f5bfc87 73 explosion_vector.push_back(new_explosion);
evanso 25:70b55f5bfc87 74 }
evanso 25:70b55f5bfc87 75
evanso 19:1bc0a2d22054 76 void GameEngine::draw_bullets(){
evanso 20:febd920ec29e 77 // interates over bullet vector and get each new_bullet object to draw its self and move
evanso 19:1bc0a2d22054 78 for (int i = 0; i < bullet_vector.size(); i++){
evanso 19:1bc0a2d22054 79 bullet_vector[i].draw_bullet(lcd);
evanso 20:febd920ec29e 80
evanso 20:febd920ec29e 81 // deletes bullet object after bullet is out of view of paceship
evanso 20:febd920ec29e 82 int bullet_delete_counter = bullet_vector[i].get_bullet_delete_counter();
evanso 20:febd920ec29e 83 if(bullet_delete_counter >> 5){
evanso 20:febd920ec29e 84 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 20:febd920ec29e 85 }
evanso 19:1bc0a2d22054 86 }
evanso 19:1bc0a2d22054 87 }
evanso 19:1bc0a2d22054 88
evanso 20:febd920ec29e 89 void GameEngine::draw_aliens(){
evanso 20:febd920ec29e 90 // interates over alien vector and get each new_alien object to draw its self
evanso 20:febd920ec29e 91 for (int i = 0; i < alien_vector.size(); i++){
evanso 24:479da6ca0e7e 92 alien_vector[i].draw_alien(lcd,spaceship.get_pos(),d_, map.get_length_map(), map.get_position_x_map());
evanso 20:febd920ec29e 93
evanso 20:febd920ec29e 94 // Deletes bullet and alien if collision detected
evanso 20:febd920ec29e 95 if (alien_vector[i].check_collision(bullet_vector[i])){
evanso 25:70b55f5bfc87 96 create_explosion();
evanso 25:70b55f5bfc87 97 explosion_vector[i].set_explosion_posistion(alien_vector[i].get_pos());
evanso 25:70b55f5bfc87 98
evanso 25:70b55f5bfc87 99 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 25:70b55f5bfc87 100 alien_vector.erase(alien_vector.begin()+ i);
evanso 25:70b55f5bfc87 101 }
evanso 25:70b55f5bfc87 102 }
evanso 25:70b55f5bfc87 103 }
evanso 25:70b55f5bfc87 104
evanso 25:70b55f5bfc87 105 void GameEngine::draw_explosions(){
evanso 25:70b55f5bfc87 106 // interates over expoltion vector and draws each explosion object then deleted object after set size
evanso 25:70b55f5bfc87 107 for (int i = 0; i < explosion_vector.size(); i++){
evanso 25:70b55f5bfc87 108 explosion_vector[i].draw_explosion(lcd);
evanso 25:70b55f5bfc87 109
evanso 25:70b55f5bfc87 110 // delete explosion after reaches set size
evanso 25:70b55f5bfc87 111 if(explosion_vector[i].get_explosion_radius() == 8){
evanso 25:70b55f5bfc87 112 explosion_vector.erase(explosion_vector.begin()+ i);
evanso 20:febd920ec29e 113 }
evanso 20:febd920ec29e 114 }
evanso 20:febd920ec29e 115 }