ELEC2645 (2019/20) / Mbed 2 deprecated ll17l2b

Dependencies:   mbed

DodgeEngine/DodgeEngine.cpp

Committer:
louisberard
Date:
2020-05-27
Revision:
2:d14bccec9948
Parent:
1:5b7c6b79a1c6
Child:
4:fe668b8cbd3e

File content as of revision 2:d14bccec9948:

#include "DodgeEngine.h"

DodgeEngine::DodgeEngine(){
//initialise score and boat position
    player_score = 0;
    y_boat = 24;
    x_boat = 76;
    
}
DodgeEngine::~DodgeEngine(){}

//function which holds the games logic 
void DodgeEngine::logic(N5110 &lcd, Gamepad &pad)
{       
    draw_screen(lcd);
    player_movements(lcd, pad);
    boat_logic(lcd, pad);
}
//Function for the boats logic
void DodgeEngine::boat_logic(N5110 &lcd, Gamepad &pad)
{   
    
    o1.set_y_boat(y_boat); 
    o1.set_x_boat(x_boat);    
    o1.sprite(lcd);
    boat_respawn(pad); //respawns the boat when it hits the other side of the screen
}

 
void DodgeEngine::draw_screen(N5110 &lcd)
{
    lcd.clear();
    lcd.drawLine(0,7, 84,7,1); //Top edge of display
    lcd.drawLine(0,47, 83,47,1); //Bottom edge of display
    lcd.printString("Dodge the boat",1,0); 
}
void DodgeEngine::player_movements(N5110 &lcd, Gamepad &pad)
{       
    swimmer.sprite(lcd);
    swimmer.get_x1();
    swimmer.get_y1();
    swimmer.movement(pad);
    
}
 
void DodgeEngine::boat_respawn(Gamepad &pad)
{ 
        x_boat += -2; //boat speed

//If statement which respawns boat (moves boat back to start of the screen)
    if(x_boat < 0) {
        x_boat = 76;
        player_score += 1;
        //y_boat gets a random position to move the boat back to
        y_boat = ((rand()%(48))+8);
    }
}

bool DodgeEngine::state(N5110 &lcd)
{   
    //checks if collision has occured and ends the game loop if it has 
    if(cd.Collision(lcd, x_boat, y_boat)== 1) {
        return true;}
   else{return false;
   }
   }
//Game over screen
void DodgeEngine::game_over(N5110 &lcd, Gamepad &pad)
{   
    
    lcd.clear();
    lcd.printString("SMASH",30,1);
    lcd.printString("Score: ", 0,3);
    char score_buffer[14];
    sprintf(score_buffer,"%2d",player_score);
    lcd.printString(score_buffer,42,3);
    lcd.refresh();
}