![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Owen Cavender 201159294
Dependencies: mbed
Diff: GameEngine.cpp
- Revision:
- 2:ffbfd3f53ee2
- Parent:
- 1:897160a1a3ae
diff -r 897160a1a3ae -r ffbfd3f53ee2 GameEngine.cpp --- a/GameEngine.cpp Tue May 26 12:17:59 2020 +0000 +++ b/GameEngine.cpp Sat May 30 06:12:09 2020 +0000 @@ -1,69 +1,73 @@ #include "GameEngine.h" -GameEngine::GameEngine() + +GameEngine::GameEngine() //constructor { } -GameEngine::~GameEngine() +GameEngine::~GameEngine() //deconstructor { - } -void GameEngine::init(int shx, int shy, int apx, int apy) + +void GameEngine::print_scores(N5110 &lcd, Snake &snake) { - _shx = shx; - _shy = shy; //intial value in front of snake - _apx = apx; - _apy = apy; + + int score = snake.get_score(); //gets score from snake class object + + char buffer1[14]; + sprintf(buffer1,"%2d",score); //sprintf is used to print as the program is displaying a variable + lcd.printString(buffer1,0,48); } -void GameEngine::set_Snakehead(Vector2D Snakehead) -{ - Snakehead.x = _shx; - Snakehead.y = _shy; -} -Vector2D GameEngine::get_Snakehead() +void GameEngine::get_LEDs(Gamepad &pad, Snake &snake) //controls LEDs based on snake position { - Vector2D Snakehead = {_shx, _shy}; - return Snakehead; -} - + pad.leds_off(); //initialise leds + Vector2D Snakehead = snake.get_Snakehead(); //call values stored in _x0 and _y0 to identify the snakehead + int _x0 = Snakehead.x; + int _y0 = Snakehead.y; + //depending where the snakehead is on the screen, different LEDs will turn on indicating its postion + // top right led on + 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 -Vector2D GameEngine::get_Applepos() -{ - Vector2D Applepos = {_apx, _apy}; - return Applepos; -} + pad.led(4, 1); //turn on top right led4 + wait(0.2); //wait 0.2 seconds + pad.led(4, 0); //turn it off + } + // topleft led on + if (_x0 <= 42 && _y0 <= 16 ) { //parameter for top left - -void GameEngine::set_Applepos(N5110 &lcd) -{ - - int appleposx = rand()%84; //is this generating new position or will it just stick to one random selection - need to make sure its in the loop - int appleposy = rand()%24; - + pad.led(1, 1); //turns on top left led + wait(0.2); + pad.led(1, 0); + } + //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 + if (_x0 <=42 && _y0 >= 16 ) { - if(lcd.getPixel(appleposx, appleposy)) { // this pixel is set } else { // this pixel is clear } - appleposx = rand()%84; - appleposy = rand()%24; // making sure the apple doesnt spawn inside the snakes body or wall which would increase the score + pad.led(3,1); //turns on bottom left led + wait(0.2); + pad.led(3, 0); + } + //bottom right + 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 + // top right led on + pad.led(6, 1); + wait(0.2); + pad.led(6, 0); //turns of bottom right led } else { - - - _apx = appleposx; //i and j are fed into applepos.x/y -- those values are then fed into set_applepos which assigngs that value to _appleposx/y which then is fed into get_applepos where it is stored and returned - _apy = appleposy; //alters value of private variable - this can then be accessed by get_Applepos - - lcd.setPixel(_apx,_apy, true); } } +void GameEngine::print_countdown(N5110 &lcd, Snake &snake) //prints counter +{ -//void GameEngine::clear_applepos(N5110 &lcd) { -// int length = _snake.get_Length(); -// wait(length x fps); -// lcd.setPixel(Applepos.x, Applepos.y,0); -// } -//void Snakerender_apple() -// lcd.setPixel(_apx,_apy, true); + int countdown = snake.get_countdown(); //Accessing the value of the member variale _countdown + + char buffer1[14]; + sprintf(buffer1,"%2d",countdown); //printing value onto lcd in each loops. - counter is set to decrement by 1 each loop + lcd.printString(buffer1,0,4); // +// printf(" countdown= %d ", countdown); +}