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-04-29
Revision:
17:25d79cca203a
Parent:
16:1ee3d3804557
Child:
18:11068b98e261

File content as of revision 17:25d79cca203a:

#include "GameEngine.h"

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

void GameEngine::init() {
    pad.init();
    lcd.init();
    spaceship.init();
    map.init(pad);
    move_map_= 0;
}

void GameEngine::gameplay_loop() {
    // clear screen 
    lcd.setContrast(pad.read_pot1());          
    lcd.clear();
    
    // Gets movements  
    read_joystick_direction();
    spaceship.movement(d_);
    weapons.calc_bullets_start_pos(spaceship.get_pos(),spaceship.get_spaceship_sprite_direction());
    
    // Redraws 
    spaceship.draw(lcd);
    map.draw_map(lcd, calc_map_movement());

    //refresh's screen
    lcd.refresh(); 
    
    //draws map again and refreshes screen so map moves faster than spaceship but doesnt require double pixel moement 
    map.draw_map(lcd, move_map_);
    lcd.refresh();
}

int GameEngine::calc_map_movement(){ 
    // Gets postition of spaceship and sprite direction      
    Vector2D spaceship_pos= spaceship.get_pos();      

    // moves the map in oposite direction to spaceship when it's position is at min and max x positions and joystick has direction     
    if (d_ == W || d_ == NW || d_ == SW){
        move_map_ = 1;
        
        // moves map fater when changing direction to offset ship 
        if (spaceship_pos.x != 57){
           move_map_ = 1;
        }
    }else if (d_ == E || d_ == NE || d_ == SE){ 
        move_map_ = -1; 
        
        // moves map fater when changing direction to offset ship
        if (spaceship_pos.x != 14){
           move_map_ = -1;
        } 
    }else {
         move_map_ = 0;
    }
    
    return move_map_;
    
    // Debug and check variables when function is called 
    #ifdef CALCULATE_MAP_MOVEMENT_DEBUG   
        printf("move map = %d\n", move_map_);   
        printf("direction = %d\n", d_);     
        printf("x = %d\n", position_x_spaceship_);
    #endif
}

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