ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el17oc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GameEngine.cpp Source File

GameEngine.cpp

00001 #include "GameEngine.h"
00002 
00003 GameEngine::GameEngine()        //constructor
00004 {
00005 
00006 }
00007 
00008 GameEngine::~GameEngine()      //deconstructor
00009 {
00010 }
00011 
00012 
00013 void GameEngine::print_scores(N5110 &lcd, Snake &snake)
00014 {
00015 
00016     int score = snake.get_score();                                  //gets score from snake class object
00017 
00018     char buffer1[14];
00019     sprintf(buffer1,"%2d",score);                                  //sprintf is used to print as the program is displaying a variable
00020     lcd.printString(buffer1,0,48);
00021 }
00022 
00023 
00024 
00025 
00026 void GameEngine::get_LEDs(Gamepad &pad, Snake &snake)       //controls LEDs based on snake position
00027 {
00028     pad.leds_off();                                         //initialise leds
00029     Vector2D Snakehead = snake.get_Snakehead();             //call values stored in _x0 and _y0 to identify the snakehead
00030     int _x0 = Snakehead.x;
00031     int _y0 = Snakehead.y;
00032     //depending where the snakehead is on the screen, different LEDs will turn on indicating its postion
00033     // top right led on
00034     if (_x0 >= 42 && _y0 <= 16) {               //defines paramaters of the top right quadrant of the lcd rectangle and turns on and off the top right LED if the x and y values are in the top right quadrant
00035 
00036         pad.led(4, 1);                        //turn on top right led4
00037         wait(0.2);                            //wait 0.2 seconds
00038         pad.led(4, 0);                        //turn it off
00039     }
00040     // topleft led on
00041     if (_x0 <= 42 && _y0 <= 16 ) {             //parameter for top left
00042 
00043         pad.led(1, 1);                          //turns on top left led
00044         wait(0.2);
00045         pad.led(1, 0);
00046     }
00047     //bottom left                           //defines paramaters of the bottom left quadrant of the rectangle on the lcd display and turns on and off the bottom left LED
00048     if (_x0 <=42 && _y0 >= 16 ) {
00049 
00050         pad.led(3,1);                       //turns on bottom left led
00051         wait(0.2);
00052         pad.led(3, 0);
00053     }
00054     //bottom right
00055     if (_x0 >= 42 && _y0 >= 16) {          //there are some overlap on the symetrical x and y axis where conditions are true in both loops causing leds to coome on at the same time and alternately blink
00056         // top right led on
00057         pad.led(6, 1);
00058         wait(0.2);
00059         pad.led(6, 0);                      //turns of bottom right led
00060     } else {
00061     }
00062 }
00063 
00064 void GameEngine::print_countdown(N5110 &lcd, Snake &snake)          //prints counter
00065 {
00066 
00067     int countdown = snake.get_countdown();                         //Accessing the value of the member variale _countdown
00068 
00069     char buffer1[14];
00070     sprintf(buffer1,"%2d",countdown);                              //printing value onto lcd in each loops. - counter is set to decrement by 1 each loop
00071     lcd.printString(buffer1,0,4);  //
00072 // printf("  countdown= %d   ", countdown);
00073 }