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.

GameEngine/GameEngine.cpp

Committer:
evanso
Date:
2020-05-04
Revision:
20:febd920ec29e
Parent:
19:1bc0a2d22054
Child:
21:f7d7834e3af1

File content as of revision 20:febd920ec29e:

#include "GameEngine.h"

GameEngine::GameEngine() {
    
}
 
GameEngine::~GameEngine() {
    
}

void GameEngine::init() {
    pad.init();
    lcd.init();
    spaceship.init();
    map.init(pad);
    create_alien();
}

void GameEngine::gameplay_loop() {
    // clear screen 
    lcd.setContrast(pad.read_pot1());          
    lcd.clear();
    
    // Gets movements  
    read_joystick_direction();
    spaceship.movement(d_);
    create_bullet();

    // Draws 
    spaceship.draw(lcd);
    map.draw_map(lcd, d_);
    draw_aliens();
    draw_bullets();

    // refresh's screen
    lcd.refresh(); 
}

void GameEngine::read_joystick_direction(){
    d_ = pad.get_direction();
}

void GameEngine::create_bullet(){
    if (pad.A_pressed()){
        // Bullet object
        Weapons new_bullet;
        
        new_bullet.init(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction());
        
        // Stores bullet object in vector 
        bullet_vector.push_back(new_bullet);
    }
}

void GameEngine::create_alien(){
    // Alien object
    Alien new_alien;
    
    new_alien.init();
    
    // Stores alien object in vector 
    alien_vector.push_back(new_alien);
}

void GameEngine::draw_bullets(){
    // interates over bullet vector and get each new_bullet object to draw its self and move
    for (int i = 0; i < bullet_vector.size(); i++){
        bullet_vector[i].draw_bullet(lcd);
        
        // deletes bullet object after bullet is out of view of paceship    
        int bullet_delete_counter = bullet_vector[i].get_bullet_delete_counter();
        if(bullet_delete_counter >> 5){
             bullet_vector.erase(bullet_vector.begin()+ i);
        }
    }
}

void GameEngine::draw_aliens(){
    // interates over alien vector and get each new_alien object to draw its self
    for (int i = 0; i < alien_vector.size(); i++){
        alien_vector[i].draw_alien(lcd);
        
        // Deletes bullet and alien if collision detected
        if (alien_vector[i].check_collision(bullet_vector[i])){
            bullet_vector.erase(bullet_vector.begin()+ i);  
            alien_vector.erase(alien_vector.begin()+ i);          
        }
    }
}