Final Commit

Dependencies:   mbed

Food/Food.cpp

Committer:
JRM1986
Date:
2018-04-30
Revision:
18:406fc298a7c4
Parent:
17:94dd8a691d4a
Child:
21:63c5590cb2c2

File content as of revision 18:406fc298a7c4:

#include "Food.h"


Food::Food()
{

}

Food::~Food()
{

}


int g_frame_counter()
{
    
    extern int g_fc;
    
    return g_fc;
    
}

void Food::init(bool collision)
{
    int frame_count = g_frame_counter();
    set_food_position(frame_count, 50, collision);
    
}

void Food::update(bool collision) 
{
    
    ++g_fc;
    
    int frame_count = g_frame_counter();
    set_food_position(frame_count, 50, collision);
         
}
  
void Food::draw(N5110 &lcd) 
{

    lcd.setPixel(_x,_y,true);
    
}     


Vector2D Food::get_rand_pos()
{
    
    Vector2D r;
    
    srand(time(NULL));
    
    r.x = rand() % 82;
    r.y = rand() % 46;
    
    return r;
    
}

void Food::set_food_position(int set_frames, int number_frames, bool collision)
{
    
    Vector2D pos = get_rand_pos();
    
    if(collision) {
        
        _x = pos.x;
        _y = pos.y;
        
        g_fc = 0;
        
        }
           
    else if((set_frames == number_frames) || (set_frames == 0)) {
        
        _x = pos.x;
        _y = pos.y;
        
        g_fc = 0;
        
        }
        
        else {
            
            _x = _x;
            _y = _y;
            
            }
        
        
}

Vector2D Food::get_food_position()
{
    
    Vector2D p = {_x, _y};
    
    return p;
    
}