ELEC2645 (2017/18) / Mbed 2 deprecated ll13jrm

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Food.cpp Source File

Food.cpp

00001 #include "Food.h"
00002 
00003 ///////////////// constructor/destructor /////////////////
00004 
00005 Food::Food()
00006 {
00007 
00008 }
00009 
00010 Food::~Food()
00011 {
00012 
00013 }
00014 
00015 ///////////////// global methods /////////////////
00016 
00017 int g_frame_counter()
00018 {
00019     
00020     extern int g_fc;
00021     
00022     return g_fc;
00023     
00024 }
00025 
00026 ///////////////// public methods /////////////////
00027 
00028 
00029 void Food::init(bool collision)
00030 {
00031     
00032     int frame_count = g_frame_counter();
00033     
00034     set_food_position(frame_count, 150, collision);
00035     
00036 }
00037 
00038 void Food::update(bool collision, int n_frames) 
00039 {
00040     // increment frame counter
00041     
00042     ++g_fc;
00043     
00044     int frame_count = g_frame_counter();
00045     
00046     set_food_position(frame_count, n_frames, collision);
00047          
00048 }
00049   
00050 void Food::draw(N5110 &lcd) 
00051 {
00052 
00053     lcd.setPixel(_x,_y,true);
00054     
00055 }     
00056 
00057 
00058 Vector2D Food::get_rand_pos()
00059 {
00060     
00061     Vector2D r;
00062     
00063     srand(time(NULL));
00064     
00065     // get random variables within screen dimensions 0-83, 0-47
00066     
00067     r.x = rand() % 83;
00068     r.y = rand() % 47;
00069     
00070     return r;
00071     
00072 }
00073 
00074 void Food::set_food_position(int set_frames, int number_frames, bool collision)
00075 {
00076     //_number_frames = number_frames;
00077     
00078     Vector2D pos = get_rand_pos();
00079     
00080     // check if collision with snake has occured
00081     
00082     if(collision) {
00083         
00084         _x = pos.x;
00085         _y = pos.y;
00086         
00087         
00088         // reset frame counter to 0
00089         g_fc = 0;
00090         
00091         }
00092         
00093         // check if required number of frames has elapsed 
00094            
00095     else if((set_frames == number_frames) || (set_frames == 0)) {
00096         
00097         _x = pos.x;
00098         _y = pos.y;
00099         
00100         // reset frame counter to 0
00101         
00102         printf("Frames Past %i \n ", g_fc);
00103 
00104         g_fc = 0;
00105         
00106         }
00107         
00108         else {
00109             
00110             _x = _x;
00111             _y = _y;
00112             
00113             }
00114         
00115         
00116 }
00117 
00118 Vector2D Food::get_food_position ()
00119 {
00120     
00121     Vector2D p = {_x, _y};
00122     
00123     return p;
00124     
00125 }