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:
Mon May 04 10:47:06 2020 +0000
Revision:
20:febd920ec29e
Parent:
19:1bc0a2d22054
Child:
21:f7d7834e3af1
Added collision detection so the alien and bullet is deleted if a bullet hits it.

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 18:11068b98e261 34
evanso 18:11068b98e261 35 // refresh's screen
evanso 18:11068b98e261 36 lcd.refresh();
evanso 13:12276eed13ac 37 }
evanso 13:12276eed13ac 38
evanso 15:90b6821bcf64 39 void GameEngine::read_joystick_direction(){
evanso 13:12276eed13ac 40 d_ = pad.get_direction();
evanso 18:11068b98e261 41 }
evanso 18:11068b98e261 42
evanso 19:1bc0a2d22054 43 void GameEngine::create_bullet(){
evanso 19:1bc0a2d22054 44 if (pad.A_pressed()){
evanso 19:1bc0a2d22054 45 // Bullet object
evanso 19:1bc0a2d22054 46 Weapons new_bullet;
evanso 19:1bc0a2d22054 47
evanso 19:1bc0a2d22054 48 new_bullet.init(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction());
evanso 19:1bc0a2d22054 49
evanso 20:febd920ec29e 50 // Stores bullet object in vector
evanso 19:1bc0a2d22054 51 bullet_vector.push_back(new_bullet);
evanso 20:febd920ec29e 52 }
evanso 20:febd920ec29e 53 }
evanso 20:febd920ec29e 54
evanso 20:febd920ec29e 55 void GameEngine::create_alien(){
evanso 20:febd920ec29e 56 // Alien object
evanso 20:febd920ec29e 57 Alien new_alien;
evanso 20:febd920ec29e 58
evanso 20:febd920ec29e 59 new_alien.init();
evanso 20:febd920ec29e 60
evanso 20:febd920ec29e 61 // Stores alien object in vector
evanso 20:febd920ec29e 62 alien_vector.push_back(new_alien);
evanso 19:1bc0a2d22054 63 }
evanso 19:1bc0a2d22054 64
evanso 19:1bc0a2d22054 65 void GameEngine::draw_bullets(){
evanso 20:febd920ec29e 66 // interates over bullet vector and get each new_bullet object to draw its self and move
evanso 19:1bc0a2d22054 67 for (int i = 0; i < bullet_vector.size(); i++){
evanso 19:1bc0a2d22054 68 bullet_vector[i].draw_bullet(lcd);
evanso 20:febd920ec29e 69
evanso 20:febd920ec29e 70 // deletes bullet object after bullet is out of view of paceship
evanso 20:febd920ec29e 71 int bullet_delete_counter = bullet_vector[i].get_bullet_delete_counter();
evanso 20:febd920ec29e 72 if(bullet_delete_counter >> 5){
evanso 20:febd920ec29e 73 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 20:febd920ec29e 74 }
evanso 19:1bc0a2d22054 75 }
evanso 19:1bc0a2d22054 76 }
evanso 19:1bc0a2d22054 77
evanso 20:febd920ec29e 78 void GameEngine::draw_aliens(){
evanso 20:febd920ec29e 79 // interates over alien vector and get each new_alien object to draw its self
evanso 20:febd920ec29e 80 for (int i = 0; i < alien_vector.size(); i++){
evanso 20:febd920ec29e 81 alien_vector[i].draw_alien(lcd);
evanso 20:febd920ec29e 82
evanso 20:febd920ec29e 83 // Deletes bullet and alien if collision detected
evanso 20:febd920ec29e 84 if (alien_vector[i].check_collision(bullet_vector[i])){
evanso 20:febd920ec29e 85 bullet_vector.erase(bullet_vector.begin()+ i);
evanso 20:febd920ec29e 86 alien_vector.erase(alien_vector.begin()+ i);
evanso 20:febd920ec29e 87 }
evanso 20:febd920ec29e 88 }
evanso 20:febd920ec29e 89 }