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.

Weapons/Weapons.cpp

Committer:
evanso
Date:
2020-05-15
Revision:
36:27aa597db3d2
Parent:
34:85ccc16f24d2
Child:
82:3211b31e9421

File content as of revision 36:27aa597db3d2:

#include "Weapons.h"

Weapons::Weapons() {
    
}
 
Weapons::~Weapons() {
    
}
 
void Weapons::init(Vector2D sprite_pos, bool sprite_direction, 
bool bullet_type) {
    // bullet for alien or for spacship 
    set_direction(sprite_direction);
    if (bullet_type){
        calc_bullets_start_pos(sprite_pos, sprite_direction);
    }else{
        calc_alien_bullets_start_pos(sprite_pos, sprite_direction);
    }
    bullet_delete_counter_ = 0;
}

void Weapons::smart_bomb(N5110 &lcd){
    // turn backlight off and on twice
    lcd.backLightOff();
    wait(0.02);
    lcd.backLightOn();
    wait(0.02);
    lcd.backLightOff();
    wait(0.02);
    lcd.backLightOn();
}


void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos, 
bool spaceship_sprite_direction_){
    // Sets start position of bullet to spaceships nose
    if (spaceship_sprite_direction_){
        position_x_ = spaceship_pos.x + 13;
        position_y_ = spaceship_pos.y + 2;
    }else{
        position_x_ = spaceship_pos.x;
        position_y_ = spaceship_pos.y + 2;
    }
    #ifdef POSITION_BULLET_DEBUG   
        printf("X = %d\n", position_x_);       
        printf("Y = %d\n", position_y_);
    #endif 
}

void Weapons::draw_bullet(N5110 &lcd){
    // draws then moves the bullet position
    lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
    if(direction_){
        position_x_ += 3;
    }else{
        position_x_ -= 3;
    }
  
    // increments counter
    bullet_delete_counter_++;
}

void Weapons::calc_alien_bullets_start_pos(Vector2D alien_pos, 
bool alien_sprite_direction_){
    // Sets start position of bullet to middle of alien
    position_x_ = alien_pos.x + 3;
    position_y_ = alien_pos.y + 3;
    
    //gets a random direction for the bullet to travel in
    set_random_move();
}

void Weapons::draw_alien_bullet(N5110 &lcd,Direction d_){
    // draws then moves the bullet position
    lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
    
    //Moves bullet in random direction and accomodates for spaceship movement
    move_direction();
    position_x_ += calc_sprite_movement(d_);
    
    // increments counter
    bullet_delete_counter_++;
}

void Weapons::set_pos_one(Vector2D pos){
    position_x_ = pos.x;
    position_y_ = pos.y;
}

void Weapons::set_direction(bool sprite_direction){
    direction_ = sprite_direction;   
}

int Weapons::get_bullet_delete_counter(){
    return bullet_delete_counter_; 
}

bool Weapons::get_direction(){
    return direction_;   
}