ELEC2645 (2017/18) / Mbed 2 deprecated ll13jrm

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SnakeEngine.cpp Source File

SnakeEngine.cpp

00001 #include "SnakeEngine.h"
00002 
00003 ///////////////// constructor/destructor /////////////////
00004 
00005 SnakeEngine::SnakeEngine()
00006 {
00007 
00008 }
00009 
00010 SnakeEngine::~SnakeEngine()
00011 {
00012 
00013 }
00014 ///////////////// global methods /////////////////
00015 
00016 int g_tail_length()
00017 {
00018     
00019     extern int g_tl;
00020     
00021     return g_tl;
00022     
00023 }
00024 
00025 void g_frame_time(int frame_time)
00026 {
00027     
00028     g_ft = frame_time;
00029         
00030 }
00031 
00032 
00033 ///////////////// public methods /////////////////
00034 
00035 void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y, int n_frames)
00036 {
00037     _snake_pos_x = snake_pos_x; 
00038     _snake_pos_y = snake_pos_y;
00039     _in = in;
00040     _cur = cur;
00041     _n_frames = n_frames;
00042     g_frame_time(_n_frames);
00043     
00044     _snake.init(_in, _cur, _snake_pos_x, _snake_pos_y);
00045     
00046     _food.init(true);
00047     
00048 }
00049 
00050 void SnakeEngine::update(Gamepad &pad)
00051 {
00052     bool food_col;
00053     int food_spawn_time;
00054     ++g_engine_fc;
00055     
00056     increase_speed(5, 1.1);
00057     food_spawn_time = food_spawning_time(3);
00058     _snake.update(_in, _cur);
00059     printf("Spawn time %i \n", food_spawn_time);
00060     // printf("Frame Time %i \n", g_ft);
00061     
00062     _food.update(_collision, food_spawn_time);
00063     food_col = detect_food_collision(pad);
00064     _collision = food_col;
00065     set_tail_length(food_col);
00066     set_tail_array(pad);
00067     g_wall = detect_wall_collision(pad);
00068     //printf("%i%", g_wall);
00069 
00070 }
00071 
00072 
00073 void SnakeEngine::draw(N5110 &lcd)
00074 {
00075     
00076     // puts frame around the game area
00077     
00078     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00079     lcd.setContrast(0.5);
00080     
00081     // draws food to lcd
00082     
00083     _food.draw(lcd);
00084     
00085     // draws snake head to lcd
00086     
00087     _snake.draw(lcd);
00088     
00089     // draws snake tail to lcd
00090     
00091     draw_tail(lcd);
00092     
00093 }
00094 
00095 
00096 void SnakeEngine::draw_tail(N5110 &lcd)
00097 {
00098     int length = g_tl;
00099     int c;
00100     c = g_engine_fc % 2;
00101     int i;
00102     int x;
00103     int y;
00104     
00105     //printf("Odd/Even %i \n", c);
00106     
00107     // runs through each segment in the tail and draws to the lcd
00108     
00109     for(i=0; i<=length; ++i) {
00110     
00111     // when odd draw odd array
00112             
00113     if(c == 1) {
00114         
00115         x = g_odd_array_x[i];
00116         y = g_odd_array_y[i];
00117         
00118         lcd.setPixel(x,y,true);
00119                 
00120         }
00121         
00122         // when even draw the even array
00123         
00124         else if (c == 0) {
00125         
00126             x = g_even_array_x[i];
00127             y = g_even_array_y[i];
00128             
00129             lcd.setPixel(x,y,true);
00130                         
00131             }
00132             
00133         }
00134 }
00135 
00136 
00137 void SnakeEngine::get_input(Gamepad &pad)
00138 {
00139     
00140     _in = pad.get_direction();
00141     
00142 }
00143 
00144 bool SnakeEngine::detect_food_collision(Gamepad &pad)
00145 {
00146     Vector2D snake_pos = _snake.get_snake_position();
00147     Vector2D food_pos = _food.get_food_position();
00148     
00149     bool success_flag = false;
00150     
00151     // when the position of the head and food are the same return true
00152     
00153     if((snake_pos.x == food_pos.x) && (snake_pos.y == food_pos.y)) {
00154         
00155         success_flag = true;
00156         
00157         }
00158         
00159         else {
00160             
00161             success_flag = false;
00162             
00163             }
00164         
00165     return success_flag;
00166     
00167 }
00168 bool SnakeEngine::detect_wall_collision(Gamepad &pad)
00169 {
00170     
00171     Vector2D snake_pos = _snake.get_snake_position();
00172 
00173     bool success_flag = false;
00174     
00175     // when an snake position is at an edge return true
00176     
00177     if((snake_pos.x == (0 || 84)) || (snake_pos.y == (0 || 48))) {
00178         
00179         success_flag = true;
00180         
00181         }
00182         
00183         return success_flag;
00184     
00185 }
00186 
00187 
00188 void SnakeEngine::set_tail_length(bool collision_detected)
00189 {
00190     // when a collision is detected increment the tail length
00191     
00192     if(collision_detected) {
00193         
00194         ++g_tl;
00195         
00196         }
00197         
00198         else {
00199             
00200             g_tl = g_tl;
00201             
00202             }
00203             
00204 }
00205 
00206 int SnakeEngine::get_tail_length()
00207 {
00208     
00209     int length = g_tl;
00210     
00211     return length;
00212     
00213 }
00214 
00215 void SnakeEngine::set_odd_array(Gamepad &pad)
00216 {
00217     
00218     int i;
00219     int length = g_tl;
00220     
00221     extern int g_even_array_x[100];
00222     extern int g_even_array_y[100];
00223     
00224     Vector2D pos = _snake.get_snake_position();
00225     
00226     // sets the first value in the array to the new position
00227     
00228     g_even_array_x[0] = pos.x;
00229     g_even_array_y[0] = pos.y;
00230     
00231     // starting from the last element in the arrays work back to zero
00232     
00233     for(i = length; i >= 0; --i) {
00234         
00235         // switch positions of the indexes adding the new head position
00236         
00237         if(i > 0) {
00238             
00239         g_odd_array_x[i] = g_even_array_x[i-1];
00240         g_odd_array_y[i] = g_even_array_y[i-1];
00241         
00242         }
00243 
00244         // when snake has no length position 0 is the new position
00245 
00246         else if(i == 0) {
00247                         
00248             g_odd_array_x[0] = pos.x;
00249             g_odd_array_y[0] = pos.y;
00250             
00251             }
00252         
00253     }
00254     
00255 }
00256 
00257 void SnakeEngine::set_even_array(Gamepad &pad)
00258 {
00259     
00260     int i;
00261     int length = g_tl;
00262     
00263     extern int g_odd_array_x[100];
00264     extern int g_odd_array_y[100];
00265     
00266     Vector2D pos = _snake.get_snake_position();
00267     
00268     // sets the first value in the array to the new position
00269    
00270     g_odd_array_x[0] = pos.x;
00271     g_odd_array_y[0] = pos.y;
00272 
00273     // starting from the last element in the arrays work back to zero
00274     
00275     for(i = length; i >= 0; --i) {
00276         
00277         // switch positions of the indexes adding the new head position
00278 
00279         if(i > 0) {
00280             
00281         g_even_array_x[i] = g_odd_array_x[i-1];
00282         g_even_array_y[i] = g_odd_array_y[i-1];
00283         
00284         }
00285         
00286         // when snake has no length position 0 is the new position
00287 
00288         else if(i == 0) {
00289             
00290             g_even_array_x[0] = pos.x;
00291             g_even_array_y[0] = pos.y;
00292             
00293             }
00294 
00295     }
00296     
00297 }
00298 
00299 
00300 void SnakeEngine::set_tail_array(Gamepad &pad)
00301 {
00302     
00303     int c = g_engine_fc % 2;
00304     
00305     // when the frame counter is odd set odd array
00306     
00307     if(c == 1) {
00308         
00309         set_odd_array(pad);
00310         
00311         }
00312         
00313         // when the frame counter is even set even array
00314         
00315         else if (c == 0) {
00316             
00317             set_even_array(pad);
00318             
00319             }
00320             
00321 }
00322 
00323 int SnakeEngine::food_spawning_time(int frame_decrementer)
00324 {
00325     
00326     int c = 0;
00327     
00328     // c is equal to the tail length - an incrementer such that c is never more
00329     // than 0 and n = frame decremtner away from 0 such that the logic 
00330     // c == 0 works
00331     
00332     c = g_tl - g_n;
00333     
00334     //printf("C = %i", c);
00335     
00336     // the frame periodity must be more than 100 to ensure the game reamains playable
00337     
00338     if((c == 0) && (g_ft > 100)) {
00339         
00340         g_n = g_n + frame_decrementer;
00341         
00342         g_ft -= 10;
00343         
00344         frame_decrementer += frame_decrementer;
00345          
00346         }
00347     
00348     return g_ft;
00349     
00350 }
00351 
00352 void SnakeEngine::increase_speed(int resetter, float speed_decrementer)
00353 {
00354     
00355     int c = 0;
00356     c = g_tl - g_n;
00357     if(g_tl == 0) {
00358         
00359         g_speed = 0.4;
00360             
00361         }
00362 
00363     if(c == 0) {
00364         
00365         g_n = g_n + resetter;
00366         
00367         g_speed = g_speed/speed_decrementer;
00368         
00369         resetter += resetter;
00370         
00371         }
00372         
00373 }
00374